← Back

Canny Edge Detection

Multi-stage filter: Gaussian blur → gradient magnitude → non-maximum suppression → hysteresis thresholding. The standard preprocessing step before contour tracing.

How it works

Canny edge detection extracts sharp boundaries from a greyscale image in four stages. Each stage refines the result of the previous one, trading false positives against missed edges using two tunable thresholds.

1. Blurremove noise
→
2. GradientSobel Gx, Gy
→
3. NMSthin to 1 px
→
4. Hysteresislink weak → strong
function canny(image, σ, lowT, highT): blurred = gaussianBlur(image, σ) [Gx, Gy] = sobelGradient(blurred) mag = magnitude(Gx, Gy) angle = atan2(Gy, Gx) // Non-maximum suppression: keep only local maxima along gradient thin = suppress(mag, angle) // Hysteresis: strong pixels (> highT) kept; weak (lowT–highT) kept // only if connected to a strong pixel return hysteresis(thin, lowT, highT)
Non-maximum suppression. For each pixel, the gradient direction is quantised to one of four angles (0°, 45°, 90°, 135°). The pixel is kept only if its magnitude is greater than both of its neighbours in that direction — this thins wide gradient ridges to single-pixel-wide edges.

Stage 1 — Gaussian blur

A Gaussian kernel smooths the image before gradient computation, suppressing high-frequency noise that would otherwise produce spurious edges. Drag the σ slider to see how blur radius trades noise suppression against edge sharpness.

σ = 1.4

Left: noisy input. Right: after Gaussian blur.

Stage 2 — Gradient magnitude & direction

The Sobel operator convolves the blurred image with two 3×3 kernels — one for horizontal (Gx) and one for vertical (Gy) change. The magnitude shows edge strength; the angle shows the normal to each edge.

Left: gradient magnitude. Right: gradient direction (hue = angle).

Full pipeline

Each stage feeds into the next. Adjust σ to control blur strength before gradient computation; tune the two hysteresis thresholds to filter weak edges — pixels above high are always kept; pixels between low and high are kept only when connected to a strong edge.

Input
Gaussian blur
σ = 1.4
Canny edges
20
60