image processing and computer vision chapter 5: hough transform (week4 continue) hough transform...

62
Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b 1

Upload: jared-hawkins

Post on 19-Dec-2015

256 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Image processingand computer vision

Chapter 5: Hough transform (week4 continue)

Hough transform v4.b 1

Page 2: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Overview

• What is the problem?– A method to fit a line for a set of features.

• Why traditional method is poor?– Need a different formulation for different line types (i.e., line, curve,

circle…)• How to do it?

– We will discuss the basic algorithm for lines, circles, ellipse.• We will extend it to any line types.

– Using the Generalized Hough transform• Ref

– http://en.wikipedia.org/wiki/Hough_transform– Lecture notes by Instructor: S. Narasimhan

Hough transform v4.b 2

Page 3: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

The problem

• Fit a curve , line, circle or ellipse to a set of edge points.

• Traditional method– Energy minimization by

Calculus. Features are found by edge detection

• Fit a curve, a circle, an ellipse or a line which has minimum distances from the edges.

Hough transform v4.b 3

cmxy

cmxy ii

),( ii yxy

x

Page 4: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Least Squares energy minimization(see http://mathworld.wolfram.com/LeastSquaresFitting.html)

Hough transform v4.b 4

cmxy

cmxy ii

),( ii yxy

x

Given: Many pairs

Find: Parameters

Minimize: Average square distance:

Using:

Note:

),( ii yx

),( cm

i

ii

N

cmxyE

2)(

0&0

c

E

m

E xmyc

ii

iii

xx

yyxxm

2)(

))((

N

yy i

i

N

xx i

i

Page 5: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Curve Fitting

Hough transform v4.b 5

y

x

Find Polynomial:

that best fits the given points

Minimize:

Using:

Note: is LINEAR in the parameters (a, b, c, d)

),( ii yx

dcxbxaxxfy 23)(

i

iiii dcxbxaxyN

223 )]([1

0,0,0,0

d

E

c

E

b

E

a

E

)(xf

Page 6: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Line detection using Hough transform

For straight line detection(a very efficient method)

Hough transform v4.b 6

Page 7: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Example• To find the line (y=mx+c)

– y=-2x+10– m=-2, c=10

• Assume you don’t know the line formula– But have three points passing the line– X=1, y=8– X=2, y=6– X=3,y=4

Hough transform v4.b 7

x

y

(1,8)(2,6)

(3,4)

Page 8: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Change of variables (x,y)(m,c)

• y=mx+c• m=(y-c)/x• m=(-1/x)c+y/x• m=G*c+D, where G=-1/x, D=y/x• Procedure: find (m,c) from points (xi,yi)

– 1)Find the line for (m,c) as variables (not x,y)– 2)For each point (xi,yi), plot line in (m,c) space

• Give a range of m values (e.g -10,0,10. etc,at least 2 values)• Find c value for each m value, plot line

– 3) The cutting point is the solution for (m,c)

Hough transform v4.b 8

Page 9: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Exercise 1 :Derivation exercise

• y=px+q• P is the gradient of the line, q is the vertical-

axis cutting point• Rearrange in the form in p,q space• p=(U)q+(V)• Find U,V in terms of x,y• Answer:

Hough transform v4.b 9

Page 10: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Excel example1• y=mx+c , (orginal)• m=(y-c)/x• m=(-1/x)c+y/x• Procedures• 1) m=G*c+D, where G=-1/x, D=y/x• 2) plot lines in (m,c) space

– m=(-1/xi), D= (yi/xi)– For (x1,y1)=(1,8),

• m=-10, c (=18 found)• m=0, c (=8 found)• m=10, c (=-2 found)

– Repeat for (xi,yi)=(2,6) and (3,4).– Each point (xi,yi) gives a line

• Cutting point in (m,c) space is the solution.• (m,c)=(-2,10)• y=-2x+10

10 -2

2 6 -0.5 3 -10 260 6

10 -14

3 4 -0.33333 1.333333 -10 340 4

10 -26

-15

-10

-5

0

5

10

15

-40 -20 0 20 40

Series1

Series2

Series3

Hough transform v4.b 10

points

Their corresponding lines in (m,c) space:Give 3 values of mfind values in c, plot line

Cutting point (m,c)=(-2,10)So the line is y=-2x+10 (done!)

Page 11: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Exercise 2Excel example• Given 3 points find the line formula

– X=1, y=9– X=3, y=15– X=5, y=21

• y=mx+c , (orginal)• m=(y-c)/x• m=(-1/x)c+y/x• Procedures• 1) m=G*c+D, where G=-1/x, D=y/x• 2) plot lines in (m,c) space

– m=(-1/xi)G*c+(yi/xi)– For (x1,y1)=(1,9),

• m=-10, c ?____• m=0, c ?____• m=10, c ?____

– Repeat for (xi,yi)=(3,15) and (5,21).– Each point (xi,yi) gives a line

• Cutting point in (m,c) space is the solution.

x y G D m(set a range)c to be found1 9

3 15

5 21

Hough transform v4.b 11

points

Their corresponding lines in (m,c) space:Give 3 values of mfind values in c, plot line

Page 12: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Reason to use Hough transform

• Why the previous energy minimization is too complicated?– Different formulations for different types (line, circle,

ellipse etc)– Hough is the same method for all types.

• Hough transform is more suitable for computers.• For straight lines (m,c) space is ok but not

extendable, so use (r, ) space for lines, circle, epllipse

Hough transform v4.b 12

Page 13: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

How to apply Hough transform to line detection using (r, ) space.

It is easily extendable to circle and ellipse• A line can be represented in

this way.

