You are on page 1of 88

CNG 483

Introduction to Computer Vision


Edge Detection

Asst. Prof. Dr. Meryem Erbilek

Slide partially based on Stanford U. CS131

METU Lecture 5 - 1 CNG 483


What we will learn today
• Edge detection
• Image Gradients
• A simple edge detector
• Sobel edge detector
• Canny edge detector
• Hough Transform
Some background reading:
Forsyth and Ponce, Computer Vision: A Modern Approach, Part II: Early Vision: Just One Image, Chp.5
Richard Szeliski, Computer Vision: Algorithms and Applications, Chp.4.

Slide partially based on Stanford U. CS131

METU Lecture 5 - 2 CNG 483


What we will learn today
• Edge detection
• Image Gradients
• A simple edge detector
• Sobel edge detector
• Canny edge detector
• Hough Transform

Slide partially based on Stanford U. CS131

METU Lecture 5 - 3 CNG 483


Slide partially based on Stanford U. CS131

METU Lecture 5 - 4 CNG 483


(A) Cave painting at Chauvet, France, about 30,000 B.C.;
(B) Aerial photograph of the picture of a monkey as part of the Nazca Lines geoglyphs, Peru, about 700 – 200 B.C.;
(C) Shen Zhou (1427-1509 A.D.): Poet on a mountain top, ink on paper, China;
(D) Line drawing by 7-year old I. Lleras (2010 A.D.).
Slide partially based on Stanford U. CS131

METU Lecture 5 - 5 CNG 483


Slide partially based on Stanford U. CS131
Hubel & Wiesel, 1960s

METU Lecture 5 - 6 CNG 483


We know edges are special from
human (mammalian) vision studies

Hubel & Wiesel, 1960s


Slide partially based on Stanford U. CS131

METU Lecture 5 - 7 CNG 483


Slide partially based on Stanford U. CS131 Walther, Chai, Caddigan, Beck & Fei-Fei, PNAS, 2011
METU Lecture 5 - 8 CNG 483
Edge detection
• Goal: Identify sudden
changes (discontinuities) in
an image
– Intuitively, most semantic and
shape information from the
image can be encoded in the
edges
– More compact than pixels

• Ideal: artist’s line drawing


(but artist is also using object-
level knowledge)
Slide partially based on Stanford U. CS131 Source: D. Lowe

METU Lecture 5 - 9 CNG 483


Why do we care about edges?
• Extract information,
recognize objects

• Recover geometry and Vertical vanishing


point
(at infinity)

viewpoint Vanishing
line

Vanishing Vanishing
point point

Slide partially based on Stanford U. CS131 Source: J. Hayes

METU Lecture 5 - 10 CNG 483


Origins of edges

surface normal discontinuity

depth discontinuity

surface color discontinuity

illumination discontinuity

Slide partially based on Stanford U. CS131 Source: D. Hoiem

METU Lecture 5 - 11 CNG 483


Closeup of edges

Surface normal discontinuity

Source: D. Hoiem
Slide partially based on Stanford U. CS131

METU Lecture 5 - 12 CNG 483


Closeup of edges

Depth discontinuity

Source: D. Hoiem
Slide partially based on Stanford U. CS131

METU Lecture 5 - 13 CNG 483


Closeup of edges

Surface color discontinuity

Source: D. Hoiem
Slide partially based on Stanford U. CS131

METU Lecture 5 - 14 CNG 483


What we will learn today
• Edge detection
• Image Gradients
• A simple edge detector
• Sobel edge detector
• Canny edge detector
• Hough Transform

Slide partially based on Stanford U. CS131

METU Lecture 5 - 15 CNG 483


Derivatives in 1D

Slide partially based on Stanford U. CS131

METU Lecture 5 - 16 CNG 483


Derivatives in 1D - example

Slide partially based on Stanford U. CS131

METU Lecture 5 - 17 CNG 483


Derivatives in 1D - example

Slide partially based on Stanford U. CS131 Slide credit: Dr Mubarak

METU Lecture 5 - 18 CNG 483


Discrete Derivative in 1D

Slide partially based on Stanford U. CS131

METU Lecture 5 - 19 CNG 483


Types of Discrete derivative in 1D

Backward

Forward

Central

Slide partially based on Stanford U. CS131

METU Lecture 5 - 20 CNG 483


1D discrete derivate filters
• Backward filter: [0 1 -1]

• Forward: [-1 1 0]

• Central: [1 0 -1]

Slide partially based on Stanford U. CS131

METU Lecture 5 - 21 CNG 483


1D discrete derivate example (backward)

Slide partially based on Stanford U. CS131

METU Lecture 5 - 22 CNG 483


Discrete derivate in 2D

Slide partially based on Stanford U. CS131

METU Lecture 5 - 23 CNG 483


Discrete derivate in 2D

Slide partially based on Stanford U. CS131

METU Lecture 5 - 24 CNG 483


Discrete derivate in 2D

Slide partially based on Stanford U. CS131

METU Lecture 5 - 25 CNG 483


2D discrete derivative filters

What does this filter do?

Slide partially based on Stanford U. CS131

METU Lecture 5 - 26 CNG 483


2D discrete derivative filters

What about this filter?

Slide partially based on Stanford U. CS131

METU Lecture 5 - 27 CNG 483


2D discrete derivative - example

Slide partially based on Stanford U. CS131

METU Lecture 5 - 28 CNG 483


2D discrete derivative - example
What happens when we apply
this filter?

Slide partially based on Stanford U. CS131

METU Lecture 5 - 29 CNG 483


2D discrete derivative - example
What happens when we apply this filter?

Slide partially based on Stanford U. CS131

METU Lecture 5 - 30 CNG 483


2D discrete derivative - example
Now let’s try the other filter!

Slide partially based on Stanford U. CS131

METU Lecture 5 - 31 CNG 483


2D discrete derivative - example
What happens when we apply this filter?

Slide partially based on Stanford U. CS131

METU Lecture 5 - 32 CNG 483


3x3 image gradient filters

Slide partially based on Stanford U. CS131

METU Lecture 5 - 33 CNG 483


What we will learn today
• Edge detection
• Image Gradients
• A simple edge detector
• Sobel edge detector
• Canny edge detector
• Hough Transform

Slide partially based on Stanford U. CS131

METU Lecture 5 - 34 CNG 483


Image gradient
• The gradient of an image:

The gradient vector points in the direction of most rapid increase in intensity

The gradient direction is given by

• how does this relate to the direction of the edge?

The edge strength is given by the gradient magnitude

Source: Steve Seitz


Slide partially based on Stanford U. CS131

METU Lecture 5 - 35 CNG 483


Characterizing edges
• An edge is a place of rapid change in the
image intensity function
intensity function
image (along horizontal scanline) first derivative

edges correspond to
extrema of derivative
(we will develop techniques to
Slide partially based on Stanford U. CS131 localize them)
METU Lecture 5 - 36 CNG 483
Finite differences: example
• Which one is the gradient in the x-direction? How about y-direction?

Original Gradient
Image magnitude

x-direction y-direction

Slide partially based on Stanford U. CS131

METU Lecture 5 - 37 CNG 483


Intensity profile

Intensity
Gradient

Source: D. Hoiem
Slide partially based on Stanford U. CS131

METU Lecture 5 - 38 CNG 483


Effects of noise
• Consider a single row or column of the image
– Plotting intensity as a function of position gives a signal

Where is
the edge?

Slide partially based on Stanford U. CS131 Source: S. Seitz

METU Lecture 5 - 39 CNG 483


Effects of noise

Slide partially based on Stanford U. CS131

METU Lecture 5 - 40 CNG 483


Effects of noise
• Finite difference filters respond strongly to
noise
– Image noise results in pixels that look very
different from their neighbors
– Generally, the larger the noise the stronger the
response
• What is to be done?
– Smoothing the image should help, by forcing
pixels different to their neighbors (=noise pixels?)
to look more like neighbors
Source: D. Forsyth
Slide partially based on Stanford U. CS131

METU Lecture 5 - 41 CNG 483


Smoothing with different filters
• Mean smoothing

• Gaussian (smoothing * derivative)

Slide partially based on Stanford U. CS131 Slide credit: Steve Seitz


METU Lecture 5 - 42 CNG 483
Smoothing with different filters

Slide partially based on Stanford U. CS131 Slide credit: Steve Seitz


METU Lecture 5 - 43 CNG 483
Solution: smooth first
f

f*g

• To find edges, look for peaks in


Slide partially based on Stanford U. CS131 Source: S. Seitz

METU Lecture 5 - 44 CNG 483


Derivative theorem of convolution
• This theorem gives us a very useful property:

• This saves us one operation:

Slide partially based on Stanford U. CS131 Source: S. Seitz

METU Lecture 5 - 45 CNG 483


Derivative of Gaussian filter

* [1 0 -1] =

2D-gaussian x - derivative

Slide partially based on Stanford U. CS131

METU Lecture 5 - 46 CNG 483


Derivative of Gaussian filter

x-direction y-direction

Slide partially based on Stanford U. CS131

METU Lecture 5 - 47 CNG 483


Derivative of Gaussian filter

Slide partially based on Stanford U. CS131

