v9.1.1
Menu

Canny Edge Detection Pipeline

hardPROEdge DetectionEdgesCV: Feature Detection and Matching

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 G=Gx2+Gy2G = \sqrt{G_x^2 + G_y^2}, where GxG_x and GyG_y are the horizontal and vertical gradient components.

  1. Apply Gaussian blur to the input image to reduce noise.
  2. Compute the gradient using Sobel operators to obtain the magnitude and direction of the gradient.
  3. Perform non-maximum suppression to thin edges to 1 pixel width by comparing each pixel to its neighbors along the gradient direction.
  4. 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.
G=Gx2+Gy2G = \sqrt{G_x^2 + G_y^2}

Use a Gaussian with σ=1.4\sigma = 1.4. 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

Input
image = grayscale image with edges
low_threshold = 50
high_threshold = 150
Output
Binary edge map with thin, connected edges
Reasoning
  • Input: image is an H×WH \times W intensity grid; take a 7×97 \times 9 image whose every row is (0,0,0,0,128,255,255,255,255)(0, 0, 0, 0, 128, 255, 255, 255, 255), a vertical edge centred on column 4. low_threshold = 50 and high_threshold = 150 are gradient-magnitude cutoffs.
  • Blur (σ=1.4\sigma = 1.4) turns the step into a ramp: 0.8,5.0,21.8,63.1,127.6,192.1,233.3,250.0,254.20.8, 5.0, 21.8, 63.1, 127.6, 192.1, 233.3, 250.0, 254.2.
  • Gradient: rows are identical, so Gy=0G_y = 0 and the direction is 0∘0^\circ. Across a row G≈16.9,84.0,232.4,423.3,515.9,422.6,231.6,83.6,16.8G \approx 16.9, 84.0, 232.4, 423.3, 515.9, 422.6, 231.6, 83.6, 16.8.
  • Suppression: with a horizontal gradient, each pixel is compared with its left and right neighbours. Only column 4 survives (515.9≥423.3515.9 \ge 423.3 and ≥422.6\ge 422.6); columns 3 and 5 exceed 150 but are erased.
  • Hysteresis: 515.9≥150515.9 \ge 150, 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: 515.9515.9 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

Learn this in

More from CV: Feature Detection and Matching

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.