Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return nearest contour point + memory leak fix #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Ali2500
Copy link

@Ali2500 Ali2500 commented Jul 1, 2019

This is related to issue #29 . Two key changes have been made:

  1. A new return_nearest_contour option has been added to the distance, travel_time and extension_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.

  2. The current code can leak memory because of the way in which Py_BuildValue is used. Specifically, using O 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 use N 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:

mask

@mrmotallebi
Copy link

@Ali2500 I don't know if you still remember your code but I tried to merge your changes to the head of this repo (mrmotallebi#1). However I am getting all 0s in some cases for the nearest_contour output (especially when part of the input array is masked out). Would you have any idea why?

@jkfurtney
Copy link
Member

Can you give an example of what you are seeing and what the expected behavior is? I can revisit this branch and help with the merge. Out of curiosity, what is your use case? Thanks.

@mrmotallebi
Copy link

mrmotallebi commented Jul 19, 2023

Can you give an example of what you are seeing and what the expected behavior is? I can revisit this branch and help with the merge. Out of curiosity, what is your use case? Thanks.

@jkfurtney Try this:

X, Y = np.meshgrid(np.linspace(0,15,15), np.linspace(0,15,15))
phi = np.ones_like(X)
mask = np.zeros_like(phi).astype(bool)
mask[0,:] = True
mask[:,0] = True
phi = np.ma.MaskedArray(phi, mask)
phi[10,10] = -1
dist, contour = skfmm.distance(phi, return_nearest_contour=True)

The distance is correctly calculated, but controur is all zeros.
(It was based on the fourth example in the docs .)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants