Computer Vision Lab Algorithms The Canny edge detector
The Canny edge detector
A multi-stage algorithm that gives thin, connected edges.
This content was prepared automatically from documented sources and its human scientific review is not yet complete. If you find an error, let us know.
Mathematical formula
\text{edge} = \text{hysteresis}\big(\text{NMS}(\lvert\nabla (G_\sigma * I)\rvert),\, T_{low},\, T_{high}\big)Explanation
The stages: smoothing, computing the gradient with Sobel, non-maximum suppression along the gradient direction to thin the edges, then a double threshold with hysteresis tracking: a pixel above the upper threshold is a strong edge, and one between the two thresholds is accepted only if it connects to a strong edge.
Detailed explanation
Intuition
Canny designed his detector to satisfy three criteria he formulated mathematically: good detection (few missed and false edges), precise localisation, and a single response per edge. The resulting algorithm is a chain of four steps that is still the classical reference for edge detection.
How it works step by step
- Gaussian smoothing to reduce noise.
- Compute the gradient magnitude and direction (Sobel).
- Non-maximum suppression: a pixel is kept only if its magnitude is greater than its two neighbours in the gradient direction, so the edges become one pixel wide.
- Double threshold and hysteresis (hysteresis): anything above
T_his a strong edge, and anything betweenT_landT_his weak and accepted only if it connects to a strong edge.
Mathematical formulation
\text{edge}(p) = \begin{cases}1 & M(p)\ge T_h\\ 1 & T_l\le M(p)<T_h\ \wedge\ p \leadsto \text{strong}\\ 0 & \text{otherwise}\end{cases}A common ratio between the two thresholds T_h : T_l between 2:1 and 3:1, as Canny suggested. Hysteresis prevents an edge from breaking up when its strength drops slightly in part of it.
Computational complexity
O(HW) roughly; hysteresis is a traversal of the graph of connected pixels.
Advantages
- Thin, connected edges and good noise resistance.
- Three understandable parameters (
\sigma, T_l, T_h).
Limitations
- The thresholds need tuning for each type of image.
- It does not understand semantics: it detects the edges of shadows and textures just as it detects object boundaries.
- Corners and junctions sometimes break up.
When do you use it?
Whenever you need a clean edge map: line detection with the Hough transform, document scanning, industrial measurement, and as input to ControlNet in generation.
Code example (Python + OpenCV)
edges = cv2.Canny(cv2.GaussianBlur(gray, (5, 5), 1.4), 50, 150)Applications
Extracting boundaries, preparing images for line and shape detection.
Limitations
It depends on the choice of the two thresholds; it does not understand what the objects mean.
References
- J. Canny (1986).A Computational Approach to Edge Detection. IEEE TPAMI 8(6). doi:10.1109/TPAMI.1986.4767851
- OpenCV (2024).OpenCV documentation: Image Processing (imgproc module). docs.opencv.org. docs.opencv.org
