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 = (∂I/∂x, ∂I/∂y)
Symbol guide
∇Igradient of the image - a 2-D vector showing the direction and strength of intensity change at each pixel
∂I/∂xpartial derivative in the horizontal direction - how much intensity changes as you move left/right
∂I/∂ypartial 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| = √(Gx² + Gy²)   θ = arctan(Gy / Gx)
Symbol guide
|∇I|gradient magnitude - edge strength; large values = sharp edge, small values = smooth region
Gxhorizontal gradient component (output of Sobel-x kernel) - positive = brighter on the right
Gyvertical 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:

Gx (vertical edges)
-10+1
-20+2
-10+1
Gy (horizontal edges)
-1-2-1
000
+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

Source (grayscale)
Gradient magnitude

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:

  1. Gaussian smoothing - suppress noise with a Gaussian blur
  2. Gradient computation - Sobel to get magnitude and direction
  3. Non-maximum suppression (NMS) - thin edges to 1-pixel width by keeping only local maxima along gradient direction
  4. Double thresholding + hysteresis - strong edges kept; weak edges kept only if connected to a strong edge
Hysteresis magic: With two thresholds (high & low), Canny avoids both missing faint edges connected to real edges AND keeping random noise as edges.

Canny Edge Detector - Interactive

Source
Canny edges

Detector Comparison

Sobel
Fast, gradient magnitude. Not thin edges. Good for real-time.
Prewitt
Like Sobel, slightly noisier (equal weights). Simpler.
Laplacian
Second derivative. Detects zero-crossings. Sensitive to noise.
LoG
Gaussian + Laplacian. Smoother. Marr-Hildreth detector.
Canny
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?