Repository navigation
fix: make canny use its sigma and threshold arguments - #239
Merged
Merged
Conversation
canny() ignored sigma, th0 and th1: the thresholds always came from the mean of the image, and the default branch for th0 could never run. sigma now smooths the image first (default None, no smoothing). th0 and th1 are passed to OpenCV and apply to its Sobel gradient magnitude. If neither is given, th1 is the percentile of that gradient (default 90) and th0 = k * th1 (default 0.5). A threshold with a non-default percentile is a ValueError. hist=True also returns a CannyHistogram of the gradient magnitude that records the thresholds and marks them when plotted. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| ErrorProne | 2 high |
🟢 Metrics 22 complexity · 2 duplication
Metric Results Complexity 22 Duplication 2
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Image.canny()ignored itssigma,th0andth1arguments. The thresholds always came from the mean of the image, the Gaussian gradients it computed were never used,sigmawas overwritten by a constant, and the default forth0could never run. The existing strictxfailtest recorded this.Changes
sigmasmooths the image before edge detection. The default is nowNone, which means no smoothing.th0andth1are passed to OpenCV. They apply to OpenCV's Sobel gradient magnitude|Gx| + |Gy|of theuint8image (a step of 255 gives 1020), and the docstring says so. If only one is given the other is set fromk.th1is thepercentile(default 90) of that gradient magnitude andth0 = k * th1(defaultk = 0.5). The gradient uses the same Sobel kernel and norm ascv2.Canny, so the thresholds match the quantity OpenCV compares.percentileraisesValueError, as dopercentileoutside (0, 100) andkoutside (0, 1].hist=Truealso returns aCannyHistogram, aHistogramof the gradient magnitude with the thresholds used as attributesth0andth1. Itsplot()marks them with vertical lines, to help choose thresholds.@overloadgives mypy the two return types... versionchanged:: 2.5.0in the docstring.Behaviour change
Default results change, because the automatic thresholds are different. Edge pixels with default arguments:
monalisa.png(473,900 pixels)church.png(1,089,280 pixels)I only counted pixels and did not compare the edge images by eye.
Tests
The strict
xfailtest is now an ordinary test. New tests cover thresholds on the Sobel scale, one threshold given,percentileandk, the percentile checked against an independent gradient calculation, theValueErrorcases, smoothing,hist=True, and the threshold lines in the plot.tests/test_image_spatial_extra.pyandtests/test_image_spatial.pypass, and mypy reports no errors. I have not run the full suite locally. CI will.This change was written with AI assistance (Claude Code) and I have reviewed the diff.
🤖 Generated with Claude Code