Canny Edge Detector Stages
Canny chains smoothing, gradients, thinning, and smart thresholding into thin clean edges. Weak pixels survive only beside strong ones.
Why Does This Exist?
Raw gradient maps are thick, broken, and threshold-fragile: one cutoff keeps noise and drops faint edges, another keeps faint edges and floods. Canny's 1986 pipeline fixes all three complaints in sequence. Gaussian smoothing hushes noise, Sobel gradients measure strength and direction, thinning narrows ridges to one pixel, and hysteresis thresholding links faint segments to strong ones instead of judging each pixel alone.
The one idea you need first
An edge is a place where brightness changes fast, and the gradient magnitude says how fast at each pixel. Strong gradients mark likely edges, but the ridge around a real edge is several pixels wide and speckled with noise responses. Everything Canny does after measuring gradients is cleanup: narrow each ridge to its peak, then decide which peaks are real. Gradients in the filtering context are covered in spatial filtering and edge detection.
Think of It Like This
Tracing a faint trail
Follow a hiking trail by painting only the clearest footprints, then filling gaps with fainter prints that continue the same line, while ignoring lone prints in the grass. Strong pixels are clear footprints, weak ones are faint but aligned, and isolated specks are animal tracks to discard. Hysteresis is that rule: faint evidence counts only when it extends something certain.
The analogy stops at direction: a hiker follows the trail's path, while Canny also checks that each pixel is the strongest across its gradient direction before any linking begins.
How It Actually Works
Four stages run in order. Smoothing blurs with a Gaussian at sigma you choose. Gradients come from Sobel and , giving magnitude and direction per pixel. Non-maximum suppression thins ridges: each pixel survives only if its magnitude beats both neighbours along the gradient direction, leaving one-pixel lines. Double thresholding then labels pixels above the high threshold as strong, below the low threshold as discarded, and between as weak. Hysteresis keeps weak pixels only when connected, directly or through other weak pixels, to a strong one.
Worked example
Thresholds low and high meet four pixels with magnitudes , , , and . The is strong and kept. The is weak but touches the strong pixel, so hysteresis keeps it as a continuing edge. The is also weak but isolated, so it is discarded despite beating . The falls below low and is discarded outright. Connectivity, not raw strength, decides the middle two.
Watch Out For
Thresholds tuned to one image
Thresholds that sing on one photo fail on the next because contrast and lighting shift gradient scales. The symptom is perfect edges on the demo image and emptiness or floods everywhere else. Set thresholds from the gradient histogram, such as a high percentile for the upper one, and re-tune per lighting condition rather than hard-coding.
Skipping sigma on noisy input
Running Canny with a tiny sigma on grainy images feeds speckle into every later stage, and no thresholding rescues the result. The symptom is hair-like false edges coating flat regions. Raise the Gaussian sigma until flat areas go quiet, accepting slightly rounded corners as the price.
The Quick Version
- Canny chains smooth, gradient, thin, threshold, and link stages.
- Non-maximum suppression narrows every ridge to a single pixel.
- Double thresholds label strong, weak, and discarded pixels.
- Hysteresis keeps weak pixels only when joined to strong chains.
- Tune sigma and thresholds per lighting; no universal pair exists.