.

)sin()sin(

)cos(

distr

angle

rxy

Hough transform v4.b 13

=Edge pointy

x

r

Page 14: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Representation changes from (x,y) to (r, )

• A test line can be represented by– r (Dist)= the perpendicular line from

origin to the test line– (Angle)= between the perpendicular

line and the horizontal axis

ii

ii

xyr

rxy

rcm

cmxy

)cos()sin( so

,)sin()sin(

)cos( hence,

)sin(,

)sin(

)cos(

Hough transform v4.b 14

Test line

Perpendicular Line of the test line

Note: =Edge point

y

x

r

Page 15: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Exercise 3Prove

,)sin(

,)sin(

)cos(

prove To

,Given

r

cm

cmxy

Hough transform v4.b 15

Test line

Perpendicular Line of the test line

=Edge point

y

r

(x”,0)

x

(0,y”)=(0,c)

ii

ii

xyr

rxy

)cos()sin( so

,)sin()sin(

)cos( hence

Page 16: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Example

• To find the line – y=-2x+10– m=-2, c=10

• Assume you don’t know the line formula– But have three points passing the line– X=1, y=8– X=2, y=6– X=3,y=4

Hough transform v4.b 16

Page 17: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Hough line detection (e.g a 3 edge point example)

• We have 3 points (1,8), (2,6), (3,4).

• concentrate on point (1,8).– Create a test line table (=0,

30, 60 ,..) of lines that pass (1,8)

– Plot vs r

• Repeat above steps for the points (2,6), (3,4).

x 1y 8Theta () r

0 130 4.8760 7.4390 8.00

120 6.43150 3.13

ii

ii

xyr

orr

xy

cmxy

)cos()sin(

,)sin()sin(

)cos(

Hough transform v4.b 17

Test lines for (1,8) using Different (0o, 30o, 60o…)

x

y

(1,8)

(2,6)

(3,4)=30

r=4.87

Fixed x=1,y=8, find r of different

Page 18: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Exercise 4:Hough line detection (e.g a 3 edge point example)

• We have 3 points (1,8), (2,6), (3,4).

• concentrate on point (2,6).– Create a test line table (=0, 30,

60 ,..) of lines that pass (2,6) – Plot vs r

• Formula• =SIN($A4*PI()/180)

*B$2+COS($A4*PI()/180)*B$1

• Repeat above steps for the points (3,4).

0306090

120150

ii

ii

xyr

orr

xy

cmxy

)cos()sin(

,)sin()sin(

)cos(

Hough transform v4.b 18

Test lines for (2,6)

x

y

(1,8)

(2,6)

(3,4)=30

R=4.87

Answer: fill in the r column:

Page 19: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

3 points 3 curves

• Formula “=SIN($A4*PI()/180) *B$2+COS($A4*PI()/180)*B$1”

• Solution: the intersecting point= (r,)=(4.5,27o)• To verify: m=-cos()/sin()=-1.96, C=r/sin()=9.9• Done, real answer is y=-2x+10, m=-2, c=10 (Hough method produces a

solution close enough to the real answer)

x 1 2 3y 8 6 4 r

0 1 2 330 4.87 4.73 4.6060 7.43 6.20 4.9690 8.00 6.00 4.00

120 6.43 4.20 1.96150 3.13 1.27 -0.60

series1 series2 series3

-2-10123456789

0 50 100 150 200r(

dis

t)

Theta

Hough transform

Series1

Series2

Series3

Hough transform v4.b 19

ii

ii

xyr

orr

xy

cmxy

)cos()sin(

,)sin()sin(

)cos(

Theta=

Page 20: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Algorithm for computation

• Objective: Given N edge points (xi,yi) for i=1,…N

find ( , r) • Create a 2-D array of ( , r), initialized with zeros• For i=1,…N• { For j=1,2,.., 360 (i.e. using 1 degree resolution)

• {– Find rj using the above formula

– Increment once the cell with indexes close to j and rj

• }• }• The cell in the ( , r) array with the biggest value is the solution.• Accuracy depends on how many memory cells you used

ii

ii

xyr

orr

xy

cmxy

)cos()sin(

,)sin()sin(

)cos(

Hough transform v4.b 20

Page 21: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Java Demo

• Demo

Hough transform v4.b 21

http://www.dis.uniroma1.it/~iocchi/slides/icra2001/java/hough.html

Page 22: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Exercise 5

• Create the Angle-Distance (r, ) table for the edge point (0,3). The step length of the angle is 45 degrees. (Angle=)

• Fill in the r-column:

Angle Dist (r)0

4590

135180

ii

ii

xyr

orr

xy

cmxy

)cos()sin(

,)sin()sin(

)cos(

Hough transform v4.b 22

In radian=(/180)*angles_in_degreeTry Google “=sin(45)”

Page 23: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Circle detection using Hough transform

ReferenceCourse notes by Sumit Tandon :# EE6358 Computer Vision, University of Texas at Arlington

Hough transform v4.b 23

Page 24: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Circle Detection

• A circle that first the give edges xi,yi (i=1,..N) is

• We need to find the center (xc,yc) and radius (r )

• Each edge point gives a cone (if r is not fixed, if r is fixed they are circles on a plane) at different locations in Hough space

• The intersection of all these cones is the solution. for xc,yc,r .

Hough transform v4.b 24

)1()()( 222 ryyxx cici

Page 25: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

To show the basic conceptIf r is known, it is a 2D problem: circles on a plane

)1()()( 222 ryyxx cici

Hough transform v4.b 25

Circle:

Radius r is known using a 2D Hough space

From (xi, yi) space to (xc, yc) spaceAccumulator Array (xc,yc)

xi

yi

r

xc

xc

(xi,yi)

xc

yc

solution

Each edge point becomes a circle

Page 26: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Hough transform v4.b 26

More efficient if directions of the edges are known. Here we show what is an edge direction• Example

– i= the angle between the horizontal axis and the line perpendicular to the tangent of the edge

• This line (perpendicular to the tangent of the edge ) should pass through the center of the circle

yi

(xi,yi)(edge)i

xi,yi (edge)

Tangent of the edge

(xc,yc) (center of a circle)

Tangent of the edge

i-90

-90

Assume the Circle curveisclockwise

(Xc,yc)

xi

Line perpendicular to the tangent of the edge

Page 27: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Direction of the edge

Hough transform v4.b 27

e=edge directioni=the angle between the horizontal axis and the line perpendicular to the tangent of the edge direction

Example ofEdge direction(+/-22.5 degrees): yellow for 0 degrees, green for 45 degrees, blue for 90 degrees and red for 135 degrees.----------------------------------To find edge direction in degrees:Gx=I*horizontal_maskGy=I*vertical_mask

Edge direction (e)=tan-1(Gy/Gx) See appendix

http://en.wikipedia.org/wiki/Canny_edge_detector

Edge i

Edge di Line perpendicular to the tangent of the edgerection

Tangent of the

edge

Horizontalaxis

i

e

Page 28: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

The use of edge direction in Hough transform

• Gradient at each edge point (tangent of the edge) is known– i=direction of the edge i= the

angle between the horizontal axis and the line perpendicular to the tangent of the edge

– So we know the line that passes through the center of the circle

– In the accumulated table for each edge we list : xc,yc,r

)3(sin

)2(cos

iic

iic

ryy

rxx

Hough transform v4.b 28

xi

yi

(xc,yc) (center of a circle)

r

xi,yi (edge)i

Tangent of the edge

Line perpendicular to the tangent of the edge

Page 29: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Exercise 6

• A point on the circle is (xi,yi). (xc,yc), r are the center and radius of the circle, resp. Prove the formulas

)3(sin

)2(cos

iic

iic

ryy

rxx

Hough transform v4.b 29

(xc,yc)Center of the circle

r

(xi,yi)

yi

xi

i

Page 30: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Exercise 7a

• Create the table (r,Xc,Yc) table for the edge point (xi=2,yi=2, =0)

[xc,yc,]=[2,2,0]

r Xc=Xi-rcos()

Yc=yi-rsin()

1 2-1*cos(0)=?

2-1*sin(0)=?

2 2-2*cos(0)=?

2-2*sin(0)=?

3 ? ?

4 ? ?

5 ? ?Useful reference for degree/radianangle(deg) angle(rad) cos(angle) sin(angle)

0.00 0.00 1.00 0.0090.00 1.57 0.00 1.00

180.00 3.14 -1.00 0.00

Hough transform v4.b 30

Page 31: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Ex 7b :Create the table occurrence tableGiven 3 edge points , directions shown, find the circle.[xi,yi,]=[2,2,0]

r Xc=Xi-rcos() Yc=yi-rsin() Occurrence

1 2-1*cos(0)=? 2-1*sin(0)=?

2 2-2*cos(0)=? 2-2*sin(0)=?

3 ? ?

4 ? ?

5 ? ?

Hough transform v4.b 31

[xi,yi, ]=[1,3,90]

r Xc=Xi-rcos() Yc=yi-rsin() Occurrence

1 1-1*cos(90)=? 3-1*sin(90)=?

2 1-2*cos(90)=? 3-2*sin(90)=?

3 ? ?

4 ? ?

5 ? ?

[xi,yi,]=[0,2,180]

r Xc=Xi-rcos() Yc=yi-rsin() Occurrence

1 0-1*cos(180)=? 2-1*sin(180)=?

2 0-2*cos(180)=? 2-2*sin(180)=?

3 ? ?

4 ? ?

5 ? ?

Useful reference for degree/radianangle(deg) angle(rad) cos(angle) sin(angle)

0.00 0.00 1.00 0.0090.00 1.57 0.00 1.00

180.00 3.14 -1.00 0.00

Page 32: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Question 8Given 3 edge points at (xi,yi) plot the edges and the circle

EXERCISE 8xi yi theta r xc=xi-r*cos(theta) yc=yi-r*sin(theta) occurrence

2 2 -90 1 2 3 12345

2 5 90 12345

1 3 180 12345

Hough transform v4.b 32

Page 33: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Example

Hough transform v4.b 33

The circle is detected and made green

http://www.youtube.com/watch?v=jVQL1DODyUE

Page 34: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Idea• If the ith (i=1,.,,,N) edge (xi,yi) and its direction (i) are known,

formula(1) is not needed , instead we use

• Each edge point has a cone of all possible (x0,y0),r• Each entry in the table increments once in the accumulated array of

x0,y0,r. One such array is needed.• The highest score in the accumulated array (x0’,y0’,r’) after all edges

are handled is the result.– X0’,y0’ is the center,– r’=radius of the circle

)3(sin

)2(cos

iic

iic

ryy

rxx

Hough transform v4.b 34

Page 35: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Algorithm: Given (xi, yi) and i for i=1,..N. Find x0,y0,r

• Use a 3-dimensioal accumulated array with indexes xc,yc,r• (Length in each direction depends on resolution you choose)• For i=1,..N • {For each possible (xc,yc) find r using equation (2) and (3) • increment the cell (xc,yc,r) once.• }• The cell (xc’,yc’,r’) with the highest score in the accumulated array after all

edges are handled is the result.– Xc’,yc’ is the center,– r’=radius of the circle

)3(sin

)2(cos

iic

iic

ryy

rxx

Hough transform v4.b 35

Page 36: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

The same idea can be applied to other curves

• Some types•

Type parameters equations

Parabola xc, yc, (y-yc)2=4(x-xc)

Ellipse xc, yc, a, b, (x-xc)2/a2+(y-yc)2/b2=1

Hough transform v4.b 36

Page 37: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Conclusion

• Studied Hough transform• Learn how it can be used for line , circle

detection

Hough transform v4.b 37

Page 38: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

APPENDIX 1

Hough transform v4.b 38

Page 39: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Irregular shape detection by Generalized Hough transform

For the detections of any shapes(An advanced topic)

Hough transform v4.b 39

Page 40: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Generalized Hough Transform (we show fixed-scale-orientation here first)

Hough transform v4.b 40

•The irregular model shape which cannot be described by an equation•It shows 2 edges with the same edge direction (i)

xc=xi+ r(i,k)*cos ( (i,k))yc=yi+ r(i,k)*sin ((i,k))

e.g.2 edges with the same direction (i)

(i)

(i,k=1)

(i,k=2)

(i) r(i,k=2)

(Xc,yc)

r(i,k=1)

(xi,yi)

(xi,yi)’

A slight change of theEdge direction and angle between them.

Page 41: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Generalized Hough Transform(fixed-scale-orientation here)

(-table) Prepared by the user to define the shapeGivenEdge direction

Given Parameters,

{r(i,k),(i,k)},… for i=1,..,N. k=1,..,K(1) (2)::(i) :

{r(1,1), (1,1)}, {r(1,2), (1,2)}, {r(1,3), (1,3)},…{r(2,1), (2,1)}, {r(2,2), (2,2)}, {r(2,3), (2,3)},…

:

:

{r(i,1), (i,1)}, {r(i,2), (i,2)},….,{r(i,k),(i,k)}Hough transform v4.b 41

•The model shape is not described by an equation •The user prepares the edge direction table (-table)

e.g.2 edges with the same direction (i)

(i)

(i,k=1)

(i,k=2)(i)

r(i,k=2) (Xc,yc)

r(i,k=1)

(xi,yi)’

(xi,yi)

The number of entries “{r(), ()}” = the number of features points obtained.

Page 42: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

How to prepare the -table• Select an arbitrary reference point (xc,yc)• For (i=1,2,..,N) :each edge on the boundary.

– {Draw a vector r, – Measure direction of edge (i) – If (i) row of table is empty– k=1– Else – k=k+1– Measure and enter {r(i,k), (i,k)} in (i) row

of table– }

Hough transform v4.b 42

(i)

(i,k=1)

(Xc,yc)

r(i,k=1)

(xi,yi)’(xi+1,yi+1)’

Page 43: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

The problem and method

• Given edges (xi,yi) and their directions (i) for i=1,,,N. from an image

• We have to locate the shape (find xc,yc) described by the -table (or called r-table) .

Hough transform v4.b 43

Hough transformalgorithm

center of the shape (xc,yc)

The shape defined by the-table

InputImageEdge (xi,yi) and i

Page 44: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Generalized Hough Transform Find Object Center (Xc,yc) given edges (xi,yi,i) and -table

• Create Accumulator Array A(xc,yc)• Initialize: A(xc,yc)=0 for all (xc,yc)• For i=1,.. N• { get given edge point (xi,yi,i)• For k=1,.. K • { From i-th row in -table get {r(i,k), (i,k)}• xc=xi+ r(i,k)*cos ( (i,k))• yc=yi+ r(i,k)*sin ((i,k))• Increment : A(xc,yc)=A(xc,yc)+1• }• }• Solution: find Maxima in A(xc,yc)

Hough transform v4.b 44

Page 45: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Enhancement: Add scale and orientation

• Upgrade the table to include scale (s) and orientation () but complexity will increase

• A[xc,xy,s,] • Upgrade the fixed scale (s) and orientation ()

– xc=xi+ r(i,k)*cos ((i,k))– yc=yi+ r(i,k)*sin ((i,k))

• to– xc=xi+ r(i,k)*s*cos ((i,k)+ )– yc=yi+ r(i,k)*s*sin ( (i,k)+ )

Hough transform v4.b 45

S,

Page 46: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Introduction | Line detection | circle detection | irregular shape detection

Summary : Generalized Hough Transform

• It can be used to detect any shapes• Requirement: -table (or called r-table) for the shape• You can detect

– Position (xc,yc),– Size (s)– Orientation () – occurrences of that shape

Hough transform v4.b 46

Page 47: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 47

Appendix2

Page 48: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 48

Assume Gx,Gy are separable, the total gradient Gm becomes

• Horizontal gradient =Gx(i,j)= h_A * I(i,j)

• vertical gradient= Gy(i,j)= h_B * I(i,j)

• here,

degrees in tan)(_

),(*_),(*_

),(),(),(

01-0

000

010

h_A :_maskdge_filterVertical_e

000

101-

000

h_A :er_mask_edge_filtHorizontal

1

22

22

x

ye

yxm

G

Gdirectionedge

jiIBhjiIAh

jiGjiGjiG

* is the convolution operator

Horizontaldirection

verticaldirection

An edge is found and direction is e

e

Page 49: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 49

You may use Sobel masks

,

121

000

121

_,

101

202

101

_

_

BhAh

maskssobel

Sobel mask approximationof a horizontal edge Sobel mask approximation

of a vertcial edge

Page 50: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 50

Exercise 1 :Derivation exercise

• y=px+q• P is the gradient of the line, q is the vertical-axis cutting point• Rearrange in the form in p,q space• p=(U)q+(V)• Answer:

– y=px+q– (y-q)/x=p– P=(y/x)-(q/x)– P=(-1/x)q+y/x, – U=(-1/x), V=y/x

Page 51: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 51

answer example2• y=mx+c , (orginal)• m=(y-c)/x• m=(-1/x)c+y/x• Procedures• 1) m=G*c+D, where G=-1/x, D=y/x• 2) plot lines in (m,c) space

