Skip to content

Make hough_circle_peaks respect min_xdistance, min_ydistance - #4350

Merged
jni merged 14 commits into
scikit-image:masterfrom
xiaoyu-wu:fix/hough_circle_peaks
Dec 21, 2019
Merged

jni merged 14 commits into
scikit-image:masterfrom
xiaoyu-wu:fix/hough_circle_peaks

Conversation

@xiaoyu-wu

@xiaoyu-wu xiaoyu-wu commented Dec 9, 2019 •

Copy link
Copy Markdown
Contributor

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.
hough_circle_peaks

Checklist

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.
  • Consider backporting the PR with @meeseeksdev backport to v0.14.x

@xiaoyu-wu xiaoyu-wu changed the title Make hough_circle_peaks respect min_xdistance, min_ydistance [WIP] Make hough_circle_peaks respect min_xdistance, min_ydistance Dec 9, 2019
@xiaoyu-wu xiaoyu-wu changed the title [WIP] Make hough_circle_peaks respect min_xdistance, min_ydistance Make hough_circle_peaks respect min_xdistance, min_ydistance Dec 10, 2019
@sciunto

sciunto commented Dec 10, 2019

Copy link
Copy Markdown
Member

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.

@xiaoyu-wu

Copy link
Copy Markdown
Contributor Author

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 normalized. If normalized is set False, then originally the returned tuple are in fact sorted with larger circles first. Enforcing a dimensionless peak height will break test_hough_circle_peaks_total_peak. Maybe I can add more to the docstring and be explicit about the preference toggled by normalized?

@sciunto

sciunto commented Dec 10, 2019

Copy link
Copy Markdown
Member

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 ?

Comment thread skimage/transform/hough_transform.py Outdated
cx = []
cy = []
r = []
for ac, x, y, ra in zip(accum_sorted, cx_sorted, cy_sorted, r_sorted):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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! :)

@lagru lagru Dec 11, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think cKDTree supports duplicate coordinates and it will return duplicates as well if queried.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the largest distance times sqrt(2)

or np.hypot(dx, dy)? =)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my latest change, I scaled in both x and y dimension so we don't get an unnecessarily long list of neighbors.

@emmanuelle

Copy link
Copy Markdown
Member

@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:

  • when computing the hough transform with hough_circle. Since the threshold of hough_circle_peaks is the same for all radii, you will have a different result whether you had normalized the transform or not.
  • and then you have the normalize argument for hough_circle_peaks, which allows you to give more or less importance to large circles when comparing circles with different radii.

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 hough_circle_peaks stage.

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!

@sciunto

sciunto commented Dec 12, 2019

Copy link
Copy Markdown
Member

@emmanuelle I agree, I just do not find it super-safe.

@emmanuelle

Copy link
Copy Markdown
Member

@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...

@pep8speaks

pep8speaks commented Dec 12, 2019 •

Copy link
Copy Markdown

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

@xiaoyu-wu

Copy link
Copy Markdown
Contributor Author

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.

@jni

jni commented Dec 13, 2019

Copy link
Copy Markdown
Member

Sometimes the builds hang. I've restarted it. Thank you!

@xiaoyu-wu
xiaoyu-wu requested a review from emmanuelle December 18, 2019 16:07
Comment thread skimage/transform/hough_transform.py Resolved
Comment thread skimage/transform/hough_transform.py Resolved

@emmanuelle emmanuelle left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just left two nitpicky comments which should be easy to address. 👍

@xiaoyu-wu

Copy link
Copy Markdown
Contributor Author

@jni Can you please restart CI?

@jni jni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Comment thread skimage/transform/hough_transform.py Outdated
y_close = abs(scaled_cy_sorted[ni] - scaled_cy_sorted[i]) <= 1
if x_close or y_close:
removed[ni] = True
i += 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 to min_xdistance and min_ydistance instead of 1. This removes some additional variables, and makes reasoning about the code more local.
  • not create lists, but rather, use removed at 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 += 1

This is exactly equivalent to:

for i in range(max_points):
    # bunch of code that does not modify i

... Or am I missing something?

@xiaoyu-wu xiaoyu-wu Dec 19, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏 !

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.)

@xiaoyu-wu

Copy link
Copy Markdown
Contributor Author

No problem. I can address your comments in this PR.

@jni
jni merged commit 08eb197 into scikit-image:master Dec 21, 2019
@jni

jni commented Dec 21, 2019

Copy link
Copy Markdown
Member

Thank you for your contribution and for your patience iterating with us, @xiaoyu-wu! 🎉

@xiaoyu-wu

Copy link
Copy Markdown
Contributor Author

@jni @emmanuelle @sciunto @lagru Thank you all for your feedback. I learned a lot in this PR.

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.

6 participants