Professor William Hoff
Dept of Electrical Engineering &Computer Science http://inside.mines.edu/~whoff/
Colorado School of Mines Department of Electrical Engineering and Computer Science
• A place in the image where intensities are changing rapidly
• Exactly what constitutes an edge can be domain-dependent
3
Edge Operator
• A spatial filter which enhances discontinuities
• Example: 1 st derivative
(
1, )
( , ) -1 +1
x
• Edge operators enhance high spatial frequencies
– Recall equivalent filter in the frequency domain d n f dx n
( x )
2
ju
n
F ( u ) imag
2
ju
For first derivative (n=1), this acts like a high pass filter, attenuating frequencies near 0 u
4
• Edge operators also enhance noise
• Example – first and second derivatives of a noisy ramp edge
– Even with very low noise, 1 st derivative is useless at this noise level
– Second derivative is useless for even less noise
Original image and cross sectional profile
First derivative
Second derivative
5
• Practical edge detectors - first do image smoothing for noise reduction
• Example – Sobel filter
•
Effectively does a smoothing followed by a differencing
-1 0 1
1
-2 0 2 -1 0 +1
2 differencing
-1 0 1 1 smoothing
• All edge detectors do
– Smoothing for noise reduction
– Detection of candidate edge points
– Edge localization – keeping only the points that are closest to the true edge
6
Original image “pout.tif” Gradient magnitude, using Sobel masks
[fx,fy] = gradient(I); fmag = (fx.^2 + fy.^2) .^ .5; imtool(fmag,[]);
f
f
x
2
f
y
2
1
/ 2
7
• We can threshold edge magnitudes, to produce binary image
Low threshold High threshold Non-maxima
• Problem – only want one response to an edge suppression
• Solution – We take the point that is the local maximum of the gradient magnitude (in the direction of the gradient)
8
• Take only the local maximum in the direction of the gradient
• Matlab’s edge function does this
Direction of gradient
90 100 90 80 70
80 90 100 90 80 70
80 90 100 90 80 70
70 80
70
90 100 90
80
80
90 100 90 ang = atan2(fy,fx); ang2 = floor(8*(ang+pi)/(2*pi)); imtool(ang2,[]);
These points are local maxima in the direction of the gradient
This image is a good example: circlesBrightDark.png
E = edge(uint8(I),'sobel');
tan
1
f y f x
9
• Intensity changes occur at different scales in an image – we need operators of different sizes to detect them
10
• The space of images created by applying a series of operators of different scales
• Gaussians of different sizes can be used to filter the image at different scales
G
( x , y )
1
2
2 e
( x
2 y
2
) /( 2
2
)
0.08
0.07
0.06
Increasing
0.05
0.04
0.03
0.02
0.01
0
0 10 20 30 40 50 60 70 80 90 100
• Convolving with a Gaussian blurs the image, wiping out all structure much smaller than σ
11
• The Gaussian is the only operator that is localized in both the spatial and frequency domains g(x,y) – spatial domain
0.04
0.035
0.03
0.025
0.02
0.015
0.01
0.005
0
0 10 20 30 40 50 60 70 80 90 100
1
2
2 e
( x
2 y
2
)/2
2
G(u,v) – also a Gaussian
0.8
0.6
0.4
0.2
1.4
1.2
1
0
0 10 20 30 40 50 60 70 80 90 100
( , )
e
2
2
( u
2 v
2
)
12
• Also called “Marr-Hildreth” operator
2 g
2
x
2
2
y
2
1
2
2 e
( x
2 y
2
) /( 2
2
)
• Convolve with a Gaussian, then take the
Laplacian; or do the equivalent operation
2
g
I
I
13
• The operator’s scale is determined by σ
2 g ( x , y )
1
4
1
r
2
2
2
e
r
2
/( 2
2
)
• where r 2 = x 2 + y 2 (it is circularly symmetric) y
Negative inside this circle r
Positive outside circle; going to zero at infinity x
Zero at radius r
2
14
15
• Zero crossings in the convolved result indicate presence of an edge
• Peaks in the first derivative are equivalent to zeros in the second derivative
• Magnitude of the edge is proportional to the slope at the zero crossing
Example function – a smoothed step edge
First derivative
Second derivative peak
Zero crossing
16
17
• LoG can be approximated by a difference of Gaussians
2 g (
)
g (
1
)
g (
2
),
2
/
1
1 .
6
• There is evidence that the human visual system does edge detection similar to LoG
– Excitatory center, inhibitory surround
– A set of spatial frequency tuned channels
+
-
18
• If a 2D convolution kernel can be separated into two 1D kernels, the operation is much faster h ( x , y )
f h ( x ,
( x , y ) y )
h
1
( x ) h
2
( y ) h
2
( y )
h
1
( x )
f ( x , y )
• LoG is not separable
2 g ( x , y )
1
4
1
r 2
2
2
e
r
2
/( 2
2
)
• DoG is separable
2 g
g (
1
)
g (
2
),
2
/
1
1.6
1
2
2 e
( x
2 y
2
)/2
2
1
2
e
x
2
( )/2
2
1
2
e
y
2
( )/2
2
Colorado School of Mines Department of Electrical Engineering and Computer Science
19
• Zero crossings form closed contours (they are a “level set”, separating positive from negative regions)
• Zero crossings are separated on the average by a distance of 2√2 σ
• As σ increases, zero crossings are never created; they only merge or eliminate in pairs
σ
Cross section of an image showing locations of zero crossings x
20
• Would need different scales to:
– Obtain closed contours around dark spots
– Detect vertical bright structures
• Watch to see that zero crossings are only destroyed as scale increase
Image “bag.png” for sigma=0.2:0.4:20
E = edge(I,'log', 0, sigma); % zero means use thresh = 0
imshow(E, []), title(sprintf('sigma = %f', sigma));
pause end
21
Problem with LoG
• The location of the edge is affected by nearby corners
• Example – zero crossing locations near a corner
-
+
1
0
22
• We can derive the optimal edge operator to find step edges in the presence of white noise, where “optimal” means
– Good detection (minimize the probability of detecting false edges and missing real edges)
– Good localization (detected edges must be close to the true edges)
– Single response (return only one point for each true edge point)
• Canny’s analysis
– He simulated step edges, with additive white Gaussian noise
– Did numerical optimization to find the optimal edge operator
– Found that a good approximation to the optimal operator is the first derivative of a Gaussian, in the direction of the gradient
Similar to LoG , but doesn’t use Laplacian
23
• The gradient of the smoothed image is
G
I
G
I
0.8
0.6
• The gradient operators are
G
G
x
G
y
T
0.4
0.2
0
-0.2
-0.4
x
y
1
3 exp
x
2
2
2 y
2
-0.6
-0.8
-4 -3 -2 -1 0 x
1 2 3 4
• An edge point is a peak in the magnitude of the gradient of the smoothed image, in the direction of the gradient
– We then suppress nonmaxima along this direction
•
Convolve image with derivative of Gaussian operators (dG/dx,
(dG/dy)
• Find the gradient direction at each pixel; quantize into one of four directions (northsouth, east-west, northeast-southwest, northwest-southeast)
• If magnitude of gradient is larger than the two neighbors along this direction, it is a candidate edge point
25
• We want to join edge points into connected curves or lines
– This facilitates object recognition
• Problem:
– Some edge points along the curve may be weak, causing us to miss them
– This would result in a broken curve
• Solution:
– We use a high threshold to make sure we capture true edge points
– Given these detected points, link additional edge points into contours using a lower threshold (“hysteresis”)
• Algorithm
– Find all edge points greater than t high
– From each strong edge point, follow the chains of connected edge points in both directions perpendicular to the edge normal
– Mark all points greater than t low
26
• Compare LoG to Canny
• Try synthetic image of a white square on a black background
I=zeros(400);
I(100:300, 100:300) = 1.0; s = 15.0;
E1 = edge(I,'log', 0, s); % 0 means to use zero thresh imshow(E1, []), title(sprintf('s = %f', s));
E2 = edge(I, 'canny', [], s); % [] means to use auto thresh figure, imshow(E2, []), title(sprintf('s = %f', s));
27
• Edges detected for image of a square
LoG s = 15.000000
Canny s = 15.000000
28
• Try on “bag.png”
I = imread( 'bag.png' ); for sigma=0.2:0.4:20
E1 = edge(I, 'log' , 0, sigma); % zero means use thresh = 0
subplot(1,2,1), imshow(E1, []), title(sprintf( 'log, s=%f' , sigma));
E2 = edge(I, 'canny' , [], sigma); % [] means to use auto thresh
subplot(1,2,2), imshow(E2, []), title(sprintf( 'canny, s=%f' , sigma));
pause end
29
• Image bag.png
LoG s = 1.000000
Canny s = 1.000000
LoG s = 2.000000
Canny s = 2.000000
30
• [E,thresh]=edge(I, ‘canny’, thresh, sigma);
– thresh is [tLow tHigh]
– sigma is std deviation of Gaussian
• Vary sigma for sigma=0.5:0.5:5
E = edge(I, 'canny' , [], sigma); % [] means to use auto thresh
imshow(E);
title(sprintf( 'sigma=%f' , sigma));
pause end
• Vary thresholds for tHigh = 0.05:0.05:0.4
E = edge(I, 'canny' , [0.4*tHigh tHigh], 1.5);
imshow(E);
title(sprintf( 'tHigh=%f' , tHigh));
pause; end
• See effect of using only one threshold
[E,thresh]=edge(I, 'canny' , [], 1.5); figure, imshow(E), title( 'dual thresh' );
E = edge(I, 'canny' , [thresh(1) thresh(1)+eps], 1.5); figure, imshow(E), title( 'low thresh only' );
E = edge(I, 'canny' , [thresh(2) thresh(2)+eps], 1.5); figure, imshow(E), title( 'high thresh only' );
Image “house.jpg”
31
• Edge points are locations in the image where the intensity changes rapidly.
– Edge operators use spatial derivatives to detect these locations.
• It is useful to be able to detect edges at different scales, depending on the features of interest.
– Adjusting the sigma of a Gaussian low pass filter is a convenient way to select the scale.
• Why should you smooth the image with a low-pass filter prior to computing derivatives?
32