– m=(-1/xi)G*c+(yi/xi)– For (x1,y1)=(1,9),

• m=-10, c (=19 found)• m=0, c (=9 found)• m=10, c (=-1 found)

– Repeat for (xi,yi)=(3,15) and (5,21).– Each point (xi,yi) gives a line

• Cutting point in (m,c) space is the solution.

x y G D m(set a range)c to be found1 9 -1 9 -10 19

0 910 -1

3 15 -0.33333 5 -10 450 15

10 -15

5 21 -0.2 4.2 -10 710 21

10 -29

-15

-10

-5

0

5

10

15

-40 -20 0 20 40 60 80

line1

Series2

Series3

points

Their corresponding lines in (m,c) space:Give 3 values of mfind values in c, plot line

Cutting point (m,c)=(3,6)So the line is y=3x+6 (done!)

Page 52: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 52

Answer: Exercise 3• When x=0 , y=y”

– since y=mx+c– then y”=c----(1)

• When y=0, x=x”– For y=mx+c– then m=-c/x”---(2)– Put (1) in (2)– m=-y”/x”---(3)

• Also – r=x”cos(),---(5)– r=y”sin(),---(6)– From (6) and (1) – r=c*sin()c=r/sin()----(6)– hence (5)/(6)=y”/x”=cos()/sin()---(7)– Put (4) in(3), m=-cos()/sin() ---(8)– Proved at (6) and (8)

