Edge Detection: Canny Operator and 2D Methods

Derivative of Gaussian in 2D

In 2D, the derivative-of-Gaussian filter must specify direction. For the x-direction:

d/dx(g ∗ f) = (d/dx g) ∗ f

where g is a 2D Gaussian and d/dx is a small derivative operator (e.g., [-1, 0, 1] or Sobel). The derivative is applied to the smaller Gaussian kernel, not the full image, making it faster. The resulting smoothed-derivative operator is computed once and reused.

Effect of Sigma

The Gaussian’s sigma controls edge detection scale:

  • Small sigma: detects fine features and texture edges

  • Large sigma: detects only large-scale edges, suppressing fine detail

Canny Edge Detector

The Canny edge detector is a multi-step process:

  1. Smooth derivatives: filter with derivative of Gaussian; compute gradient magnitude and orientation

  2. Thresholding: remove pixels with low gradient magnitude

  3. Non-maximum suppression (thinning): reduce thick edge responses to single-pixel-wide contours

  4. Hysteresis linking: connect edge pixels using dual thresholds

Non-Maximum Suppression

A gradient above threshold may span multiple pixels perpendicular to the edge. Non-maximum suppression examines gradient magnitude along the gradient direction and retains only the peak value. Sub-pixel interpolation is used when the gradient direction falls between pixel positions, yielding a thin edge contour.

Hysteresis Thresholding

A single threshold creates a dilemma: too high misses valid edge segments, too low introduces noise. Canny’s dual-threshold solution:

  1. Apply a high threshold to find strong edge pixels

  2. Link strong pixels into strong edges

  3. Apply a low threshold to find weak but plausible edge pixels

  4. Extend strong edges through adjacent weak pixels

An edge is only detected if it contains at least some strong pixels. Weak pixels alone do not initiate edges — they only extend existing ones. This assumes every real edge has at least some strong-response pixels.

MATLAB: edge(img, 'canny') with optional hysteresis values and smoothing sigma.

Canny and Noise

The Canny operator is mostly robust to noise due to Gaussian smoothing, but sensitivity depends on the chosen sigma. Larger sigma provides more noise suppression at the cost of missing fine-scale edges.

Laplacian of Gaussian (LoG)

In 1D, the second derivative of a Gaussian yields zero crossings at edges. Extending to 2D requires choosing among ∂²f/∂x², ∂²f/∂y², and ∂²f/∂x∂y.

The Laplacian combines directional second derivatives:

∇²f = ∂²f/∂x² + ∂²f/∂y²

The Laplacian of Gaussian (LoG) produces a symmetric “Mexican hat” operator. Applying it to an image and finding zero crossings yields edges.

  • LoG edges tend to produce closed contours

  • Canny edges appear only where gradient magnitude is sufficient

  • Canny is more commonly used for general edge detection

MATLAB Edge Detection Summary

img_gray = rgb2gray(img);
img_smooth = imfilter(double(img_gray), fspecial('gaussian', [n n], sigma));

% Canny edges
edges_canny = edge(img_gray, 'canny');
edges_smooth = edge(img_smooth, 'canny');

% Laplacian of Gaussian edges
edges_log = edge(img_gray, 'log');

Smoothing before Canny removes fine detail edges. The edge function supports multiple methods ('sobel', 'canny', 'log', etc.) with configurable parameters via doc edge.