Edge Detection: Gradients and Derivatives¶
Edges in Images¶
Edges convey the essential structure of an image. Even heavily reduced line drawings (edges only) remain recognizable because edges encode the most important visual information.
Types of Edge Boundaries¶
Depth discontinuity: occlusion boundary between foreground and background
Shadow edge: discontinuity in illumination
Surface normal discontinuity: change in surface orientation (e.g., ridge of an object)
Reflectance discontinuity: change in surface color/texture with same geometry and illumination
A reflectance change (e.g., a stripe on a sign) is not a shape or illumination boundary — it arises from the surface’s reflectance function.
The goal of edge detection is to convert an image f(x, y) into a reduced set of edge pixels or curves that encode the significant changes.
Edge Detection via Derivatives¶
An image viewed as a height map (intensity = height) has edges at steep cliffs. The first derivative of a 1D intensity profile has extrema at edge locations — a peak in derivative magnitude corresponds to the steepest intensity change.
Approach: filter the image with a derivative operator, then threshold the response to select edge pixels.
Image Gradient¶
For an image f(x, y), the gradient is the vector of partial derivatives:
Direction (orientation of most rapid intensity increase):
Magnitude (rate of change in that direction):
When gradient magnitude is zero, the function is either locally constant, at a local maximum, or at a local minimum.
Finite Differences¶
Continuous derivatives are approximated by discrete finite differences:
The centered difference averages left and right derivatives:
Gradient images are displayed with gray = 0, white = positive, black = negative.
Sobel Operator¶
The Sobel operator uses a 3×3 kernel incorporating neighboring rows for robustness:
Sx = [[-1, 0, 1], Sy = [[ 1, 2, 1],
[-2, 0, 2], [ 0, 0, 0],
[-1, 0, 1]] / 8 [-1, -2, -1]] / 8
The normalization factor is 8 (sum of positive coefficients). MATLAB’s imgradientxy
applies Sobel by default but does not divide by 8 — all gradient values are scaled
by a factor of 8.
Gradient magnitude: |∇f| = √(gx² + gy²), where gx = Sx ∗ f, gy = Sy ∗ f.
Other well-known operators include Prewitt and Roberts. MATLAB’s
fspecial('sobel') returns the Sobel kernel.
Correlation vs. convolution both work for computing gradients as long as sign
conventions are tracked consistently. MATLAB’s imfilter uses correlation by default.
Gradient Direction¶
imgradientxy returns x and y gradient matrices (Sobel default). imgradient
returns magnitude and direction (degrees, −180 to +180, counterclockwise from
positive x-axis).
Sobel response range: each component spans [−4, 4]. To normalize to [0, 1]: add 4, divide by 8. Combined magnitude spans [0, 4√2].
A select_gdir(gmag, gdir, min_mag, angle_low, angle_high) function selects
pixels by gradient direction:
result = (gmag >= min_mag) & (gdir >= angle_low) & (gdir <= angle_high)
Filters out low-magnitude noisy pixels and retains only those with direction in the specified angular range. Note: 180° boundaries require special handling since 180° equals −180°.
Dealing with Noise¶
Noise causes spurious derivative responses. Solution: smooth with a Gaussian h first, then differentiate.
By the associative property and linearity of differentiation:
The derivative of the Gaussian can be precomputed and applied as a single filter. This is both mathematically equivalent and computationally cheaper (differentiating a small kernel vs. a full image).
Second Derivative and Zero Crossings¶
The second derivative of the smoothed signal produces zero crossings at edge locations. A strong-slope zero crossing reliably indicates an edge — no need to search for general maxima.
First derivative of Gaussian: edges are peaks
Second derivative of Gaussian (“inverted Mexican hat”): edges are zero crossings with strong nearby slope
The key properties exploited are the associative property of convolution and the linearity of differentiation.