,)sin(

,)sin(

)cos(

prove To

,Given

r

cm

cmxy

Test line

Perpendicular Line of the test line

=Edge point

y

r

(x”,0)

x

(0,y”)=(0,c)

ii

ii

xyr

rxy

)cos()sin( so

,)sin()sin(

)cos( hence

Page 53: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 53

Answer: 3(alternative answer)Prove

• Answer:• When x=0,y’=c• C=r/sin()------(a)

• When y=0,x”=-c/m• (r/x”)=cos(()• so [r/(-c/m)]=cos()• -rm/c=cos()-----(b)• Put (a) in (b)• -rm/(r/sin())=cos()• m= -cos()/sin()

ii

ii

xyr

rxy

rcm

cmxy

)cos()sin( so

,)sin()sin(

)cos( hence,

)sin(,

)sin(

)cos(

Test line

Perpendicular Line of the test line

=Edge point

y

r

(x”,0)

x

(0,y’)=(0,c)

Page 54: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 54

Answer 4:Hough line detection (e.g a 3 edge point example)• We have 3 points (1,8), (2,6),

(3,4).• concentrate on point (2,6).

– Create a test line table (=0, 30, 60 ,..) of lines that pass (2,6)

– Plot vs r

• Forumla• =SIN($A4*PI()/180)

*B$2+COS($A4*PI()/180)*B$1

• Repeat above steps for the points (3,4).

x 2y 6Theta () r

0 230 4.7360 6.2090 6.00

120 4.20150 1.27

ii

ii

xyr

orr

xy

cmxy

)cos()sin(

,)sin()sin(

)cos(

Test lines for (2,6)

x

y

(1,8)

(2,6)

(3,4)=30

R=4.87

Answer: fill in the r column:

Page 55: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 55

matlab• x=2,y=6• theta=0,r=sin(theta*pi/

180)*y+cos(theta*pi/180)*x• theta=30,r=sin(theta*pi/

180)*y+cos(theta*pi/180)*x• theta=60• r=sin(theta*pi/180)*y+cos(theta*pi/180)*x• theta=90• r=sin(theta*pi/180)*y+cos(theta*pi/180)*x• theta=120• r=sin(theta*pi/180)*y+cos(theta*pi/180)*x• theta=150• r=sin(theta*pi/180)*y+cos(theta*pi/180)*x

• x =• 2• y =• 6• theta = 0, r = 2• theta = 30, r = 4.7321• theta = 60, r = 6.1962• theta = 90, r = 6• theta = 120, r = 4.1962• theta = 150, r = 1.2679

Page 56: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 56

Answer 5

• Create the Angle-Distance (r, ) table for the edge point (0,3). The step length of the angle is 45 degrees. (Angle=)

• Fill in the r-column:

x 0y 1.732051angle r

0 045 1.2290 1.73

135 1.22180 0.00

ii

ii

xyr

orr

xy

cmxy

)cos()sin(

,)sin()sin(

)cos(

Page 57: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 57

matlab• x=0,y=sqrt(3)• theta1=0,• theta=theta1;• r1=sin(theta*pi/180)*y+cos(theta*pi/180)*x• theta2=45,• theta=theta2;• r2=sin(theta*pi/180)*y+cos(theta*pi/180)*x• theta=90• theta=theta3;• r3=sin(theta*pi/180)*y+cos(theta*pi/180)*x• theta=135• theta=theta4;• r4=sin(theta*pi/180)*y+cos(theta*pi/180)*x• theta=180• theta=theta5;• r5=sin(theta*pi/180)*y+cos(theta*pi/180)*x• theta=150• theta=theta6;• r6=sin(theta*pi/180)*y+cos(theta*pi/180)*x• %text1=sprintf('%d,%d',theta1,r1)• sprintf('theta= %d, r=%d',theta1,r1)• sprintf('theta= %d, r=%d',theta2,r2)• sprintf('theta= %d, r=%d',theta3,r3)• sprintf('theta= %d, r=%d',theta4,r4)• sprintf('theta= %d, r=%d',theta5,r5)• sprintf('theta= %d, r=%d',theta6,r6)

• theta= 0, r=0• theta= 45, r=1.224745e+000• theta= 90, r=1.732051e+000• theta= 135, r=1.224745e+000• theta= 180, r=2.121150e-016• theta= 150, r=8.660254e-001

Page 58: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 58

Answer: Exercise 6

• A point on the circle is (xi,yi) and (xc,yc), r are the center and radius of the circle, resp. Prove the formulas

)3(sin

)2(cos

iic

iic

ryy

rxx

(Xc,yc))

r

(xi,yi)

x

xi

AnswerCos(i)=(xi-xc)/rr*Cos(i)=xi-xcXc=xi-r*cos (i)

sin(i)=(yi-yc)/rr*sin(i)=yi-ycyc=yi-r*sin (i)

Page 59: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Diagram for Exercise 7b

Hough transform v4.b 59

x

y

1 2 3 4 5

5

432

1

90

180

Result: Center at (2,3) r=1

Assume the Circle curveisclockwise

Page 60: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Hough transform v4.b 60

Ans:Ex 7a,b :Create the table occurrence tableGiven 3 edge points , draw the edges and find the circle.

[xi,yi,]=[2,2,0]

r Xc=Xi-rcos() Yc=yi-rsin() Occurrence

1 2-1*cos(0)=1 2-1*sin(0)=2 1

2 2-2*cos(0)=0 2-2*sin(0)=2 1

3 -1 2 1

4 -2 2 1

5 -3 2 1

[xi,yi, ]=[1,3,90]

r Xc=Xi-rcos() Yc=yi-rsin() Occurrence

1 1-1*cos(90)=1 3-1*sin(90)=2 2

2 1-2*cos(90)=1 3-2*sin(90)=?1 1

3 1 0 1

4 1 -1 1

5 1 -2 1

[xi,yi,]=[0,2,180]

r Xc=Xi-rcos() Yc=yi-rsin() Occurrence

1 0-1*cos(180)=1 2-1*sin(180)=2 3 (highest,the solution)(x-1)^2+(y-2)^2=1

2 0-2*cos(180)=2 2-2*sin(180)=2 1

3 3 2 1

4 4 2 1

5 5 2 1

Useful reference for degree/radianangle(deg) angle(rad) cos(angle) sin(angle)

0.00 0.00 1.00 0.0090.00 1.57 0.00 1.00

180.00 3.14 -1.00 0.00

Page 61: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Answer 8

• EXERCISE 8xi yi theta r xc=xi-r*cos(theta) yc=yi-r*sin(theta) occurrence

2 2 -90 1 2 3 12 2 4 13 2 5 14 2 6 15 2 7 1

2 5 90 1 2 4 22 2 3 23 2 2 14 2 1 15 2 0 1

1 3 180 1 2 3 3 hghest (xc=2,yc=3,r=1)2 3 4 13 4 5 14 5 6 1

Hough transform v4.b 61

Page 62: Image processing and computer vision Chapter 5: Hough transform (week4 continue) Hough transform v4.b1

Answer: Exercise 8

Hough transform v4.b 62

x

y

1 2 3 4 5

5

432

1

-90

90

180

Result: Center at (2,3) r=1

Assume the Circle curveisclockwise