METU Lecture 5 - 48 CNG 483


Tradeoff between smoothing at different scales

1 pixel 3 pixels 7 pixels

• Smoothed derivative removes noise, but blurs


edge. Also finds edges at different “scales”.
Slide partially based on Stanford U. CS131 Source: D. Forsyth

METU Lecture 5 - 49 CNG 483


Designing an 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)

Slide partially based on Stanford U. CS131

METU Lecture 5 - 50 CNG 483


Designing an 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: the edges detected must be as close as possible to
the true edges

Slide partially based on Stanford U. CS131

METU Lecture 5 - 51 CNG 483


Designing an 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: the edges detected must be as close as possible to
the 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

Slide partially based on Stanford U. CS131

METU Lecture 5 - 52 CNG 483


What we will learn today
• Edge detection
• Image Gradients
• A simple edge detector
• Sobel Edge detector
• Canny edge detector
• Hough transform

Slide partially based on Stanford U. CS131

METU Lecture 5 - 53 CNG 483


Sobel Operator
• uses two 3×3 kernels which
are convolved with the original image to
calculate approximations of the derivatives
• one for horizontal changes, and one for
vertical

Slide partially based on Stanford U. CS131

METU Lecture 5 - 54 CNG 483


Sobel Operation
• Smoothing + differentiation

Gaussian smoothing differentiation

Intuition: take a weighted average to obtain


smoothed f(x-1,y) and f(x+1,y) data.
Slide partially based on Stanford U. CS131

METU Lecture 5 - 55 CNG 483


Sobel Operation
• Magnitude:

• Angle or direction of the gradient:

Slide partially based on Stanford U. CS131

METU Lecture 5 - 56 CNG 483


Sobel Filter example

Slide partially based on Stanford U. CS131

METU Lecture 5 - 57 CNG 483


Sobel Filter Problems

• Poor Localization (Trigger response in multiple adjacent pixels)


• Favors certain directions (horizontal and vertical edges) over others (oblique)
– False negatives

Slide partially based on Stanford U. CS131

METU Lecture 5 - 58 CNG 483


What we will learn today
• Edge detection
• Image Gradients
• A simple edge detector
• Sobel Edge detector
• Canny edge detector
• Hough Transform

Slide partially based on Stanford U. CS131

METU Lecture 5 - 59 CNG 483


Canny edge detector
• This is probably the most widely used edge
detector in computer vision
• Theoretical model: step-edges corrupted by
additive Gaussian noise
• Canny has shown that the first derivative of
the Gaussian closely approximates the
operator that optimizes the product of
signal-to-noise ratio and localization

J. Canny, A Computational Approach To Edge Detection, IEEE Trans. Pattern


Analysis and Machine Intelligence, 8:679-714, 1986.
Slide partially based on Stanford U. CS131

METU Lecture 5 - 60 CNG 483


Canny edge detector
• Suppress Noise
• Compute gradient magnitude and direction
• Apply Non-Maximum Suppression
– Assures minimal response
• Use hysteresis and connectivity analysis to
detect edges

Slide partially based on Stanford U. CS131

METU Lecture 5 - 61 CNG 483


Example

• original image
Slide partially based on Stanford U. CS131

METU Lecture 5 - 62 CNG 483


Derivative of Gaussian filter

x-direction y-direction

Slide partially based on Stanford U. CS131

METU Lecture 5 - 63 CNG 483


Compute gradients (DoG)

X-Derivative of Gaussian Y-Derivative of Gaussian Gradient Magnitude

Source: J. Hayes
Slide partially based on Stanford U. CS131

METU Lecture 5 - 64 CNG 483


Get orientation at each pixel

Source: J. Hayes
Slide partially based on Stanford U. CS131

METU Lecture 5 - 65 CNG 483


Canny edge detector
• Suppress Noise
• Compute gradient magnitude and direction
• Apply Non-Maximum Suppression
– Assures minimal response

Slide partially based on Stanford U. CS131

METU Lecture 5 - 66 CNG 483


Non-maximum suppression
• Edge occurs where gradient reaches a maxima
• Suppress non-maxima gradient even if it
passes threshold
• Only eight angle directions possible
– Suppress all pixels in each direction which are not
maxima
– Do this in each marked pixel neighborhood

Slide partially based on Stanford U. CS131

METU Lecture 5 - 67 CNG 483


Remove spurious gradients
is the gradient at pixel (x, y)

Slide partially based on Stanford U. CS131

METU Lecture 5 - 68 CNG 483


Non-maximum suppression
At q, we have a
maximum if the
value is larger than
those at both p
and at r.
Interpolate to get
these values.

