Canny Edge Detection Pipeline
Implement the complete Canny edge detection pipeline, a multi-stage process for detecting edges in images. The Canny edge detection algorithm is a widely used technique in Computer Vision for identifying and locating edges in images, which is crucial for various applications such as object recognition, image segmentation, and feature extraction.
The process involves several key steps, including Gaussian blur to reduce noise, gradient computation using Sobel operators to calculate the magnitude and direction of the gradient, non-maximum suppression to thin edges to 1 pixel width, and hysteresis thresholding to connect edges using two thresholds. The gradient magnitude is calculated as , where and are the horizontal and vertical gradient components.
- Apply Gaussian blur to the input image to reduce noise.
- Compute the gradient using Sobel operators to obtain the magnitude and direction of the gradient.
- Perform non-maximum suppression to thin edges to 1 pixel width by comparing each pixel to its neighbors along the gradient direction.
- Apply hysteresis thresholding to connect edges using two thresholds, where strong edges are retained and weak edges are only kept if connected to strong edges.
Use a Gaussian with . The output is an array the same shape as the image holding integer values (0 = no edge, 1 = edge), not booleans; the 1-pixel image border is always 0.
This technique is widely used in image processing and analysis applications.
Example
image = grayscale image with edges low_threshold = 50 high_threshold = 150
Binary edge map with thin, connected edges
- Input:
imageis an intensity grid; take a image whose every row is , a vertical edge centred on column 4.low_threshold= 50 andhigh_threshold= 150 are gradient-magnitude cutoffs. - Blur () turns the step into a ramp: .
- Gradient: rows are identical, so and the direction is . Across a row .
- Suppression: with a horizontal gradient, each pixel is compared with its left and right neighbours. Only column 4 survives ( and ); columns 3 and 5 exceed 150 but are erased.
- Hysteresis: , so column 4 is strong.
- Output: a Binary edge map with thin, connected edges: five 1s in column 4, rows 1–5, zeros elsewhere. With
high_threshold= 600 it is all zeros: clears the low cutoff but has no strong pixel to attach to.
Constraints
- image: Grayscale image (H, W)
- low_threshold: Lower threshold for hysteresis
- high_threshold: Upper threshold for hysteresis
- Return: Binary edge map