Hough Transform For Straight Lines: Mini-Project in Image Processing, 7th Semester 2007 by Jeppe Jensen. Group 721
Hough Transform For Straight Lines: Mini-Project in Image Processing, 7th Semester 2007 by Jeppe Jensen. Group 721
Mini-project in Image Processing, 7th semester 2007 By Jeppe Jensen. Group 721
The Hough transform is a technique used to nd shapes in a binary digital image. By Hough Transform it is possible to nd all kind of shapes that can be mathematical expressed, for instance lines, circles and ellipses, but only straight lines will be considered here. If having a white pixel in a binary image, innity many straight lines can go through that single pixel, and each of these lines can go through other white pixels in the same image, and the more white pixels on the same line the more is this line represented in the image. This is the principle of the Hough transform for straight lines. As mentioned above a shape can be found if a mathematical expression can be set for the shape, and in this case where the shape is a straight line, an expression can be set as: y = ax+b
(1)
Where a is the slope, and b is where the line intersects the y-axis. These parameters, a and b, can be used to represent a straight line as single point (a, b) in the parameter-space spanned by the two parameters a and b. The problem by represent a line as a point in the (a, b) parameter-space, is that both a and b goes toward innity when the line becomes more and more vertical, and thereby the parameterspace becomes innity large. Therefore it is desirable to nd another expression of the line with some parameters that have limited boundaries. It is done by using an angle and a distance as parameters, instead of a slope and an intersection. If the distance (rho) is the distance from the origin to the line along a vector perpendicular to the line, and the angle (theta) is the angle between the x-axis and the vector (see Figure 1), Equation 1 can be written as: y= cos () x+ sin () sin ()
(2)
Figure 1: Rho and theta representation of a straight line. Each line has a unique
parameter set (, ).
The expressions here, instead of a and b, is found by trigonometrical calculations. To get an expression of , Equation 2 can be rearranged to: = x cos () + y sin () (3)
Contrary to when the parameters is a and b, the values that and can have are limited to: [0, 180] in degrees or [0, ] in radians, and [D, D] where D is the diagonal of the image. A line can then be transformed into a single point in the parameter space with the parameters and , this is also called the Hough space. If, instead of a line, having a pixel in an image with the position (x, y), innity many lines can go through that single pixel. By using Equation 3 all these lines can be transformed into the Hough space, which gives a sinusoidal curve that is unique for that pixel. Doing the same for another pixel, gives another curve that intersect the rst curve in one point, in the Hough space. This point represents the line, in the image space, that goes through both pixels. This can be repeated for all the pixels on the edges, in a edge detected image. Figure 2 shows an example with a RGB input image and the image after edge detection has been made. When the Hough transform is made on the image for all the white pixels (edges) the lines that have most pixels lie on can be found, the result as the Hough space can be seen in Figure 3. The result of the Hough transform is stored in a matrix that often called an accumulator. One dimension of this matrix is the theta values (angles) and the other dimension is the rho values (distances), and each element has a value telling how many points/pixel that lie on the line with the parameters (rho, theta). So the element with the highest value tells what line that is most represented in the input image.
Figure 2: An example with an input image and the result of the edge detection,
Figure 3: The Hough space of the input image in Figure 2. The dark red points
are the points with the highest number of intersections. Many dark red points are around 90 degrees, i.e. that the image has many horizontal lines.
Implementation
The Hough transform is implemented in Matlab. Even though Matlab has some built-in Hough functions, these are not used. Listing 1 shows the pseudo code of the Hough implementation. For a more detailed code, see the source code in houghLines.m.
Listing 1: Pseudo code of the Hough Transform for straight lines
1 2
l o a d image f i n d t h e e d g e s i n t h e image
3 4 5 6 7 8 9 10
f o r a l l p i x e l s i n t h e image i f t h e p i x e l ( x , y ) i s an e d g e for a l l the t h e t a angles c a l c u l a t e r h o f o r t h e p i x e l ( x , y ) and t h e a n g l e ( t h e t a ) i n c r e m e n t t h a t p o s i t i o n ( rho , t h e t a ) i n t h e a c c u m u l a t o r show t h e hough s p a c e find the highest value in the accumulator draw t h e l i n e w i t h t h e h i g h e s t v a l u e i n t h e i n p u t image
References
By Richard O. Duda & Peter E. Hart Use of the Hough Transformation to Detect Lines and Curves in Pictures, April 1971 URL: http://www.ai.sri.com/pubs/files/tn036-duda71.pdf R. Fisher, S. Perkins, A. Walker and E. Wolfart Hough Transform, 2003 URL: http://homepages.inf.ed.ac.uk/rbf/HIPR2/hough.htm Wikipedia - Hough transform URL: http://en.wikipedia.org/wiki/Hough_transform