Skip to content

Refactor canny edge detector - #4342

Merged
grlee77 merged 37 commits into
scikit-image:mainfrom
rfezzani:Refactor_canny
Jul 13, 2021
Merged

grlee77 merged 37 commits into
scikit-image:mainfrom
rfezzani:Refactor_canny

Conversation

@rfezzani

@rfezzani rfezzani commented Dec 6, 2019 •

Copy link
Copy Markdown
Member

Description

In this PR, the skimage.feature._canny module is refactored:

  • low_threshold and high_threshold default values are now respectively 0.1 and 0.2 instead of None.
  • setting low_threshold and high_threshold to None is deprecated
  • the preserve_range argument is added to manage the issue use_quantiles description in canny is incorrect #4282: low_threshold and high_threshold are only divided by dtype_max if preserve_range is False.
  • a ValueError is raised if low_threshold > high_threshold
  • smooth_with_function_and_mask is deprecated
  • intermediary functions _preprocess, _get_local_maxima and _set_local_maxima are defined.

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

@pep8speaks

pep8speaks commented Dec 6, 2019 •

Copy link
Copy Markdown

Hello @rfezzani! 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 2021-07-08 18:44:25 UTC

Comment thread skimage/feature/_canny.py Outdated
raise ValueError("Quantile thresholds must not be < 0.0")

def fsmooth(x):
return img_as_float(gaussian(x, sigma, mode='constant'))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In the previous version, preserve_range was implicitly set to False as the default argument of gaussian

Comment thread skimage/feature/_canny.py
Comment on lines -198 to -208
pts_plus = (isobel >= 0) & (jsobel >= 0) & (abs_isobel >= abs_jsobel)
pts_minus = (isobel <= 0) & (jsobel <= 0) & (abs_isobel >= abs_jsobel)
pts = pts_plus | pts_minus

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These boolean expressions are simplified in _get_local_maxima

Comment thread skimage/feature/_canny.py
Comment on lines -205 to -221
c1 = magnitude[1:, :][pts[:-1, :]]
c2 = magnitude[1:, 1:][pts[:-1, :-1]]
m = magnitude[pts]
w = abs_jsobel[pts] / abs_isobel[pts]
c_plus = c2 * w + c1 * (1 - w) <= m
c1 = magnitude[:-1, :][pts[1:, :]]
c2 = magnitude[:-1, :-1][pts[1:, 1:]]
c_minus = c2 * w + c1 * (1 - w) <= m
local_maxima[pts] = c_plus & c_minus

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This interpolation step is performed in _set_local_maxima

@rfezzani rfezzani changed the title Refactor/Fix canny edge detector Fix use_quantile argument usage in canny edge detector Jan 11, 2020
@emmanuelle emmanuelle added this to the 0.17 milestone Jan 24, 2020
@rfezzani rfezzani added the 🩹 type: Bug fix Fixes unexpected or incorrect behavior label Jan 28, 2020
@rfezzani rfezzani added the 🔽 Deprecation Involves deprecation label Feb 6, 2020
@rfezzani rfezzani removed the 🔽 Deprecation Involves deprecation label Feb 6, 2020
@rfezzani rfezzani changed the title Fix use_quantile argument usage in canny edge detector Refactor canny edge detector Feb 7, 2020
@rfezzani rfezzani added 🔧 type: Maintenance Refactoring and maintenance of internals and removed 🩹 type: Bug fix Fixes unexpected or incorrect behavior labels Feb 7, 2020
tile up to size 2048, 2048 to get closer to 1 s runtime for canny
@grlee77

grlee77 commented Dec 29, 2020

Copy link
Copy Markdown
Contributor

Hi @rfezzani, I added the benchmark commit from #5162 here. Let me know if you would prefer I open a pull request vs. your repo instead of pushing here directly.

@rfezzani

Copy link
Copy Markdown
Member Author

Excellent! No worry @grlee77, thank you 😉

When mask=None, we can avoid binary morphology and just set the edge
values to zero instead.

We also do not need to Gaussian filter the mask
We really only need to know if sum > 0, not the actual value, so can use
numpy.unique on the masked values instead.
@grlee77

grlee77 commented Dec 29, 2020

Copy link
Copy Markdown
Contributor

I ran the benchmark on bbc4460, which is just this PR immediately after addition of the benchmark itself. The last two performance-related commits were ported from #5162. Unlike in the other PR, I ran these benchmarks with asv run mode instead of asv dev mode, so it should be a bit more accurate.

time_canny (ms) commit
1040 v0.18.1
994 bbc4460
770 4c60efe
752 2aea7db

Rather than hard-coding mode internally for the unmasked case, I added a keyword-only argument for it to the API as @rfezzani had mentioned wanting to do above. There is one test case (test_use_quantiles) that needs this to be specified as mode='nearest' ('reflect' and 'mirror' also pass, but mode='constant' does not).

@grlee77 grlee77 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The style changes here look good to me.

@rfezzani, please review the three commits I had added. For the benchmark, I think there was an error in setting up the test image previously because it contained only noise! I switched to using the astronaut image as a more realistic case.

I can confirm that the changes here give the same result as for the image comparison in the first commit of #5162. It may be worth changing the default mode='constant' to mode='nearest' or mode='reflect' to keep results the most similar to before in the unmasked case.

Comment thread skimage/feature/_canny.py Resolved
@rfezzani rfezzani modified the milestones: 0.18, 0.19 Jan 20, 2021
Base automatically changed from master to main February 18, 2021 18:23
@rfezzani
rfezzani requested a review from sciunto April 16, 2021 07:32
@rfezzani

Copy link
Copy Markdown
Member Author

@sciunto, do you still see requested changes? Thank you for your time ;)

grlee77 added 4 commits April 29, 2021 13:14
move comments within _get_local_maxima to its docstring

remove comments from canny that duplicate the text of the docstring
@grlee77
grlee77 removed the request for review from sciunto June 8, 2021 14:11
@grlee77

grlee77 commented Jun 8, 2021

Copy link
Copy Markdown
Contributor

@sciunto: I think this is ready, but it needs you to mark the requested changes as resolved to proceed. One outstanding issue to be left to a separate PR is changing the default mode to 'nearest'.

@grlee77
grlee77 merged commit 372b901 into scikit-image:main Jul 13, 2021
@grlee77

grlee77 commented Jul 13, 2021

Copy link
Copy Markdown
Contributor

Thanks @rfezzani!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔧 type: Maintenance Refactoring and maintenance of internals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants