Image processing and computer vision Chapter 5 Hough

  • Slides: 66
Download presentation
Image processing and computer vision Chapter 5: Hough transform KH Wong Hough transform v

Image processing and computer vision Chapter 5: Hough transform KH Wong Hough transform v 1 a 1

Introduction | Line detection | circle detection | irregular shape detection Overview • What

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 methods are 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 v 1 a 2

Introduction | Line detection | circle detection | irregular shape detection The problem •

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 v 1 a 3

Introduction | Line detection | circle detection | irregular shape detection Least Squares energy

Introduction | Line detection | circle detection | irregular shape detection Least Squares energy minimization (see http: //mathworld. wolfram. com/Least. Squares. Fitting. html) Given: Many pairs Find: Parameters Minimize: Average square distance: Using: Note: Hough transform v 1 a 4

Introduction | Line detection | circle detection | irregular shape detection Curve Fitting Find

Introduction | Line detection | circle detection | irregular shape detection Curve Fitting Find Polynomial: that best fits the given points Minimize: Using: Note: is LINEAR in the parameters (a, b, c, d) Hough transform v 1 a 5

Introduction | Line detection | circle detection | irregular shape detection Line detection using

Introduction | Line detection | circle detection | irregular shape detection Line detection using Hough transform For straight line detection (a very efficient method) Hough transform v 1 a 6

Introduction | Line detection | circle detection | irregular shape detection Reason to use

Introduction | Line detection | circle detection | irregular shape detection Reason to use Hough transform • Why the previous energy minimization method is too complicated? Because: – Different formulations for different types (line, circle, ellipse etc. ) – Hough is the same method for all types. – Hough transform is more suitable for computers. Hough transform v 1 a 7

Introduction | Line detection | circle detection | irregular shape detection Example • To

Introduction | Line detection | circle detection | irregular shape detection Example • To find the line (y=mx+c) – y=-2 x+10 – m=-2, c=10 • Assume you don’t know the line formula – But have three points passing through the line y – X=1, y=8 (1, 8) Line (2, 6) – X=2, y=6 y=mx+c (3, 4) – X=3, y=4 x Hough transform v 1 a 8

Introduction | Line detection | circle detection | irregular shape detection Change of variables

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 as variables) – 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 of lines in m-c space is the solution for (m, c) Hough transform v 1 a 9

Introduction | Line detection | circle detection | irregular shape detection Exercise 1 :

Introduction | Line detection | circle detection | irregular shape detection Exercise 1 : Derivation exercise • Exercise 1: y=px+q, where p is the gradient of the line, q is the vertical-axis cutting point. Rearrange the equation in p, q space. I. e. p=(U)q+(V). Find U, V in terms of x, y • Answer: Hough transform v 1 a 10

Introduction | Line detection | circle detection | irregular shape detection Example 1 y=mx+c

Introduction | Line detection | circle detection | irregular shape detection Example 1 y=mx+c , (original) 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 (x, y) points G=(-1/xi), D= (yi/xi) For (x 1, y 1)=(1, 8), G=-(1/1), D=(8/1) So m=G*c+D=>m=-c+8 m m=-2, c (=10 found) m=0, c (=8 found) m=10, c (=-2 found): blue line c Cutting point of the 3 lines: (m, c)=(-2, 10) So the line is y=-2 x+10 (done!) 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=-2 x+10 Hough transform v 1 a 11

Introduction | Line detection | circle detection | irregular shape detection Exercise 2, Excel

Introduction | Line detection | circle detection | irregular shape detection Exercise 2, Excel example • Given 3 points find the line formula • • • – X=1, y=9 – X=3, y=15 – X=5, y=21 y=mx+c , (original), Now in m, c space 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 Their corresponding points lines in (m, c) space: Give 3 values of m find values in c, plot line – m=(-1/xi)*c+(yi/xi) – For (x 1, y 1)=(1, 9), – G=-(1/1), D=(9/1) – So m=G*c+D=>m=-c+9 • m=-10, 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. Hough transform v 1 a 12

Introduction | Line detection | circle detection | irregular shape detection Reason to use

Introduction | Line detection | circle detection | irregular shape detection Reason to use polar coordinates • Why the previous method (x, y) space to (m, c) space is still not ideal? Because: – Different formulations for different types (line, circle, ellipse etc): For straight lines (m, c) space is ok but not extendable, so use polar coordinates (r, ) space for lines, circle or ellipse. • Hough transform is very suitable for computer implementation. Hough transform v 1 a 13

Introduction | Line detection | circle detection | irregular shape detection How to apply

Introduction | Line detection | circle detection | irregular shape detection How to apply Hough transform to line detection using polar coordinates (r, ) space. It is easily extendable to circle and ellipse • A line can be represented in this way. y =Edge point • r x Hough transform v 1 a 14

Introduction | Line detection | circle detection | irregular shape detection Representation changes from

Introduction | Line detection | circle detection | irregular shape detection Representation changes from (x, y) to (r, ) space • • 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 Note: =Edge point y Perpendicular Line of the test line r Test line Hough transform v 1 a x 15

Introduction | Line detection | circle detection | irregular shape detection Exercise 3 •

Introduction | Line detection | circle detection | irregular shape detection Exercise 3 • • (0, y”) y =(0, c) Perpendicular Line of the test line =Edge point Test line r x (x”, 0) Hough transform v 1 a 16

Introduction | Line detection | circle detection | irregular shape detection Example • To

Introduction | Line detection | circle detection | irregular shape detection Example • To find the line – y=-2 x+10 – m=-2, c=10 • Assume you don’t know the line formula – But have three points on the line – X=1, y=8 – X=2, y=6 – X=3, y=4 Hough transform v 1 a 17

Introduction | Line detection | circle detection | irregular shape detection Hough line detection

Introduction | Line detection | circle detection | irregular shape detection Hough line detection (e. g a 3 edge point example) • We have 3 points (x, y)= (1, 8), (2, 6), (3, 4). • r=sin( )*yi+cos( )*xi • concentrate on the first point (xi, yi)=(1, 8). – Create a test line table ( =0, 30, 60 , . . ) of lines that pass thru. (xi, yi)=(1, 8) – Plot vs r y Test lines for (1, 8) using Different (0 o, 30 o, 60 o…) (1, 8) (2, 6) r=4. 87 (3, 4) =30 • x Fixed x=1, y=8, find r of different • Repeat above steps for the points (2, 6), (3, 4). (xi, yi)=(1, 8), if =30, so r=sin(30*pi/180)*yi+cos(30*pi/180)*xi r=sin(30*pi/180)*8+cos(30*pi/180)*1=4. 87 18 Hough transform v 1 a

Introduction | Line detection | circle detection | irregular shape detection Exercise 4: Hough

Introduction | Line detection | circle detection | irregular shape detection Exercise 4: Hough line detection (e. g a 3 edge point example) y • 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 (1, 8) (2, 6) R=4. 87 • Formula • (3, 4) =30 x =SIN($A 4*PI()/180) *B$2+COS($A 4*PI()/180)*B$1 Answer: fill in the Repeat above steps for the points r column: (3, 4). • • Test lines for (2, 6) Hough transform v 1 a 19

Introduction | Line detection | circle detection | irregular shape detection 3 points 3

Introduction | Line detection | circle detection | irregular shape detection 3 points 3 curves Theta= • Formula “=SIN($A 4*PI()/180) *B$2+COS($A 4*PI()/180)*B$1” • Solution: the intersecting point= (r, )=(4. 5, 27 o) • To verify: m=-cos( )/sin( )=-1. 96, C=r/sin( )=9. 9 • Done, real answer is y=-2 x+10, m=-2, c=10 (Hough method produces a Hough transform v 1 a solution close enough to the real answer) 20

Introduction | Line detection | circle detection | irregular shape detection Algorithm for computation

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 Hough transform v 1 a • 21

Introduction | Line detection | circle detection | irregular shape detection Hough Transform Demo

Introduction | Line detection | circle detection | irregular shape detection Hough Transform Demo • Demos From http: //www. dis. uniroma 1. it/~iocchi/slides/icra 2001/java/hough. html http: //www. activovision. com/octavi/doku. php? id=hough_transform https: //www. youtube. com/watch? v=ebfi 7 q. OFLuo If points are not exactly lying on the same line (noise effect), the curves may not collide at a single point. Solution: take approximation. Hough transform v 1 a 22

Introduction | Line detection | circle detection | irregular shape detection Exercise 5 •

Introduction | Line detection | circle detection | irregular shape detection Exercise 5 • • Create the Angle-Distance (r, ) table for the edge point (x, y)=(0, 3). The step length of the angle is 45 degrees. (Angle= ) Use =SIN($A 7*PI()/180) *B$2+COS($A 7*PI()/180)* B$1 Fill in the r-column: • In radian= ( /180)*angles_in_degree Try Google “=sin(45)” Hough transform v 1 a • 23

Introduction | Line detection | circle detection | irregular shape detection Circle detection using

Introduction | Line detection | circle detection | irregular shape detection Circle detection using Hough transform Reference Course notes by Sumit Tandon : # EE 6358 Computer Vision, University of Texas at Arlington Hough transform v 1 a 24

Introduction | Line detection | circle detection | irregular shape detection Circle Detection •

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 v 1 a 25

Introduction | Line detection | circle detection | irregular shape detection To show the

Introduction | Line detection | circle detection | irregular shape detection To show the basic concept If r is known, it is a 2 D problem: circles on a plane • Circle: Assume the radius r is known, each point on circle of the (x, y) space will form a circle in the (Xc, Yc) circle-enter-space yi Accumulator Array (xc, yc) (xi, yi) r yc Each edge point becomes a circle solution xc xc xi Hough transform v 1 a xc 26

Introduction | Line detection | circle detection | irregular shape detection More efficient if

Introduction | Line detection | circle detection | irregular shape detection 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 i -90 (Xc, yc) -90 xi, yi (edge) Tangent of the edge xi Tangent of the edge i Assume the Circle curve (xi, yi)(edge) is clockwise Line perpendicular to the tangent of the edge (xc, yc) (center of a circle) Hough transform v 1 a 27

Introduction | Line detection | circle detection | irregular shape detection Direction of the

Introduction | Line detection | circle detection | irregular shape detection Direction of the edge Edge i • Edge Example of di Line Edge direction(+/-22. 5 degrees): perpendicular yellow for 0 degrees, to the tangent green for 45 degrees, blue for 90 degrees and of the red for 135 degrees. e edgerection ----------------- Horizontal axis i he t f to n e g Tan e edg § § To find edge direction in degrees: Gx=I*horizontal_mask Gy=I*vertical_mask Edge direction ( e)=tan-1(Gy/Gx) See appendix (or notes on Canny edge detector, in chapter 3) e=edge direction http: //en. wikipedia. org/wiki/Canny_edge_detector i=the angle between the horizontal axis and the line perpendicular to the tangent of the edge direction Hough transform v 1 a 28

Introduction | Line detection | circle detection | irregular shape detection The use of

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 Tangent of the edge yi i Hough transform v 1 a r xi, yi (edge) Line perpendicular to the tangent of the edge (xc, yc) (center of a circle) xi 29

Introduction | Line detection | circle detection | irregular shape detection Exercise 6 •

Introduction | Line detection | circle detection | irregular shape detection Exercise 6 • A point on the circle is (xi, yi). The point (xc, yc), and a scalar r are the center and radius of the circle, resp. Prove the formulas • yi (xi, yi) i r (xc, yc) Center of the circle Hough transform v 1 a xi 30

Introduction | Line detection | circle detection | irregular shape detection Exercise 7 a

Introduction | Line detection | circle detection | irregular shape detection Exercise 7 a • Create the table (r, Xc, Yc) table for the edge point (xi=2, yi=2, =0) [xi, yi, ] =[2, 2, 0] r Xc= xi-rcos( ) Yc= yi-rsin( ) 1 2 -1*cos(0)= ? Find Xc: 2 -1*sin(0)= ? Find Yc: 2 2 -2*cos(0)= ? : 2 -2*sin(0)= ? : 3 ? : 4 ? : 5 ? : Hough transform v 1 a 31

Hough transform v 1 a Ex 7 b : Create the table occurrence table

Hough transform v 1 a Ex 7 b : Create the table occurrence table Given 3 edge points , directions shown, find the circle. [xi, yi, ]= [2, 2, 0] • [xi, yi, ]= [1. 7071, 2. 7071, 45] 45 degrees =0. 7854 radians [xi, yi, ]= [0, 2, 180] 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 ? ? r Xc=Xi-rcos( ) Yc=yi-rsin( ) 1 1 -1*cos(45 o)=? 3 -1*sin(45 o)=? 2 1 -2*cos(45 o)=? 3 -2*sin(45 o)=? 3 ? ? 4 ? ? 5 ? ? r Xc=Xi-rcos( ) Yc=yi-rsin( ) 1 0 -1*cos(180 o)=? 2 -1*sin(180 o)=? 2 0 -2*cos(180 o)=? 2 -2*sin(180 o)=? 3 ? ? 4 ? ? 5 ? ? Occurrence 32

Exercise 8 (a) In many cases you are not sure about the direction of

Exercise 8 (a) In many cases you are not sure about the direction of the edge. For example, the edge at (1, 3) can be 0 or 180 o, or at (3, 1) it can be 90 o or -90 o, so what will you do? (b) Fill in the following table and find (Xc, Yc)=____, r=____ Find (Xc, Yc)=__, r=? ____ y 5 4 3 2 1 =90 =180 =-90 1 2 3 4 5 Assume the Circle curve is clockwise x Hough transform v 1 a 33

Introduction | Line detection | circle detection | irregular shape detection Demo • The

Introduction | Line detection | circle detection | irregular shape detection Demo • The circle is detected and made green http: //www. youtube. com/watch? v=j. VQL 1 DODy. UE Hough transform v 1 a 34

Introduction | Line detection | circle detection | irregular shape detection Idea: why it

Introduction | Line detection | circle detection | irregular shape detection Idea: why it works • 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 (x 0, y 0), r • Each entry in the table increments once in the accumulated array of x 0, y 0, r. One such array is needed. • The highest score in the accumulated array (x 0’, y 0’, r’) after all edges are handled is the result. – X 0’, y 0’ is the center, – r’=radius of the circle Hough transform v 1 a 35

