Canny Edge Detection

Machine Learning
Introduced by
John Canny
Year
1986
Type
Image gradient analysis
Family
Edge detection

What does it do?

The Canny edge detector, introduced by John Canny in 1986, is a multi-stage algorithm for locating edges in digital images, where edges are taken as points of significant intensity change.

It was derived from explicit detection and localization criteria for step edges, and it is optimal under those criteria; it remains widely used as a classical edge detector.

The intuition behind the method

Edges in images mark boundaries between objects and background or between different materials. The Canny algorithm detects edges by identifying pixels where image intensity changes sharply. It works in stages: first, it smooths the image to reduce noise; then, it computes the image gradient to find the direction and magnitude of intensity change; next, it thins edges to single-pixel-wide contours; finally, it uses a two-threshold approach (hysteresis) to distinguish true edges from noise-induced false positives.

History

John Canny published his edge detector in 1986, formulating edge detection as an optimisation problem with detection and localisation criteria, plus a third criterion ensuring a single response per edge. He showed that the optimal operator for step edges has a simple approximate implementation in which edges are marked at maxima of the gradient magnitude of a Gaussian-smoothed image. The optimality result holds for step edges under these three criteria. The paper also proposes extensions that use operators of several widths to cope with different signal-to-noise ratios, and a method of feature synthesis for combining responses across scales; these multi-scale extensions are not part of the common single-scale implementation. The widely used implementation adds non-maximum suppression and hysteresis thresholding to the gradient-maxima rule, which yields thinner and more continuous edge maps than gradient magnitude alone.

When to use it

Choose Canny when the task needs thin, well-localised edge maps from intensity images with reasonable contrast, for example as preprocessing for Hough Transform shape detection, contour analysis, or segmentation. It suits scenes whose boundaries are reasonably step-like and where noise can be controlled by smoothing. Do not use it when the image is heavily textured or noisy so that true edges are not distinct, when sub-pixel localisation is required, or when the boundary of interest is defined by texture or semantic content rather than intensity change; learned edge detectors are an alternative for complex scenes.

Limitations and considerations

  • Sensitive to threshold selection; poor choice of thresholds results in too many false positives or missed edges
  • The Gaussian sigma sets a trade-off: larger values suppress more noise but blur fine edges and small features
  • Assumes edges are step-like; performs poorly on textured boundaries or soft transitions, where gradient responses are weak or fragmented
  • Uses local gradients only, so it does not distinguish true object boundaries from internal texture edges

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Optimal under the detection and localisation criteria Canny set for step edges, which is the basis of its design rather than a general guarantee for all images
  • Non-maximum suppression produces single-pixel-wide edge contours from the gradient magnitude map
  • Hysteresis thresholding keeps weak responses only when they connect to strong ones, which suppresses isolated noise while preserving continuous contours
  • Built from a short sequence of standard operations (Gaussian smoothing, gradient computation, non-maximum suppression, double thresholding), so each stage can be inspected separately
  • Few and directly interpretable parameters: the Gaussian sigma and the two thresholds

Applications

  • Preprocessing for Hough Transform-based line and circle detection in engineering drawings
  • Contour extraction for shape analysis in object recognition and industrial inspection
  • Document image analysis: detecting text regions and table boundaries in scanned documents
  • Delineating boundaries in medical images as an early step before segmentation, not as a complete segmentation method

How it works

How it works

1

Apply Gaussian smoothing to reduce noise. Compute the image gradient in the x and y directions, for example with Sobel kernels or as derivatives of the Gaussian-smoothed image, then calculate the gradient magnitude and direction.

2

Apply non-maximum suppression (NMS): for each pixel, compare the gradient magnitude with its neighbours along the gradient direction and keep the pixel only if it is a local maximum, thinning the response to single-pixel-wide edges. This is a fixed, non-learned operation; nothing is fitted from data.

3

Apply double thresholding: classify pixels as strong edges, weak edges, or non-edges based on two threshold values; use edge tracking by hysteresis to connect weak edges adjacent to strong edges.

4

Inspect the resulting edge map overlaid on the original image: check whether true boundaries are continuous and whether spurious responses from texture or noise remain. Adjust the Gaussian sigma and the two thresholds accordingly and repeat. Where reference boundaries are available, compare the edge map with annotated boundaries using precision, recall, or boundary F-score, and vary the parameters to find the best trade-off. For colour images, the gradient is commonly computed on the luminance channel or per colour channel, keeping the strongest response at each pixel. Canny has no learned parameters, so this tuning, whether visual or quantitative, is the only calibration step.

Common pitfalls

  • Using fixed Canny thresholds across different image types; threshold values should be tuned per image or dataset
  • Applying Canny to JPEG-compressed images without preprocessing: block boundaries from compression produce gradient responses that NMS and hysteresis cannot separate from object edges
  • Discarding the gradient direction: NMS compares each pixel with neighbours along the gradient direction, so thinning by magnitude alone leaves thick or broken edges (hysteresis itself relies on pixel connectivity, not direction)
  • Over-smoothing with a large Gaussian sigma, which destroys fine structural details and thin features

Frequently asked questions

What is non-maximum suppression (NMS) and why is it necessary?

Non-maximum suppression thins the gradient magnitude map by suppressing pixels that are not local maxima along the gradient direction. This step is crucial because Gaussian derivatives produce thick gradient responses near edges. By keeping only the locally strongest responses (perpendicular to the edge direction), NMS produces single-pixel-wide edge contours. Without NMS, edge maps would be thick and diffuse.

How do the two thresholds in hysteresis thresholding work?

The Canny pipeline, as usually implemented, uses two thresholds applied to the gradient magnitude: a high threshold and a low threshold. Pixels above the high threshold are immediately classified as edges, and pixels below the low threshold are rejected. Pixels between the two thresholds are kept only if they are connected to a pixel above the high threshold. This hysteresis mechanism suppresses isolated noise while preserving thin, weakly defined edges that belong to continuous contours. Implementations often express the thresholds as fractions of the maximum gradient; the values 0.3 and 0.1 of the maximum gradient used here are illustrative examples only, and appropriate values depend on how the gradient is normalised and on the image.

Why use Gaussian blur before gradient computation?

Gaussian blur reduces noise before computing gradients. Since gradient operators are sensitive to high-frequency noise, smoothing first ensures that detected edges correspond to true intensity changes rather than noise. The smoothing scale (Gaussian sigma) is a tuning parameter: larger sigma removes more noise but also blurs edges; smaller sigma preserves edge sharpness but may be sensitive to noise.

How should threshold values be chosen?

Threshold selection depends on image contrast and noise level. Illustrative starting values expressed as fractions of the maximum gradient are a high threshold of about 0.2 to 0.3 and a low threshold of about 0.05 to 0.1. Because these ratios depend on how the gradient is normalised, they are starting points only, and thresholds should be tuned per image or dataset. For interactive applications, thresholds can be exposed as sliders. For batch processing, automated threshold selection based on image statistics, such as histogram-based methods like Otsu's method, can be more consistent than fixed values.

Related methods

Sources

  1. Canny, J. (1986). A computational approach to edge detection. IEEE Transactions on Pattern Analysis and Machine Intelligence, 8(6), 679–698.

    Open DOI reference
  2. Sobel, I., & Feldman, G. (1968). A 3x3 isotropic gradient operator for image processing. Pattern Recognition and Machine Intelligence, 271–272.

    Open publication reference