Skip to content

fix: make canny use its sigma and threshold arguments - #239

Merged
petercorke merged 3 commits into
mainfrom
bug/canny-args
Nov 22, 2026
Merged

petercorke merged 3 commits into
mainfrom
bug/canny-args

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Image.canny() ignored its sigma, th0 and th1 arguments. The thresholds always came from the mean of the image, the Gaussian gradients it computed were never used, sigma was overwritten by a constant, and the default for th0 could never run. The existing strict xfail test recorded this.

Changes

  • sigma smooths the image before edge detection. The default is now None, which means no smoothing.
  • th0 and th1 are passed to OpenCV. They apply to OpenCV's Sobel gradient magnitude |Gx| + |Gy| of the uint8 image (a step of 255 gives 1020), and the docstring says so. If only one is given the other is set from k.
  • If neither is given, th1 is the percentile (default 90) of that gradient magnitude and th0 = k * th1 (default k = 0.5). The gradient uses the same Sobel kernel and norm as cv2.Canny, so the thresholds match the quantity OpenCV compares.
  • A threshold together with a non-default percentile raises ValueError, as do percentile outside (0, 100) and k outside (0, 1].
  • hist=True also returns a CannyHistogram, a Histogram of the gradient magnitude with the thresholds used as attributes th0 and th1. Its plot() marks them with vertical lines, to help choose thresholds. @overload gives mypy the two return types.
  • .. versionchanged:: 2.5.0 in the docstring.

Behaviour change

Default results change, because the automatic thresholds are different. Edge pixels with default arguments:

Image Before After
monalisa.png (473,900 pixels) 164,510 78,104
church.png (1,089,280 pixels) 122,332 89,948

I only counted pixels and did not compare the edge images by eye.

Tests

The strict xfail test is now an ordinary test. New tests cover thresholds on the Sobel scale, one threshold given, percentile and k, the percentile checked against an independent gradient calculation, the ValueError cases, smoothing, hist=True, and the threshold lines in the plot. tests/test_image_spatial_extra.py and tests/test_image_spatial.py pass, 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

petercorke and others added 2 commits November 23, 2026 06:58
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>
@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 2 high

Alerts:
⚠ 2 issues (≤ 0 issues of at least minor severity)

Results:
2 new issues

Category Results
ErrorProne 2 high

View in Codacy

🟢 Metrics 22 complexity · 2 duplication

Metric Results
Complexity 22
Duplication 2

View in Codacy

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.

@petercorke
petercorke merged commit d5ad31a into main Nov 22, 2026
33 of 34 checks passed
@petercorke
petercorke deleted the bug/canny-args branch November 22, 2026 21:56
@github-actions github-actions Bot mentioned this pull request Nov 22, 2026
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.

1 participant