Canny Edge Detection
2025-08-08
Canny Edge Detection is one of the most popular and effective edge detection algorithms used in computer vision.
Introduction
Canny Edge Detection, developed by John F. Canny in 1986, is a multi-stage algorithm designed to detect a wide range of edges in images robustly and accurately.
Its goals were to:
- Detect true edges (good detection)
- Locate edges precisely (good localization)
- Avoid multiple responses to a single edge (minimal response)
Step-by-Step Process of the Canny Algorithm
1. Noise Reduction with Gaussian Filter
- Why? Edges are susceptible to noise, which can cause false detections.
- How? Smooth the image by convolving it with a Gaussian kernel:
G(x,y)=2πσ21e−2σ2x2+y2
- σ controls the amount of smoothing (larger σ means more blur).
- The smoothed image Is is:
Is=G∗I
where ∗ is convolution.
2. Compute Intensity Gradients (Using Sobel Filters)
- Calculate gradients in the horizontal and vertical directions:
Gx=Is∗Kx,Gy=Is∗Ky
where Kx,Ky are Sobel kernels.
- Compute gradient magnitude G and gradient direction θ:
G=Gx2+Gy2
θ=arctan(GxGy)
- The magnitude G indicates edge strength, and θ shows edge orientation.
3. Non-Maximum Suppression (NMS)
Goal: Thin out edges to 1-pixel wide by suppressing non-maximum pixels along the gradient direction.
For each pixel:
- Compare gradient magnitude G(x,y) with neighbors along the gradient direction.
- If G(x,y) is not greater than neighbors, suppress it (set to zero).
This step preserves only the local maxima (potential edge pixels).
4. Double Thresholding
- Use two thresholds: high and low.
- Categorize pixels:
Category | Condition | Interpretation |
---|---|---|
Strong edge | G(x,y)≥Thigh | Definitely edge |
Weak edge | Tlow≤G(x,y)<Thigh | Potential edge, needs validation |
Non-edge | G(x,y)<Tlow | Suppress |
5. Edge Tracking by Hysteresis
- Final edges include all strong edges.
- Also include weak edges connected to strong edges (connected by 8-neighborhood).
- Discard weak edges not connected to strong edges (likely noise).
This step helps reduce false edges while preserving meaningful contours.
Summary Diagram of the Pipeline
- Smooth image with Gaussian
- Compute gradient magnitude and direction
- Apply Non-Maximum Suppression
- Double Thresholding
- Edge Tracking by Hysteresis
Important Parameters and Their Effects
Parameter | Effect |
---|---|
Gaussian σ | Controls smoothing; higher means smoother image, fewer edges detected, but less noise |
High threshold Thigh | Controls strong edge sensitivity; higher means fewer strong edges |
Low threshold Tlow | Controls inclusion of weak edges; lower means more edges included (including noise) |
- Typically, Tlow is about half of Thigh.
Practical Usage in OpenCV
edges = cv2.Canny(image, threshold1=low_threshold, threshold2=high_threshold)
threshold1
= low thresholdthreshold2
= high threshold- Input image should be grayscale
Why Is Canny Edge Detection Effective?
- Combines noise reduction, precise localization, and robust edge selection.
- Avoids detecting false edges caused by noise.
- Produces thin edges, suitable for downstream tasks like shape detection.
Visualization Example
- After applying Canny, edges appear as thin white lines on a black background.
- Changing thresholds changes edge density and continuity.
Would you like me to provide code examples and visualizations for Canny edge detection? Or maybe a comparison with simpler edge detectors like Sobel?