Detecting lines in images: The Hough transform Frank Nielsen
[email protected] 2011 Many applications in computer vision and robotics require detecting efficiently lines in images. To give a simple example, cars nowadays have an integrated real-time lane mark recognition system (see Figure 1). We present a theoretically elegant solution to line detection in pictures that yields the computational efficient celebrated algorithm: The Hough transform.
1
Several line parameterizations
Let us consider the digital picture shown in Figure 2: a few lines sketched using a drawing software. A line L can be parameterized using two real parameters (a, b): L : y = ax + b.
(1)
For x = 0, we have y = b. That is, b is the intercept of the line with the vertical axis. For an increment ∆x = 1, we have ∆y = a(x + ∆x + b) − (ax + b) = a∆x. That is, coefficient a models the slope of the line. Thus equation 1 is the usual slope-intercept parameterization form of lines. We can also express the slope coefficient a using the angular representation: a = α
=
sin α , cos α arctan a, tan α =
(2) (3)
with α expressed in radians, so that the equation of the line becomes: L : y = x tan α + b. (We convert the radians into degrees by multiplying by the factor the slope-intercept line equation.
(4) 180 π .)
Figure 3 explicits the notation for
Figure 1: Lane mark recognition system for autonomous car driving and lane departure warning system. 1
Figure 2: A digital picture with a few rasterized line segments (synthetic image drawn by hand using the Paint program).
y
b
∆x α ∆y = a∆x = ∆x tan α
L
O
x
Figure 3: The slope-intercept representation of a line.
2
y
Lv
Lh L
ρ
θ O
x
Figure 4: The (ρ, θ) normal parameterization of lines that avoids the degeneracy problem of the slopeintercept representation for vertical lines. However, one major drawback is that this parameterization fails to model vertical line x = c for a given constant c (although we can have horizontal lines: y = c). To overcome this degeneracy problem, we could consider the generic line parameterization at the hear of the duality point/line in projective geometry: L : ax + by + c = 0.
(5)
But we introduced one more parameter. We may ask ourselves • Question 1: What are the range values of coefficients that yield lines visible in the digital picture of size [0, w − 1] × [0, h − 1]? • Question 2: How do we choose randomly coefficients to obtain a random line? A far better parameterization of lines is the normal parameterization that uses the radial decomposition of lines. Any line L can be parameterized by its distance ρ from the origin O = (0, 0), and the angle θ of the perpendicular projection of O onto L with the horizontal axis. Figure 4 illustrates the (ρ, θ) normal parameterization, also called the polar representation. Observe that with the (r, θ) coordinates, we can easily answer Question 1, and bound the parameter ranges as follows: √ • ρ: from 0 to w2 + h2 , the maximal distance from any image pixel to the origin, • θ: arbitrary, from 0 to 2π. To sample a few random lines, we also prefer the (ρ, θ)-parameterization. The code below shows how to draw a few random lines in an image: Listing 1: Drawing a few random lines using the normal representation public s t a t i c void randomLineImage ( i n t n , i n t w, i n t h , S t r i n g f i l e n a m e ) { double rho , t h e t a ; IMG img=new IMG(w, h ) ; img . background ( 2 5 5 , 2 5 5 , 2 5 5 ) ; // white
3
Figure 5: Randomly drawing a few lines using the (ρ, θ) normal parameterization.
y
b α ρ γ=
π 2
L
−θ θ
α O
α
x
θ Figure 6: Trigonometric identities used to recover the slope-intercept equation y = − cos sin θ + b = the polar parameters.
ρ sin θ
from
f o r ( i n t i =0; i =H[ i + 1 ] [ j −1]) ) { n b l i n e ++; rmax=Math . s q r t ( img . width ∗img . width+img . h e i g h t ∗img . h e i g h t ) ; r=rmax ∗ ( ( double ) i / ( double ) Rbins ) ; t =2.0∗Math . PI ∗ ( ( double ) j / ( double ) Tbins ) ; drawLine ( img , r , t ) ; System . out . p r i n t l n ( r+" "+t ) ; } }
The (ρ, θ) local maxima are retrieved as (ρ, θ) parameters as follows:
7
Figure 9: Detected lines in the Hough transform space via thresholding and local maxima retrieval. Here, we detected 9 lines. One line has been detected by three different local maximal bins in the quantized Hough transform space. 60.43644162723762 1.7671458676442586 63.38456073100531 1.7671458676442586 69.28079893854068 1.7426021750380885 72.22891804230838 1.7426021750380885 92.8657517686822 1.9389517158874505 110.55446639128833 5.473243451175968 179.835265329829 0.9572040116406401 325.76716096632964 1.1290098598838318 361.14459021154187 0.39269908169872414 Detected 9 line(s)
Note that the first three lines are basically indicating the same line, see Figure 9.
4
Detecting lines in digital photographs
To detect lines in digital pictures, we first compute a Sobel gradient mask at all pixel positions and threshold it. The remaining edge pixels, called edgels (for edge pixels for short), are then given as a binary image for voting in the Hough transform space. Figure 10 shows such a result obtained for a natural image of a building exhibiting long horizontal lines: The Hough transform succeeded to retrieve the long horizontal edges. Since the gradient provides also some local information on the normal vector of a line, we may use the gradient information to only vote on a selected θ-range instead of the full range [0, 2π). This allows some signification speed-up in practice. In general, the Hough transform may also detect spurious lines: That is, artifacts that come from the grid discretization or other routines such as edge detector filters, etc. Lines that are detected several times can be identified by performing coefficient clustering. Observe that we can also retrieve the thickness of lines from the roundness of the local extrema in the Hough transform space. The Hough algorithm works very well for retrieving the quadrangles of paintings captured in museums, etc. It is also often used in photogrammetry to estimate the vanishing points and the horizon line, or in robotics to detect perspective rectangles coinciding with indoor walls for self-localization.
5
Bibliographical notes
The basic principle of the Hough transform (collinear points as equivalent lines intersecting in the transformed space) was pioneered by Paul V. C. Hough in 1959 [6], and disclosed in a US patent #3,069,654 (1962). The method was primarily designed for detecting particle tracks in bubble chamber images, and the reported
8
(a)
(b)
(e)
(c)
(f)
Figure 10: Detecting straight lines in a digital picture (a): (b) Sobel edge filter, (c) thresholded Sobel edge into a binary image (black pixels are edgels). (d) Hough transform on (c), and (e) detected lines (corresponding to horizontal structures).
9
algorithm was an analog circuit. There were no explicit algebraic equations of lines reported in this seminal work. Rosenfeld [8] further described the principle with algebraic equations, and introduce the idea of using an array of counters to detect maxima. The normal line parameterization (ρ, θ) of lines to avoid degeneracy was introduced in [4] (1972), motivated by an indoor robotic application of the world’s first mobile intelligent robot: Shakey. To further speed up the algorithm, the use of gradient information [7] was suggested in 1973. The Hough transform principle can be extended to detect arbitrary parametric shapes [1, 4] like circles, ellipses, etc. Since the Hough transform relies on a “good” discretization of the parameter space and an effective maxima retrieving procedure, countless variants have been proposed [5]. Interestingly, there are a few methods that allow one to detect partial ellipses in images efficiently using a 1D parametric space [3, 2]. This is all the more important in computer vision where circular parts of objects are imaged under perspective projection as ellipses. Nowadays, the Hough transform is considered as one of the most widely used procedure in computer vision.
6
Source code
The complete source code is given in program: HoughTransformDetectLines.java Compiling and running the program, we get at the console output the following verbose information: Hough Transform for Line detection. Nielsen Frank 2011. First, draw random lines... Then detect lines on a synthetic image... Image=testl Threshold percentage:0.3 60.43644162723762 1.7671458676442586 63.38456073100531 1.7671458676442586 69.28079893854068 1.7426021750380885 72.22891804230838 1.7426021750380885 92.8657517686822 1.9389517158874505 110.55446639128833 5.473243451175968 179.835265329829 0.9572040116406401 325.76716096632964 1.1290098598838318 361.14459021154187 0.39269908169872414 Detected 9 line(s) Finally detect lines on a natural building thresholded Sobel binary image... Image=Sobel-polytechnique-thresholded Threshold percentage:0.5 196.875 1.5707963267948966 210.9375 1.5707963267948966 221.875 1.5707963267948966 243.75 1.5707963267948966 Detected 4 line(s) Completed!
References [1] D.H. Ballard. Generalizing the Hough transform to detect arbitrary shapes. 13(2):111 – 122, 1981.
Pattern Recognition,
[2] A. Y. S. Chia, S. Rahardja, D. Rajan, and M. K. Leung. A split and merge based ellipse detector with self-correcting capability. IEEE Transactions on Image Processing, 20(7):1991–2006, 2011.
10
[3] Alex Y. S. Chia, Maylor K. H. Leung, How-Lung Eng, and Susanto Rahardja. Ellipse detection with Hough transform in one dimensional parametric space. In Proc. International Conference on Image Processing, volume 5, pages 333–336, 2007. [4] R. O. Duda and P. E. Hart. Use of the Hough transformation to detect lines and curves in pictures. Communication of the ACM, 15:11–15, January 1972. [5] L. A. F. Fernandes and M. M. Oliveira. Real-time line detection through an improved hough transform voting scheme. Pattern Recognition, 41:299–314, January 2008. [6] P. E. Hart. How the Hough transform was invented [DSP history]. IEEE Signal Processing Magazine, 26(6):18–22, October 2009. [7] F. O’Gorman and M. B. Clowes. Finding picture edges through collinearity of feature points. In Proceedings 3rd International Joint Conference on Artificial Intelligence, pages 543–555. Morgan Kaufmann Publishers Inc., 1973. [8] A. Rosenfeld. Picture processing by computer. ACM Computing Survey, 1:147–176, September 1969.
11