Edge Detection
Module 4 - Sobel, Prewitt, Laplacian, Canny
Why Edges?
Edges mark boundaries - where the intensity changes sharply. They are among the most information-rich parts of an image. Object boundaries, surface discontinuities, shadows - all appear as edges. Detecting them reduces an image to its structural skeleton.
Humans can recognize objects from edge drawings alone. This is why edge detection is a fundamental preprocessing step for many CV pipelines.
The Image Gradient
An edge is a location where intensity changes rapidly - a large gradient. The gradient is a vector pointing in the direction of steepest increase:
| ∇I | gradient of the image - a 2-D vector showing the direction and strength of intensity change at each pixel |
| ∂I/∂x | partial derivative in the horizontal direction - how much intensity changes as you move left/right |
| ∂I/∂y | partial derivative in the vertical direction - how much intensity changes as you move up/down |
| (…, …) | a 2-component vector: horizontal change paired with vertical change |
The magnitude and direction of an edge:
| |∇I| | gradient magnitude - edge strength; large values = sharp edge, small values = smooth region |
| Gx | horizontal gradient component (output of Sobel-x kernel) - positive = brighter on the right |
| Gy | vertical gradient component (output of Sobel-y kernel) - positive = brighter below |
| √(Gx² + Gy²) | Pythagorean theorem applied to the gradient vector - combines both directions into one strength number |
| θ (theta) | gradient direction - the angle of the edge in degrees, perpendicular to the actual edge line |
| arctan(Gy / Gx) | inverse tangent - converts the (Gx, Gy) ratio back to an angle in radians/degrees |
We approximate these partial derivatives using convolution kernels.
Sobel Operator
The Sobel operator uses two 3×3 kernels to approximate horizontal and vertical derivatives:
| -1 | 0 | +1 |
| -2 | 0 | +2 |
| -1 | 0 | +1 |
| -1 | -2 | -1 |
| 0 | 0 | 0 |
| +1 | +2 | +1 |
The center row/column gets weight 2 - this is implicit Gaussian smoothing, making Sobel more noise-resistant than Prewitt (which uses weights 1,1,1).
Sobel Edge Detection - Live
Canny Edge Detector
Canny (1986) is the gold-standard edge detector. It aims for: good detection (find all real edges), good localization (edges are thin and precise), minimal response (each edge causes only one response).
The Canny algorithm has 4 stages:
- Gaussian smoothing - suppress noise with a Gaussian blur
- Gradient computation - Sobel to get magnitude and direction
- Non-maximum suppression (NMS) - thin edges to 1-pixel width by keeping only local maxima along gradient direction
- Double thresholding + hysteresis - strong edges kept; weak edges kept only if connected to a strong edge
Canny Edge Detector - Interactive
Detector Comparison
Fast, gradient magnitude. Not thin edges. Good for real-time.
Like Sobel, slightly noisier (equal weights). Simpler.
Second derivative. Detects zero-crossings. Sensitive to noise.
Gaussian + Laplacian. Smoother. Marr-Hildreth detector.
Best overall. Thin edges, hysteresis, tunable. Slower.
Quiz
Check your understanding
1. What does Non-Maximum Suppression do in Canny edge detection?
2. If Gx = 3 and Gy = 4, what is the gradient magnitude?
3. Why is Gaussian smoothing done as a first step in Canny?