You are on page 1of 43

Image Processing & Computer Vision

Edge Detection
2 Edges as Reduced Images
Edges seem to be important…

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
3 Why Edge Detection
- More compactly represents salient features of –
the scene than pixels
- Helps in determining object boundaries and
segmentation
- Further features can be extracted from the
edges of an image (e.g., corners, lines etc.)
- These features can be used by higher-level
computer vision algorithms (e.g., recognition).
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
4 Edge Detection
What is an edge? An edge is a set of connected pixels
that lie on the boundary between two regions. It is
locally identifiable place of local transition from one
object to another. They are not complete border.

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
6 Edges in reality

Depth
discontinuity:
Reflectance change, object boundary
texture

Cast shadows
Discontinuous
change in surface
orientation

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
7 Edges & Derivatives
We have already spoken about how derivatives are
used to find discontinuities
Image intensity function first derivative
(along horizontal scanline)

edges correspond to
extrema of derivative
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
9 Image gradient

f f
The gradient of an image: f = [ , ]
x y

f f f f
f = [ , 0]  f= [ 0 , ] f = [ , ]
y  x y
x

The gradient points in the direction of most


rapid increase in intensity

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
10 Image gradient

For a function f(x, y) the gradient of f at coordinates (x, y)


is given as the column vector:
 f 
G x   x 
f =   =  f 
G y   
 y 
 

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
11 Image gradient (cont…)

The edge strength is given by the gradient


magnitude: f = mag (f )
1
 f   f  
2 2 2

=   +
 
 
x   y  

 

For practical reasons this can be simplified as:


f  G x + G y

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
12 Recollect our 1st Derivative
For 2D function, f(x,y), the partial derivative is defined as:
 f ( x , y) f (x +,y) − f(x ,y )
= lim
x  →0

For discrete data, we can approximate using finite
differences:
f (x, y)
 f (x +1, y) − f (x, y)
• Right difference x
f (x, y)
 f (x , y) − f (x-1, y)
• Left difference x
f
• Central difference  ( f (x +1) − f (x −1)) / 2
x

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
13 1st derivatives as a filter

f ( x , y) f ( x , y)
x y
-1
-1 1 1

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
14 1st Derivative Kernels

0 0 Not symmetric 0 0 0 Central


-1 +1 around image -1/2 0 +1/2 difference
0 0 0 0 0
point; which is
H “middle” pixel? H

-1 0 1 1 1 1
Prewitt Kernel:
1 -1 0 1 1 0 0 0
Try to reduce the ∗ ∗
6 -1 0 1 6 -1 -1 -1
sensitiveness to
noise by averaging
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
15 Sobel operator
-1 0 1 1 2 1
(Sobel) Gradient is 1 1
∗ -2 0 2 ∗ 0 0 0
8 -1 0 1 8 -1 -2 -1

I = [gx gy]T g𝑥 g𝑦

g = (gx2 + gy2)1/2 is the gradient magnitude.


 = atan2(gy , gx) is the gradient direction.

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
16 Edge Detection Example
Original Image Horizontal Gradient Component

Vertical Gradient Component Combined Edge Image


Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
17 But in the real world…
Derivative based edge detectors are very sensitive to
noise

f(x)

Apply derivative operator….

d
f(x) where’s
dx
the edge?

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
18
Solution: smooth first

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
19
Solution: smooth first
f

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
20
Solution: smooth first
f

h f

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
21
Solution: smooth first

f Where is the
edge?
h
h f


(h  f ) Look for peaks
x

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
22 Importance of Scale
Structures exist at multiple scales

Too fine of a scale…can’t see the forest for the trees.


Too coarse of a scale…the branches are gone.
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
23 Scale space

A simple image pyramid Image scale space by varying Edge gradient at


sigma of a smoothing function different scale
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
24 Main Steps in edge detection
1. Image smoothing: for reduction of noise and
irrelevant details/adjusting scale
2. Differentiation: apply differential operator to obtain
the gradient magnitude
3. Thresholding: determining edge pixels
4. Localization - Finding the curve with peak/optimal
gradient magnitude.
5. link or connect edge pixels.

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
25 Optimal edge detector
Criteria for an “optimal” edge detector:
Good detection: the optimal detector must minimize the probability of false
positives (detecting spurious edges caused by noise), as well as that of
false negatives (missing real edges)
Good localization: edges detected must be as close as possible to true edges
Single response: the detector must return one point only for each true edge
point; that is, minimize the number of local maxima around the true edge

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
26 The Canny Edge Detection
1. Smooth the image with a Gaussian filter with spread .
2. Compute gradient magnitude and direction at each pixel of
the smoothed image.
3. non-maximum suppression: Thin multi-pixel wide “ridges”
down to single pixel width
4. Linking and thresholding (hysteresis): Use double
thresholding to segregate ‘strong’ (high-magnitude) and
‘weak’ edge pixels. Use strong edge pixels to start edge
curves and weak edge pixels to continue them.
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
27 Example Result

original image (Lena) magnitude of the gradient

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
28 Example Result

thresholding

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
29 Example Result

How to turn
these thick
regions of
the gradient
into curves?

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
30 Non-maximal suppression

• Check if pixel is local maximum along gradient direction.


Zero out pixel whose magnitude is less than its
neighbour’s along the gradient direction.
• Can require checking interpolated pixels p and r
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
31 Example Result

Problem:
pixels along this
edge didn’t
survive the
thresholding
thinning
(non-maximum
suppression)

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
32 Canny threshold hysteresis
1. Apply a high threshold to detect strong edge pixels.
2. Link those strong edge pixels to form strong edges.
3. Apply a low threshold to find weak but plausible edge
pixels.
4. Extend the strong edges to follow weak edge pixels by
doing connectivity analysis (keep only those weak
edge pixels which are connected to strong edge
pixels).

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
33 Final Canny edge result

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
34 Effect of  (Gaussian kernel spread/size)

original Canny with 𝜎 = 1 Canny with 𝜎 =2


• Large σ detects large scale edges
• Small σ detects fine features
The choice of σ depends on desired behavior
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
35
An optimization

h
h f


(h  f )
x

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
36 Derivative property of convolution
Derivativeproperty of convolution :  ( f  g ) = f
*g
x x

This saves us one operation:

f

x
h


( x
h )  f

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
37 2nd Derivative Methods
• Finding optimal edges (maxima of gradient
magnitude) is equivalent to finding places where the
second derivative is zero. Why?
• How do we find maxima or minima of any functions?
We differentiate them and find places where the
derivative is zero.
• Differentiating the first derivative (gradient
magnitude) gives us the second derivative

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
38 2nd Derivative Methods: Laplacian
We encountered the 2nd-order derivative based
Laplacian filter already – isotropic i.e rotationally
invariant

The Laplacian is typically not used by itself as it is too


sensitive to noise .It is combined with a smoothing
Gaussian filter for purpose of edge detection
Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
39 1D 2nd derivative of Gaussian
f
f
h
2 h h
2 Second derivative of Gaussian
2
x operator 
h
x
2

2
(h  f)
x
2

Zero-crossings of bottom graph are edges


Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
40 2nd Derivative Methods (contd.)
• The zeroes may not fall exactly on a pixel. We can
isolate these zeroes by finding zero crossings:
places where one pixel is positive and a
neighbour is negative (or vice versa)
• zero crossings form closed paths and very
sensitive to noise

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
41 LOG (Marr-Hildreth edge detector)
• To reduce the noise effect and also to adjust the
scale, the image is first smoothed.
• When the filter chosen is a Gaussian, we call it the
LoG edge detector.
σ controls smoothing/adjusts scale
• It can be shown that:

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
42 LOG (Marr-Hildreth edge detector)
The Laplacian of Gaussian function can be
approximated over a discrete spatial neighborhood to
give a convolution kernel which looks like a Mexican
hat.

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
43 Example Result

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
44 Canny vs LoG Edge Detector

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT
45 Matlab
Demo

Image Processing and Computer Vision by Dr. Praveen Kumar @ CSE, VNIT

You might also like