Canny Edge Detection: Extracting Structure
Edges represent the boundaries between distinct regions in an image. The Canny Edge Detector, developed by John F. Canny in 1986, remains the gold standard in computer vision for extracting these vital structural outlines while suppressing useless noise.
1. The Pre-requisite: Gaussian Blur
Edge detection algorithms look for sudden changes in pixel intensity. Because cameras and sensors naturally produce random "noise" (tiny, erratic pixel changes), we must first smooth the image. We apply a Gaussian filter to blur the image slightly, ensuring that we only detect true, structural edges rather than digital static.
2. Finding Gradients (The Math)
The algorithm then uses Sobel kernels to calculate the first derivative of the image in both horizontal ($G_x$) and vertical ($G_y$) directions. From these derivatives, we can calculate the overall Edge Gradient Magnitude ($G$) and its angle ($\theta$):
$$\theta = \arctan(G_y / G_x)$$
The direction angle is rounded to one of four angles (0, 45, 90, or 135 degrees) to represent vertical, horizontal, and two diagonal edges.
3. Non-Maximum Suppression
Gradient calculation often produces thick, blurry edges. Non-Maximum Suppression (NMS) scans the image and checks if the current pixel is a local maximum in the direction of the gradient. If it is, it is kept. If not, it is suppressed (set to zero). This process results in sharp, 1-pixel thin lines.
4. Hysteresis Thresholding
Finally, we must decide which of the remaining edges are real and which are just faint variations. We set two thresholds: minVal and maxVal.
- Strong Edges: Pixels with a gradient higher than
maxValare confirmed as edges. - Discarded Edges: Pixels lower than
minValare discarded completely. - Weak/Candidate Edges: Pixels between the thresholds are only kept if they physically touch a Strong Edge.
❓ Computer Vision FAQ
How do I choose the right minVal and maxVal for cv2.Canny?
Choosing thresholds is empirical and depends heavily on your image's lighting and contrast. A common heuristic is the 1:2 or 1:3 ratio. For example, if your minimum threshold is 100, set your maximum to 200 or 300.
You can also compute these dynamically based on the median pixel intensity of the image to make your pipeline more robust to lighting changes.
Why do my edges look broken or disconnected?
Broken edges usually occur when your minVal is too high, causing weak (but valid) connecting lines to be discarded during the hysteresis phase. Try lowering your minVal. Alternatively, your initial image might be too heavily blurred, destroying the edge gradients before they reach the Canny function.
Do I have to convert the image to grayscale first?
Yes. Edge detection relies on calculating intensity (brightness) gradients. Color images (RGB) have three separate intensity channels. While you can compute gradients per channel, standard Canny implementations (like OpenCV's `cv2.Canny`) expect a single-channel, 8-bit grayscale image to accurately measure intensity changes.