Edge Detection
An edge is a place where image intensity changes quickly, such as an object boundary, a shadow border, or a change in surface color. Edge detection finds these places by estimating the image gradient with derivative filters and keeping the pixels where its magnitude is large.
A digital image has no true derivative, so a finite difference replaces it. The forward difference in is
which is a cross-correlation with the kernel . The central-difference kernels and , applied by convolution, give and , each about twice the derivative. Here is the column index and the row index, which grows downward. responds to vertical edges and to horizontal edges.The gradient magnitude and gradient orientation are
The gradient points across the edge toward the brighter side, so is perpendicular to the edge itself. Thresholding gives a binary edge image.Finite differences amplify noise, so the image is smoothed with a Gaussian first. Convolution is associative, so . The single kernel is the derivative of Gaussian filter, and it smooths and differentiates in one convolution. The Sobel kernel is a version of the same idea:
The column smooths along the edge, and the row differentiates across it. The Laplacian kernel approximates . It responds to change in every direction and changes sign at an edge. Every derivative kernel sums to , so flat regions give zero response.
Sign of the Difference Kernel
The lecture slide writes the -derivative kernel as and the -kernel as “or” . Project 2 defines and and applies them by convolution. Once the flip is accounted for, both estimate “next pixel minus previous pixel”. The sign of the output depends on correlation versus convolution and on whether points up or down. The gradient magnitude does not depend on these sign choices.
Two Meanings of DoG
In edge detection, DoG means derivative of Gaussian, the kernel . In vision science and texture analysis, DoG also means difference of Gaussians, , a center-surround filter covered in 9.2. Filter Banks. The two kernels have different shapes and different purposes.
Intuition
An object boundary in a photo usually shows up as a jump in brightness. A derivative measures how fast a function changes, so large derivatives mark candidate edges. In 1D the picture is a step. The derivative is zero on both flat sides and spikes at the jump. In 2D the gradient collects the two partial derivatives into a vector. Its length says how strong the edge is, and its direction says which way the brightness increases.
The difficulty is noise. Every pixel carries some random error, and the difference of two noisy pixels is noisier than either one. Smoothing first suppresses the noise, but it also spreads the edge over more pixels. The Gaussian’s sets this balance. The diagram shows a noisy 1D step, its raw derivative, and its derivative of Gaussian response.
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\definecolor{sig}{HTML}{2F80ED}
\definecolor{bad}{HTML}{D9483B}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[x=1.4cm,y=1cm,>=latex,font=\small]
\draw[->,mid] (-3,0) -- (3.2,0) node[right] {$x$};
\draw[thick,sig,domain=-3:3,samples=500] plot (\x,{1.6*(0.5+atan(4*\x)/180) + 0.07*sin(30*\x r) + 0.05*sin(47*\x r + 1 r)});
\node[sig,left] at (-3,0.8) {$f$};
\node[right] at (3.2,1.3) {noisy step edge};
\begin{scope}[yshift=-2.6cm]
\draw[->,mid] (-3,0) -- (3.2,0) node[right] {$x$};
\draw[thick,bad,domain=-3:3,samples=700] plot (\x,{0.25*(6.4/(pi*(1+16*\x*\x)) + 2.1*cos(30*\x r) + 2.35*cos(47*\x r + 1 r))});
\node[bad,left] at (-3,0) {$\dfrac{\partial f}{\partial x}$};
\node[right] at (3.2,0.6) {noise dominates};
\end{scope}
\begin{scope}[yshift=-5.1cm]
\draw[->,mid] (-3,0) -- (3.2,0) node[right] {$x$};
\draw[thick,sig,domain=-3:3,samples=200] plot (\x,{0.5*4.8/(pi*(1+9*\x*\x))});
\node[sig,left] at (-3,0.4) {$f * \dfrac{\partial G}{\partial x}$};
\node[right] at (3.2,0.6) {one peak at the edge};
\draw[dashed,mid] (0,-0.15) -- (0,1.0) node[right] {edge};
\end{scope}
\end{tikzpicture}
\end{document}Properties
(Why Noise Wins) A derivative filter multiplies each frequency component of the image by a gain that grows with frequency.
Differentiating gives , so the amplitude is multiplied by . The discrete kernel has gain at radians per pixel. A slow change with a period of about pixels () gets gain . Patterns with periods of to pixels get gains between and . Independent pixel noise has equal energy at all frequencies, while the brightness profile of an edge has most of its energy at low frequencies. The derivative therefore raises the noise relative to the edge. A Gaussian blur multiplies frequency by , which removes the high band before the derivative can amplify it. One quirk of the central difference is a gain of at , so it ignores a pattern that alternates every pixel.
(One Kernel or Two) Smoothing then differencing gives the same result as one convolution with , except near the border.
Associativity guarantees the equality. In code, build the kernel with convolve2d(G, D_x) in full mode, so a Gaussian and a difference give a kernel. Shown as an image, the -derivative of Gaussian is a bright lobe beside a dark lobe. The -version is the same pattern rotated by . The two routes differ only near the image border, where the padding enters at different stages. The single kernel is cheaper when many images are filtered with the same .
(Choosing ) A larger removes more noise but widens each edge response and merges nearby edges.
The derivative of Gaussian response to an ideal step is a Gaussian bump of standard deviation , centered on the step. With small , texture and noise produce many weak responses. With large the response is clean but wide. Two steps in the same direction closer than produce a single bump. The reason is that a sum of two equal Gaussians has one peak when their centers are at most apart. Fine detail such as hair or text needs a small , and object outlines in a noisy photo need a larger one. Edges therefore have a scale, the same issue that scale-invariant detection handles for feature points.
(Orientation in Image Coordinates) Row indices grow downward, so from atan2 is mirrored relative to math axes with up.
A gradient pointing down and to the right has when is the row index. With pointing up, the same vector has . Either convention works if it is used consistently. Use atan2 and not of the ratio, because the ratio loses the quadrant and fails when . To visualize orientation, map to hue and to brightness. The two sides of a thin dark line then get hues apart, because their gradients point in opposite directions.
(Threshold Trade-off) No single threshold on keeps every true edge and rejects every noise pixel.
A low threshold admits texture and noise. A high threshold breaks weak edges, such as a soft shadow, into fragments. Thresholded edges also come out several pixels thick, because the magnitude stays high across the whole width of a smoothed transition. The right threshold depends on contrast. Doubling the image contrast doubles every gradient, so a threshold tuned on one photo can fail on a darker one. Choosing it is a judgment made by inspecting the edge image.
(Sobel and Prewitt) The Sobel kernel is a derivative of a tiny binomial blur.
The weights are the 3-tap binomial approximation of a Gaussian. The Prewitt kernel, with rows repeated three times, uses the box instead. Both are separable into a 3-tap smoothing pass and a 3-tap difference pass. Their smoothing spans only pixels, so they suppress less noise than a derivative of Gaussian with of or pixels. Both ignore a constant added to the image, because they sum to . Both scale with contrast, so they are not invariant to a contrast stretch such as histogram equalization.
(Laplacian Zero Crossings) The Laplacian marks an edge by a sign change and amplifies noise even more than the gradient does.
Across a step from to , the Laplacian kernel gives on the dark side and on the bright side. The edge lies at the zero crossing between them. One kernel covers every orientation, so no second directional output is needed. A second derivative multiplies frequency by , so it boosts high-frequency noise more than a first difference. In practice the image is smoothed with a Gaussian first, which gives the Laplacian of Gaussian. The same center-surround shape appears in the unsharp mask.
Examples
(Easy) Gradient of a Diagonal Edge
A patch sits on a diagonal edge between a dark wall and a bright window:
Rows are indexed top to bottom and columns left to right.Convolve with and in valid mode. Find the gradient magnitude and orientation, and say which way the gradient points. Convolution flips , so the response is the right column minus the left column, with weights from top to bottom. That gives . The same computation down the rows gives , the bottom row minus the top row. The magnitude is . The orientation is with pointing down, so the gradient points toward the bottom-right corner, where the patch is brightest. The edge itself runs from bottom-left to top-right, perpendicular to the gradient. Cross-correlating with instead would give .
(Medium) A Hot Pixel Near a Real Edge
A row from a photo has a sensor glitch at index and a real edge between indices and :
An edge pixel is any position where the absolute response exceeds .Compare the central difference with the derivative of the blur . Write the single combined kernel. The central difference in valid mode, at , is . The glitch produces responses of at and , half as strong as the real edge. The threshold marks four pixels, and two of them are false.
Blurring first gives at . The glitch is spread over three pixels, and its peak above the background drops from to . The step keeps its full height of . The central difference of the blurred row, at , is . The glitch now gives at most and the edge gives , so only and pass the threshold. The combined kernel is . Convolving with it once gives the same eight values.
(Hard) When Two Edges Become One
A picture frame on a wall gives a row with two steps of the same sign: wall up to pixel , frame for pixels to , picture from pixel . The steps are pixels apart. The row is filtered with a derivative of Gaussian (a truncated Gaussian at half-width , convolved with ).
How many peaks does the response have for and for ? Explain with the rule. For the response has two peaks of at pixels and , with a dip to at pixel . The two steps are resolved. For the response has one peak of at pixel , so the frame's two edges merge into one. Each step contributes a Gaussian bump of width , and two equal bumps merge when their centers are at most apart. Here the separation is , which exceeds but not . A detector that uses to suppress noise reports one edge where the frame has two.
See Also
- 2.1.1. Linear Filters, for convolution, correlation and the sum rule.
- 4.1.1. Harris Corner Detection, which builds a corner score from the same image gradients.
- 4.3.2. SIFT, which describes features with histograms of gradient orientations.