Skip to content

Canny

Fact Value
Also called Canny edge detector
Status Implemented and tested
Family Edge detection and boundary analysis
Engine Browser
Type Nonlinear, spatial, multi-stage edge detection
Output Mask
Operator shown as algorithm (exact)
Support radius global
Code web/src/core/filters/edges/hysteresis.ts, cannyFilter
Operator code web/src/core/filters/edges/hysteresis.ts, cannyStage
Channels Brightness only, RGB

Overview

The Canny edge detector is the classic way to find thin, connected edges. It blurs the image, computes the gradient, thins the gradient to lines one pixel wide, and keeps edges with two thresholds and hysteresis. Each of the five stages can be shown on its own with the Stage parameter, or side by side in the Stage viewer.

Six panels: noisy bright disks, the smoothed image, the gradient magnitude with wide rings, the thinned rings, the double threshold with strong pixels white and weak pixels gray, and the final edges as thin clean circles. Six panels: noisy bright disks, the smoothed image, the gradient magnitude with wide rings, the thinned rings, the double threshold with strong pixels white and weak pixels gray, and the final edges as thin clean circles.
The five stages of Canny on noisy disks: blur, gradient magnitude, thinning, double threshold and hysteresis.

How it works

  1. Blur the image with a Gaussian to remove noise.
  2. Compute the Sobel gradient: its magnitude and its direction.
  3. Keep the magnitude only where it peaks across the edge.
  4. Mark strong pixels above the high threshold and weak pixels above the low one.
  5. Keep strong pixels and the weak pixels joined to them.

Mathematical foundation

\[ E = \operatorname{hyst}_{T_L,\,T_H}\!\left(\operatorname{nms}\!\left(\left\| \nabla (G_{\sigma} * I) \right\|\right)\right) \]

The formula shows the default parameter values.

where

  • \(G_{\sigma}\): The Gaussian with standard deviation sigma
  • \(\nabla\): The gradient from the Sobel kernels
  • \(\operatorname{nms}\): Non-maximum suppression along the gradient
  • \(\operatorname{hyst}\): Hysteresis with the low and high thresholds
  • \(T_L, T_H\): The low and high thresholds

G_sigma * I is the image smoothed with a Gaussian. The gradient comes from the Sobel kernels, and its magnitude uses the L2 or L1 norm. The step nms keeps the magnitude only where it is at least as large as both neighbors along the gradient, rounded to four directions. The step hyst keeps the thinned values at or above T_H, plus those at or above T_L that are joined to them through eight neighbors. The result is 255 on the edges and 0 elsewhere.

Canny set out three goals for an edge detector: find real edges and few false ones, place them close to the true edge, and give one response per edge. He showed that the derivative of a Gaussian is a good, simple answer for step edges. The blur and the derivative are done here as a Gaussian followed by the Sobel kernels. Thinning gives the single response, and hysteresis keeps edges connected without keeping noise. The thinning step keeps both pixels of a flat-topped ridge, so a perfectly symmetric step gives an edge two pixels wide.

Kernel or operator

There is no single kernel. The Inspector lists the five stages with their formulas and the current parameters. The Gaussian and Sobel kernels are on their own pages.

Algorithm

  1. Extend the image with the border mode and blur it with a Gaussian.
  2. Compute the Sobel derivatives, the magnitude and the direction.
  3. Keep the magnitude only where it peaks across the edge, and cut the extension off.
  4. Mark strong pixels at or above the high threshold and weak pixels at or above the low one.
  5. Keep strong pixels and the weak pixels joined to them through eight neighbors.

Parameters

Parameter Default Values Meaning
Sigma sigma 1.5 0.5 to 5 px The size of the Gaussian blur in the first stage. The blur is cut at 4 sigma.
Low threshold low 40 1 to 1530 Thinned gradient values at or above this are weak edges, kept only when joined to a strong edge.
High threshold high 100 1 to 1530 Thinned gradient values at or above this are strong edges. If it is below the low threshold, the low threshold is used.
Magnitude norm norm L2 (square root of the sum of squares) L2 (square root of the sum of squares), L1 (sum of absolute values) How Gx and Gy are combined into the gradient magnitude.
Stage stage 5. Edges after hysteresis 1. Smoothed image, 2. Gradient magnitude, 3. After non-maximum suppression, 4. Double threshold, 5. Edges after hysteresis Which stage to show. The double threshold shows strong pixels as 255 and weak ones as 128.
Output mapping outputMapping Stretch to full range Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 How signed results, such as gradients, become 8-bit pixel values.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope Brightness only RGB, Brightness only Which part of the color image the filter changes.

Implementation

cannyStage in web/src/core/filters/edges/hysteresis.ts extends the plane by floor(4 sigma + 0.5) + 2 pixels with the border mode, then runs the blur, the Sobel gradient and the suppression of web/src/core/filters/edges/nms.ts. It cuts the extension off before the thresholds and hysteresis with 8 neighbors, so edges never join through it. The filter runs in the browser engine through the shared filter framework in web/src/core/filters/run.ts, which handles the channel scope, alpha and the output mapping. It is a global filter. A path can cross the whole image, so the filter always runs on the whole image in one tile. A selection is cut out of the result. By default it works on luma.

Worked example

x 5 6 7 8 9 10
Input 40 40 100 215 215 215
1. Smoothed 44 64.8 116.5 176.9 208 214.5
2. Magnitude 97.9 290 448.6 365.9 150.2 28
3. Thinned 0 0 448.6 0 0 0
5. Edges 0 0 255 0 0 0

A vertical edge with one in-between pixel at x = 7, and sigma 1, low threshold 40 and high threshold 100. Every row is the same, so the gradient points along x and each pixel is compared with its left and right neighbors. The magnitude peaks at x = 7, and only that pixel survives thinning. Its value, 448.6, is above 100, so it is a strong pixel and becomes an edge. The edge is one pixel wide.

Complexity

  • Time: \(O(N r)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel and r the radius of the Gaussian, which is separable.

Artifacts and limitations

Limitations

  • The thresholds depend on the image contrast, so they often need tuning.
  • Corners and junctions are often rounded or broken.
  • A perfectly symmetric step gives an edge two pixels wide, because both pixels of the flat ridge survive suppression.

Artifacts

  • Small gaps at junctions and on sharp corners.
  • Short spurs where noise touches an edge.

Tests

The reference runs scipy.ndimage.gaussian_filter and scipy.ndimage.sobel on the extended plane, the same four-sector rule as Sobel with non-maximum suppression, and scipy.ndimage.label for hysteresis. Before the cases are written, the script checks that a step edge is marked on the two columns next to the step and nowhere else. The test covers every stage, both norms and all five border modes.

References

  • Canny, J. A computational approach to edge detection. IEEE Transactions on Pattern Analysis and Machine Intelligence PAMI-8(6), 679 to 698, 1986. DOI 10.1109/TPAMI.1986.4767851.
  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 10.2, the Canny edge detector.