35
CSE152, Spr 07 Intro Computer Vision Edge Detection, Lines, Hough Transform Introduction to Computer Vision CSE 152 Lecture 10

Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Edge Detection, Lines, Hough Transform

Introduction to Computer VisionCSE 152

Lecture 10

Page 2: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Announcements

• Assignment 2 on tracking due Tuesday, May 8.• Midterm: Thursday, May 10.

Page 3: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Last Lecture

Page 4: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Median filters : examplefilters have width 5 :

Page 5: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Edges

1. Object boundaries2. Surface normal discontinuities3. Reflectance (albedo) discontinuities4. Lighting discontinuities (shadow boundaries)

Page 6: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Edge is Where Change Occurs: 1-D• Change is measured by derivative in 1D

Smoothed Edge

First Derivative

Second Derivative

Ideal Edge

• Biggest change, derivative has maximum magnitude• Or 2nd derivative is zero.

Page 7: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Numerical Derivativesf(x)

xX0 X0+hX0-h

Take Taylor series expansion of f(x) about x0f(x) = f(x0)+f’(x0)(x-x0) + ½ f’’(x0)(x-x0)2 + …

Consider Samples taken at increments of h and first two terms, we have

f(x0+h) = f(x0)+f’(x0)h+ ½ f’’(x0)h2

f(x0-h) = f(x0)-f’(x0)h+ ½ f’’(x0)h2

Subtracting and adding f(x0+h) and f(x0-h) respectively yields

20

0

0

2)()(2)(

)(''

2)()(

)('

00

00

hhxfxfhxf

xf

hhxfhxf

xf

−−++−=

−−+=

Page 8: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Implementing 1-D Edge Detection1. Filter out noise: convolve with Gaussian

2. Take a derivative: convolve with [-1 0 1]– We can combine 1 and 2.

3. Find the peak of the magnitude of the convolved image: Two issues:

– Should be a local maximum.– Should be sufficiently high.

Page 9: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

2D Edge Detection: Canny

1. Filter out noise– Use a 2D Gaussian Filter.

2. Take a derivative– Compute the magnitude of the gradient:

22

Gradient theis ,),(

yx

yx

JJJ

yJ

xJJJJ

+=∇

⎟⎟⎠

⎞⎜⎜⎝

⎛∂∂

∂∂

==∇

GIJ *=

Page 10: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Next stepCan next step, be the same for 2-D edge

detector as in 1-D detector??

NO!!

Page 11: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

What is the gradient?

)0,(, kyI

xI

=⎟⎟⎠

⎞⎜⎜⎝

⎛∂∂

∂∂

Change

No Change

Page 12: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Smoothing and Differentiation• Need two derivatives, in x and y direction. • Filter with Gaussian and then compute

gradient, or• Use a derivative of Gaussian filter

• because differentiation is convolution, and convolution is associative

Page 13: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

xG∂∂ σ

yG∂∂ σ

yG

xG

∂∂

+∂∂ σσ θθ sincos

θ

Directional Derivatives

Page 14: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Finding derivatives

x

y

Is this dI/dx or dI/dy?

Page 15: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

There are three major issues:1. The gradient magnitude at different scales is different;

which scale should we choose?2. The gradient magnitude is large along thick trail; how

do we identify the significant points?3. How do we link the relevant points up into curves?

σ = 1 σ = 2

Page 16: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

There is ALWAYS a tradeoff between smoothing and good edge localization!

Image with Edge Edge Location

Image + Noise Derivatives detect edge and noise

Smoothed derivative removes noise, but blurs edge

Page 17: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer VisionThe scale of the smoothing filter affects derivative estimates1 pixel 3 pixels 7 pixels

Page 18: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

We wish to mark points along the curve where the magnitude is biggest.We can do this by looking for a maximum along a slice normal to the curve(non-maximum suppression). These points should form a curve. There arethen two algorithmic issues: at which point is the maximum, and where is thenext one?

Magnitude ofGradient

Page 19: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Non-maximum suppression

For every pixel in the image (e.g., q) we have an estimate of edge direction and edge normal (shown at q)

Page 20: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Non-maximum suppression

Using normal at q, find two points p and r on adjacent rows (or columns).

We have a maximum if the value is larger than those at both p and at r.

Interpolate to get values.

Page 21: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Assume the marked point is an edge point. Then we construct the tangent to the edge curve (which is normal to the gradient at that point) and use this to predict the next points (here either r or s).

Non-maximum suppressionPredicting the next edge point

Page 22: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Hysteresis• Track edge points by starting at point where

gradient magnitude > τhigh.

• Follow edge in direction orthogonal to gradient.

• Stop when gradient magnitude < τlow.• i.e., use a high threshold to start edge curves and a

low threshold to continue them.

τhigh

τlow

gradient magnitude

Page 23: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Input image

Page 24: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

T=15 T=5

HysteresisTh=15 Tl = 5

Hysteresisthresholding

SingleThreshold

Page 25: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Page 26: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

fine scale

high thresholds

Page 27: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

coarse scale,High thresholds

Page 28: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

coarsescaleLower highthresholds

Page 29: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Why is Canny so Dominant• Still widely used after 20 years.

1. Theory is nice (but end result same,).2. Details good (magnitude of gradient, non-max

suppression).3. Hysteresis an important heuristic.4. Code was distributed.

Page 30: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Corner Detection Sample Results

Threshold=25,000 Threshold=10,000

Threshold=5,000

Page 31: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

• Segment linked edge chains into curve features (e.g., line segments).

• Group unlinked or unrelated edges into lines (or curves in general).

• Accurately fitting parametric curves (e.g., lines) to grouped edge points.

What to do with edges?

Page 32: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

CSE152, Spr 07 Intro Computer Vision

Hough Transform[ Patented 1962 ]

Page 33: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

Finding lines in an imageOption 1:

• Search for the line at every possible position/orientation• What is the cost of this operation?

Option 2:• Use a voting scheme: Hough transform

Page 34: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

Finding lines in an image

Connection between image (x,y) and Hough (m,b) spaces• A line in the image corresponds to a point in Hough space• To go from image space to Hough space:

– given a set of points (x,y), find all (m,b) such that y = mx + b

x

y

m

b

m0

b0

image space Hough space

Page 35: Edge Detection, Lines, Hough Transform · 2D Edge Detection: Canny 1. Filter out noise – Use a 2D Gaussian Filter. 2. Take a derivative – Compute the magnitude of the gradient:

Finding lines in an image

Connection between image (x,y) and Hough (m,b) spaces• A line in the image corresponds to a point in Hough space• To go from image space to Hough space:

– given a set of points (x,y), find all (m,b) such that y = mx + b• What does a point (x0, y0) in the image space map to?

x

y

m

b

image space Hough space

– A: the solutions of b = -x0m + y0

– this is a line in Hough space

x0

y0