Introduction | Line detection | circle detection | irregular shape detection Algorithm: Given (xi

Introduction | Line detection | circle detection | irregular shape detection Algorithm: Given (xi , yi) and i for i=1, . . N. • Find x 0 , y 0 , 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 Hough transform v 1 a 36

Introduction | Line detection | circle detection | irregular shape detection The same idea

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, r, (y-yc)2=4 r(x-xc) Ellipse xc, yc, a, b, (x-xc)2/a 2+(y-yc)2/b 2=1 Hough transform v 1 a 37

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

Conclusion • Studied Hough transform • Learn how it can be used for line , circle detections Hough transform v 1 a 38

APPENDIX 1 Hough transform v 1 a 39

APPENDIX 1 Hough transform v 1 a 39

Latex ch 5 of 5711 • • • • • • • • •

Latex ch 5 of 5711 • • • • • • • • • documentclass{article} %for testing formulas ppt cam, dec 8, 2019 usepackage{amsmath} begin{document} Ch 5 of 5711, Hough transform\ %%%%###### slide 7 ######## %begin{flalign} %begin{aligned} %R_{cam} = R_c^{-1}, T_{cam} %end{aligned} %end{flalign} %end{document} %%%###### 05_hough slide 23 ######## begin{flalign} begin{aligned} y &= mx+c\ y_i &=left( -frac{cos(theta )}{sin(theta)}right)x_i + left( frac{r}{sin(theta)}right), text{or}\ r &= sin(theta) y_i +cos(theta)x_i end{aligned} end{flalign} %%%%%%%%%%%%%%%%% %%%###### 05_hough slide 25 ######## slide 25\ begin{flalign} begin{aligned} (x_i-x_c)^2+(y_i-y_c)^2=r^2 end{aligned} end{flalign} %%%%%%%%%%%%%%%%%%% %%%###### 05_hough slide 30 ######## slide 30\ begin{flalign} begin{aligned} x_c&=x_i-rcos(theta_i)\ y_c&=y_i-rsin(theta_i) Hough transform v 1 a 40

Introduction | Line detection | circle detection | irregular shape detection Irregular shape detection

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 v 1 a 41

Introduction | Line detection | circle detection | irregular shape detection Generalized Hough Transform

Introduction | Line detection | circle detection | irregular shape detection Generalized Hough Transform (we show fixed-scale-orientation here first) • 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)) (Xc, yc) (i) r(i, k=2) r(i, k=1) A slight change of the Edge direction and angle between them. (xi, yi) e. g. 2 edges with the same direction (i) (xi, yi)’ Hough transform v 1 a (i, k=1) 42

Introduction | Line detection | circle detection | irregular shape detection Generalized Hough Transform

Introduction | Line detection | circle detection | irregular shape detection Generalized Hough Transform (fixed-scale-orientation here) • The model shape is not described by an equation • The user prepares the edge direction table ( -table) (xi, yi) r(i, k=2) (Xc, yc) (i, k=2) r(i, k=1) e. g. 2 edges with (xi, yi)’ the same direction (i) (i, k=1) ( -table) Prepared by the user to define the shape Given Edge direction Given Parameters, (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, k), (i, k)}, … for i=1, . . , N. k=1, . . , K : : {r(i, 1), (i, 1)}, {r(i, 2), (i, 2)}, …. , {r(i, k), (i, k)} Hough transform v 1 a The number of entries “{r(), ()}” = the number of features points obtained. 43

Introduction | Line detection | circle detection | irregular shape detection How to prepare

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. (Xc, yc) – {Draw a vector r, – Measure direction of edge (i) r(i, k=1) – If (i) row of table is empty – k=1 (i) – Else – k=k+1 (xi+1, yi+1)’ (xi, yi)’ (i, k=1) – Measure and enter {r(i, k), (i, k)} in (i) row of table – } Hough transform v 1 a 44

Introduction | Line detection | circle detection | irregular shape detection The problem and

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). Input Image Edge (xi, yi) and i The shape defined by the -table Hough transform algorithm Hough transform v 1 a center of the shape (xc, yc) 45

Introduction | Line detection | circle detection | irregular shape detection Generalized Hough Transform

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 v 1 a 46

Introduction | Line detection | circle detection | irregular shape detection Enhancement: Add scale

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 ( ) S, – 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 v 1 a 47

Introduction | Line detection | circle detection | irregular shape detection Summary : Generalized

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 v 1 a 48

Appendix 2 Hough transform v 1 a 49

Appendix 2 Hough transform v 1 a 49

Assume Gx, Gy are separable, the total gradient Gm becomes • Horizontal gradient =Gx(i,

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, • Horizontal direction vertical direction * is the convolution operator e Hough transform v 1 a An edge is found 50 and direction is e

You may use Sobel masks • • Sobel mask approximation of a horizontal edge

You may use Sobel masks • • Sobel mask approximation of a horizontal edge Sobel mask approximation of a vertcial edge Hough transform v 1 a 51

Exercise 1 : Derivation exercise • • • y=px+q P is the gradient of

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 Hough transform v 1 a 52

answer example 2 • • • points Their corresponding lines in (m, c) space:

answer example 2 • • • points Their corresponding lines in (m, c) space: Give 3 values of m find values in c, plot line 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)*c+(yi/xi) – For (x 1, y 1)=(1, 9), • • • m=-10, -10=(-1/1)*c+9/1, c =19 found m=0, 0=(-1/1)*c+9/1 c =9 found m=10, 10=(-1/1)*c+9/1 c =-1 found m 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. Cutting point (m, c)=(3, 6) So the line is y=3 x+6 (done!) Hough transform v 1 a 53

 • Answer: Exercise 3 • When x=0 , y=y” – since y=mx+c –

• Answer: Exercise 3 • When x=0 , y=y” – since y=mx+c – then y”=c----(1) • When y=0, x=x” – – (0, y”) y =(0, c) For y=mx+c then m=-c/x”---(2) Put (1) in (2) m=-y”/x”---(3) Perpendicular Line of the test line =Edge point • Also – – – – Test line r 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) x (x”, 0) Hough transform v 1 a 54

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

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( ) (0, y’) y =(0, c) Perpendicular Line of the test line =Edge point Test line r x (x”, 0) Hough transform v 1 a 55

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

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). y – Create a test line table ( =0, 30, 60 , . . ) of lines that pass (2, 6) – Plot vs r • Forumla • Test lines for (2, 6) (1, 8) (2, 6) R=4. 87 =SIN($A 4*PI()/180) *B$2+COS($A 4*PI()/180)*B$1 (3, 4) =30 • Repeat above steps for the points (3, 4). x Answer: fill in the r column: Hough transform v 1 a 56

 • • • 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

• • • 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 matlab • • • Hough transform v 1 a 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 57

 Answer 5 • • Create the Angle-Distance (r, ) table for the edge

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: Hough transform v 1 a 58

 • • • • • • • x=0, y=sqrt(3) theta 1=0, theta=theta 1;

• • • • • • • x=0, y=sqrt(3) theta 1=0, theta=theta 1; r 1=sin(theta*pi/180)*y+cos(theta*pi/180)*x theta 2=45, theta=theta 2; r 2=sin(theta*pi/180)*y+cos(theta*pi/180)*x theta=90 theta=theta 3; r 3=sin(theta*pi/180)*y+cos(theta*pi/180)*x theta=135 theta=theta 4; r 4=sin(theta*pi/180)*y+cos(theta*pi/180)*x theta=180 theta=theta 5; r 5=sin(theta*pi/180)*y+cos(theta*pi/180)*x theta=150 theta=theta 6; r 6=sin(theta*pi/180)*y+cos(theta*pi/180)*x %text 1=sprintf('%d, %d', theta 1, r 1) sprintf('theta= %d, r=%d', theta 2, r 2) sprintf('theta= %d, r=%d', theta 3, r 3) sprintf('theta= %d, r=%d', theta 4, r 4) sprintf('theta= %d, r=%d', theta 5, r 5) sprintf('theta= %d, r=%d', theta 6, r 6) matlab • • • theta= 0, r=0 theta= 45, r=1. 224745 e+000 theta= 90, r=1. 732051 e+000 theta= 135, r=1. 224745 e+000 theta= 180, r=2. 121150 e-016 theta= 150, r=8. 660254 e-001 Excel Hough transform v 1 a 59

Answer: Exercise 6 • A point on the circle is (xi, yi) and (xc,

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 • Answer Cos( i)=(xi-xc)/r r*Cos( i)=xi-xc Xc=xi-r*cos ( i) sin( i)=(yi-yc)/r r*sin( i)=yi-yc yc=yi-r*sin ( i) x (xi, yi) i r (Xc, yc)) x Hough transform v 1 a 60

Clc %matlab demo clear format short. G r=1 xc=1 yc=2 theta_degree=[ 0, 45, 90,

Clc %matlab demo clear format short. G r=1 xc=1 yc=2 theta_degree=[ 0, 45, 90, 120, 180] ; theta=theta_degree*pi/180 ; for i=1: length(theta) %xc=xi-r*cos(theta) x(i)=xc+r*cos(theta(i)) y(i)=yc+r*sin(theta(i)) end x y theta_degree Diagram for Exercise 7 b y 5 4 3 2 1 (x=1. 7071, y=2. 7071, Theta_degrees=45 ) 90 180 Assume the Circle curve is clockwise 1 2 3 4 5 x Result: Center at (2, 1) r=1 If center is (xc, yc)=(2, 1), r=1 xc=xi-r*cos(theta_i), theta_i=acos((xc-xi)/r) %%%%%%%%%%%% x = 2 1. 7071 1 0. 5 0 y = 2 2. 7071 3 2. 866 2 theta = 0 0. 7854 1. 5708 2. 0944 3. 1416 theta_degree = 0 45 90 120 180 • Hough transform v 1 a 61

Ans: Ex 7 a, b : Create the table occurrence table Given 3 edge

Ans: Ex 7 a, b : Create the table occurrence table Given 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. 7071, 2. 7071, 45] r Xc=Xi-rcos( ) Yc=yi-rsin( ) Occurrence 1 1. 7071 -1*cos(0. 7854) 1 2. 70711*sin(0. 7854) 2 After rounding (1, 2) 1+1=2 45 degrees =0. 7854 radians Cos(0. 7854 )=0. 70711 2 1 -2*cos(45*pi/180) =-0. 414 3 -2*sin(45*pi/180)=1. 585 1 3 1 -3*0. 7071=-1. 1213 3 -2*sin(45*pi/180)= 1. 5858 1 4 ? ? ? 5 ? ? ? • [xi, yi, ]= [0, 2, 180] r Xc=Xi-rcos( ) Yc=yi-rsin( ) Occurrence 1 0 -1*cos(180 o)=1 2 -1*sin(180 o)=2 2+1=3 (highest): SOLUTION (x-1)^2+(y-2)^2=1 2 0 -2*cos(180 o)=2 2 -2*sin(180 o)=2 1 3 3 2 1 4 4 2 1 5 5 Hough transform v 1 a 2 1 62

Exercise 8 (a) In many cases you are not sure about the direction of

Exercise 8 (a) In many cases you are not sure about the direction of the edge. For example the edge at (1, 3) can be 0 or 180 o, or at (3, 1) it can be 90 o or -90 o, so what will you do? (b) Fill in the following table and find (Xc, Yc)=____, r=____ Find (Xc, Yc)=__, r=? ____ y 5 4 3 2 1 =90 =180 =-90 1 2 3 4 5 Assume the Circle curve is clockwise x Hough transform v 1 a 63

Answer (8 a) • (a) In many cases you are not sure about the

Answer (8 a) • (a) In many cases you are not sure about the direction of the edge. For example the edge at (1, 3) can be 0 or 180 o, or at (3, 1) it can be 90 o or -90 o, so what will you do? • Answer 8 (a) Treat the two directions as two different edges and put them in the table, the correct result will come out. E. g. at (1, 3) the edge can be 0 or 180 o. SO put • (Xi, Yi)=(1, 3) Theta=0 • (Xi, Yi)=(1, 3) Theta=180 • in the table as two entry groups, the occurrence will indicate the correct answer. It is because the correct result will have more support from other edges. Hough transform v 1 a 64

Hough transform v 1 a Answer (8 b) • 65

Hough transform v 1 a Answer (8 b) • 65

Answer: Exercise 8 b • y 5 4 3 2 1 (Xc, Yc)=(3, 3),

Answer: Exercise 8 b • y 5 4 3 2 1 (Xc, Yc)=(3, 3), r=2 found Assume the Circle curve is clockwise 90 180 -90 1 2 3 4 5 x Result: Center at (X=3, Yc=3) r=2 Hough transform v 1 a 66