Return nearest contour point + memory leak fix #30
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is related to issue #29 . Two key changes have been made:
A new
return_nearest_contour
option has been added to thedistance
,travel_time
andextension_velocities
functions (default to False). If true, the function returns an additional array which contains the coordinates of the nearest phi = 0 point for each point in the input array. For e.g., for a 2D image of size HxW, the returned nearest contour array will have size HxWx2.The current code can leak memory because of the way in which
Py_BuildValue
is used. Specifically, usingO
in the format string causes the reference counter of the objects to be increased, so the returned array(s) are never freed. The solution is to useN
instead, which doesn't increase the reference counter.I didn't name the option 'image_gradient' since it returns the absolute coordinates of the nearest phi = 0 point, not the coordinate offset to it, but that can be changed easily.
I also wrote a simple interactive script that can be used to test the functionality. The code for it is pasted here (requires OpenCV, NumPy). It basically converts the generated distance array into an easily visualizable color map. Clicking anywhere on the image causes the nearest contour point to be marked with a green circle (the exact coordinates are also printed to console).
https://www.pastiebin.com/5d1a070f101a2
A simple test image on which it can be applied (change the file path in the script accordingly). It appears all black but there are some points with intensity 1: