The Canny edge detector operates in six sequential stages:
- Gaussian smoothing — Convolve the image with a Gaussian kernel to suppress noise. The sigma parameter controls the scale of edges detected.
- Gradient magnitude — Apply Sobel operators to compute edge strength: E = |Gx| + |Gy| (fast L1 norm) or E = √(Gx² + Gy²) (accurate L2 norm).
- Gradient direction — Compute edge orientation: θ = arctan(Gy/Gx) at each pixel.
- Direction quantization — Snap continuous angles to four discrete directions: 0°, 45°, 90°, 135°. This defines the direction perpendicular to the edge.
- Non-maximum suppression — Compare each pixel's gradient magnitude to its two neighbors along the gradient direction. Only local maxima survive — all other pixels are set to zero. This thins thick edge responses to single-pixel-wide lines.
- Hysteresis thresholding — Apply two thresholds: T_high (strong edges, always kept) and T_low (weak edges). Starting from strong edge pixels, trace connected paths through weak edge pixels. Weak edges connected to strong edges are kept; isolated weak edges are discarded.