Repository navigation
Make hough_circle_peaks respect min_xdistance, min_ydistance - #4350
Conversation
|
It's a good suggestion. After reading the code, not tested it, I think there is a flaw. Peak's heights depend on the perimeter. So this algorithm will prefer larger circles. A dimensionless peak height should be better. |
Thanks for pointing out. Whether larger circles are preferred is already controlled by the kwarg |
|
I'm not sure what would be the meaning of normalized=False regarding the removed circles. (I admit that the interpretation is also unclear for the sorting feature, but at least, it is less harmful). Any comment @scikit-image/core ? |
| cx = [] | ||
| cy = [] | ||
| r = [] | ||
| for ac, x, y, ra in zip(accum_sorted, cx_sorted, cy_sorted, r_sorted): |
There was a problem hiding this comment.
could we execute this piece of code only if min/max_xdistance>1? I guess tt can take quite a long time for a large number of circles with the two loops (I haven't tried). Or could cythonize this part. I wonder how long it takes says for a 2000x2000 image with 1000 random circles?
There was a problem hiding this comment.
Another option could be to use a KDTree here for the x and y coordinates (one might have to add some jitter, I don't know from the top of my head if duplicate coordinates are allowed for KDTree), and use the query_ball_point method (https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.KDTree.query_ball_point.html#scipy.spatial.KDTree.query_ball_point) to keep a complexity of O(N) (with N the number of circles).
There was a problem hiding this comment.
Just to quickly chime in: in case this data structure is a good fit and you decide to use it I'd recommend cKDTree which is the faster version written in C. I've had great experiences (speed ups) with it so far! :)
There was a problem hiding this comment.
I think cKDTree supports duplicate coordinates and it will return duplicates as well if queried.
There was a problem hiding this comment.
could we execute this piece of code only if
min/max_xdistance>1? I guess it can take quite a long time for a large number of circles with the two loops (I haven't tried). Or could cythonize this part.
it is definitely helpful to skip picking when default values are used. and i agree that the two nested loops can led to O(N^2) complexity (N = number of peaks) and that this part of code should be optimized.
There was a problem hiding this comment.
Another option could be to use a KDTree here for the x and y coordinates ...
I have tried using a cKDTree as suggested by @lagru and I believe the code now has better performance.
One subtile difference is that query_ball_point might still keep circles which centers are close in one dimension but far enough than specified distance in 2d plane. For example, two circles A, B with center (0, 0), (10, 10) should be considered close if min distance is 10 in both x and y directions. However, B will not be returned if we search A's neighbors within a distance of 10, i.e. sqrt(2) > 1.
There was a problem hiding this comment.
Oh you're right. We really made our life more complicated with having two parameters for the distances along x and y, but ok why not. One could still use the cKDTree with the largest distance times sqrt(2), and then test if the KDE neighbours should be eliminated or not.
There was a problem hiding this comment.
with the largest distance times sqrt(2)
or np.hypot(dx, dy)? =)
There was a problem hiding this comment.
In my latest change, I scaled in both x and y dimension so we don't get an unnecessarily long list of neighbors.
|
@sciunto, regarding normalization, I think the code is ok, what is needed is more explanations in the docstring (probably in a Notes section). Indeed, there are two ways of normalizing:
So either you normalize in the hough_transform and then there is no bias between small and large circles (with the caveat that you can detect a lot of small spurious circles), or you always have the option to normalize at the Would you agree with the above @sciunto ? I might be missing something. In any case this will require a solid paragraph in the docstring. And thank you @xiaoyu-wu for taking care of this! |
|
@emmanuelle I agree, I just do not find it super-safe. |
|
@sciunto I agree but I still think that the proposed behaviour is better than the existing one. So let's add explanations and see how it goes... |
|
Hello @xiaoyu-wu! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found: There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻 Comment last updated at 2019-12-20 16:06:22 UTC |
|
Can someone help with the failing CI? It says "The job exceeded the maximum time limit for jobs, and has been terminated." I have no clue why. Doesn't seem to be related with my changes. |
|
Sometimes the builds hang. I've restarted it. Thank you! |
|
@jni Can you please restart CI? |
jni
left a comment
There was a problem hiding this comment.
@xiaoyu-wu this is a great improvement, thank you! I've made some comments about the code style, which I'm happy to address if you are pressed for time. Overall though I'm very happy with the functionality.
| y_close = abs(scaled_cy_sorted[ni] - scaled_cy_sorted[i]) <= 1 | ||
| if x_close or y_close: | ||
| removed[ni] = True | ||
| i += 1 |
There was a problem hiding this comment.
For this section, I would:
- use a for-loop instead of a while-loop.
for i in range(tnp): - not scale, but rather use
np.hypot(min_xdistance, min_ydistance)for the ball tree radius, and compare the distance tomin_xdistanceandmin_ydistanceinstead of 1. This removes some additional variables, and makes reasoning about the code more local. - not create lists, but rather, use
removedat the very end to subset the arrays with boolean masks when returning.
@xiaoyu-wu would you be happy to make these modifications? If you don't have time, I would be happy to take over for you if needed.
There was a problem hiding this comment.
I would also make this whole chunk a separate function that we call out to, to keep the hough_peaks code itself shorter and more readable.
There was a problem hiding this comment.
use a for-loop instead of a while-loop.
for i in range(tnp):
Here the while loop is used to stop the iteration early once the number of distant circles reach max numbers to keep. The number of iteration is unknown, which needs a while loop.
There was a problem hiding this comment.
not scale, but rather use np.hypot(min_xdistance, min_ydistance) for the ball tree radius, and compare the distance to min_xdistance and min_ydistance instead of 1. This removes some additional variables, and makes reasoning about the code more local.
I agree that the code is easier to read this way. There is one potential pitfall when min_xdistance and min_ydistance differ largely. Each time the list returned by query_ball_point can include too many unnecessary candidates. But I understand it might be a very rare case.
not create lists, but rather, use removed at the very end to subset the arrays with boolean masks when returning.
Agreed and changed as suggested.
There was a problem hiding this comment.
@xiaoyu-wu I don't understand: i starts at 0, is incremented by 1 each time, and is checked against a maximum value. There are no other increments of i and there is no break anywhere? Right now you have:
i = 0
while i < max_points:
# bunch of code that does not modify i
i += 1This is exactly equivalent to:
for i in range(max_points):
# bunch of code that does not modify i... Or am I missing something?
There was a problem hiding this comment.
@jni you are right. Actually it was a bug. It could return circles less than specified total_num_peaks. It wasn't caught in test because total_num_peaks was tested with value 1. I fixed the bug (now using a for loop) and added another regression test.
There was a problem hiding this comment.
@jni With the latest change, I think I have addressed all your comments and the PR is ready for a final round of review. (CI needs to be restarted.)
|
No problem. I can address your comments in this PR. |
|
Thank you for your contribution and for your patience iterating with us, @xiaoyu-wu! 🎉 |
|
@jni @emmanuelle @sciunto @lagru Thank you all for your feedback. I learned a lot in this PR. |
Description
Closes issue #2728
For circles with different radius but close in distance, only keeping the one with highest peak intensity.
Using the example code in the issue, now only one circle is drawn per coin.

Checklist
./doc/examples(new features only)./benchmarks, if your changes aren't covered by anexisting benchmark
For reviewers
later.
__init__.py.doc/release/release_dev.rst.@meeseeksdev backport to v0.14.x