Slide partially based on Stanford U. CS131 Source: D. Forsyth

METU Lecture 5 - 69 CNG 483


Non-maximum suppression

Slide partially based on Stanford U. CS131

METU Lecture 5 - 70 CNG 483


Non-max Suppression

Before
Slide partially based on Stanford U. CS131
After
METU Lecture 5 - 71 CNG 483
Canny edge detector
• Suppress Noise
• Compute gradient magnitude and direction
• Apply Non-Maximum Suppression
– Assures minimal response
• Use hysteresis and connectivity analysis to
detect edges

Slide partially based on Stanford U. CS131

METU Lecture 5 - 72 CNG 483


Hysteresis thresholding
• Define two thresholds: Low and High
– If less than Low, not an edge
– If greater than High, strong edge
– If between Low and High, weak edge

Slide partially based on Stanford U. CS131

METU Lecture 5 - 73 CNG 483


Connecting edges
• The final step is connecting the edges. All
strong edge pixels are edges.
• For weak edge pixels, only the weak edge
pixels that are linked to strong edge pixels
are edges.
• The part uses BFS or DFS to find all the edges.

Slide partially based on Stanford U. CS131

METU Lecture 5 - 74 CNG 483


Connecting edges

strong edge pixel weak but connected


edge pixels

strong edge pixel

Slide partially based on Stanford U. CS131 Source: S. Seitz

METU Lecture 5 - 75 CNG 483


Final Canny Edges

Slide partially based on Stanford U. CS131

METU Lecture 5 - 76 CNG 483


Effect of σ (Gaussian kernel spread/size)

original Canny with Canny with

The choice of σ depends on desired behavior


• large σ detects large scale edges
• small σ detects fine features
Slide partially based on Stanford U. CS131 Source: S. Seitz

METU Lecture 5 - 77 CNG 483


Hough Transform
• Hough Transform is a way to detect particular structures
(whose parametric equation is known) in images.
• This works to detect lines (or other shapes) in an image
after an edge detector is applied to get the pixels of just
the edges.

Slide partially based on Stanford U. CS131

METU Lecture 5 - 78 CNG 483


Hough Transform
Consider a straight line with slope a and intercept b using the equation
in the xy-plane; y = ax + b

Slide partially based on Stanford U. CS131

METU Lecture 5 - 79 CNG 483


Hough Transform
Points in xy-plane forms an intersection of lines in ab-space.

Slide partially based on Stanford U. CS131

METU Lecture 5 - 80 CNG 483


Hough Transform
A practical difficulty with this approach, however, is that a (the slope of a line)
approaches infinity as the line approaches the vertical direction.
Hene, lets use the polar representation of a line using angle θ and distance ρ :
x cos  + y sin  = p

ρ : euclidean distance between the point and origin.


θ : angle between the distance vector and the positive x-axis.

Slide partially based on Stanford U. CS131

METU Lecture 5 - 81 CNG 483


Hough Transform
• Algorithm:
• Step 1: Partition the Pθ-plane into accumulator A[P,θ]

Range of P (distance) is  m +n (image size m  n)


2 2

Range of  (angle) is  90

• Step 2: initialize A[P,θ]=0

Slide partially based on Stanford U. CS131

METU Lecture 5 - 82 CNG 483


Hough Transform
• Algorithm:
• Step3: for each edge point [x,y] in the image
for θ = [θmin to θmax ]
P=xcosθ+ysinθ
A[P,θ]+=1

• Example: Evaluate P and θ for point (x,y)= (50,100)


p = x cos  + y sin 
... p = 50 cos 0 + 100 sin 0 = 50
p = 50 cos 90 + 100 sin 90 = 100
p = 50 cos 45 + 100 sin 45 = 106.1

Slide partially based on Stanford U. CS131

METU Lecture 5 - 83 CNG 483


Hough Transform
• Algorithm:

• Step 4: Find the line candidetes (i.e. value(s) of (P, θ)) where A[P, Q] is maximum or
above threshold. (For Line Detection!)

Slide partially based on Stanford U. CS131

METU Lecture 5 - 84 CNG 483


Hough Transform
• Example: consider 5 points in the given image.

Slide partially based on Stanford U. CS131

METU Lecture 5 - 85 CNG 483


Hough Transform
• Matlab Code:

Slide partially based on Stanford U. CS131

METU Lecture 5 - 86 CNG 483


Hough Transform
• Matlab Code:

Slide partially based on Stanford U. CS131

METU Lecture 5 - 87 CNG 483


Hough Transform
• Detecting Circles?

Slide partially based on Stanford U. CS131

METU Lecture 5 - 88 CNG 483

You might also like