1 Hough Line Transform {#tutorial_hough_lines}
4 @prev_tutorial{tutorial_canny_detector}
5 @next_tutorial{tutorial_hough_circle}
10 In this tutorial you will learn how to:
12 - Use the OpenCV functions **HoughLines()** and **HoughLinesP()** to detect lines in an
18 @note The explanation below belongs to the book **Learning OpenCV** by Bradski and Kaehler.
23 -# The Hough Line Transform is a transform used to detect straight lines.
24 -# To apply the Transform, first an edge detection pre-processing is desirable.
28 -# As you know, a line in the image space can be expressed with two variables. For example:
30 -# In the **Cartesian coordinate system:** Parameters: \f$(m,b)\f$.
31 -# In the **Polar coordinate system:** Parameters: \f$(r,\theta)\f$
33 ![](images/Hough_Lines_Tutorial_Theory_0.jpg)
35 For Hough Transforms, we will express lines in the *Polar system*. Hence, a line equation can be
38 \f[y = \left ( -\dfrac{\cos \theta}{\sin \theta} \right ) x + \left ( \dfrac{r}{\sin \theta} \right )\f]
40 Arranging the terms: \f$r = x \cos \theta + y \sin \theta\f$
42 -# In general for each point \f$(x_{0}, y_{0})\f$, we can define the family of lines that goes through
45 \f[r_{\theta} = x_{0} \cdot \cos \theta + y_{0} \cdot \sin \theta\f]
47 Meaning that each pair \f$(r_{\theta},\theta)\f$ represents each line that passes by
50 -# If for a given \f$(x_{0}, y_{0})\f$ we plot the family of lines that goes through it, we get a
51 sinusoid. For instance, for \f$x_{0} = 8\f$ and \f$y_{0} = 6\f$ we get the following plot (in a plane
52 \f$\theta\f$ - \f$r\f$):
54 ![](images/Hough_Lines_Tutorial_Theory_1.jpg)
56 We consider only points such that \f$r > 0\f$ and \f$0< \theta < 2 \pi\f$.
58 -# We can do the same operation above for all the points in an image. If the curves of two
59 different points intersect in the plane \f$\theta\f$ - \f$r\f$, that means that both points belong to a
60 same line. For instance, following with the example above and drawing the plot for two more
61 points: \f$x_{1} = 4\f$, \f$y_{1} = 9\f$ and \f$x_{2} = 12\f$, \f$y_{2} = 3\f$, we get:
63 ![](images/Hough_Lines_Tutorial_Theory_2.jpg)
65 The three plots intersect in one single point \f$(0.925, 9.6)\f$, these coordinates are the
66 parameters (\f$\theta, r\f$) or the line in which \f$(x_{0}, y_{0})\f$, \f$(x_{1}, y_{1})\f$ and
67 \f$(x_{2}, y_{2})\f$ lay.
69 -# What does all the stuff above mean? It means that in general, a line can be *detected* by
70 finding the number of intersections between curves.The more curves intersecting means that the
71 line represented by that intersection have more points. In general, we can define a *threshold*
72 of the minimum number of intersections needed to *detect* a line.
73 -# This is what the Hough Line Transform does. It keeps track of the intersection between curves of
74 every point in the image. If the number of intersections is above some *threshold*, then it
75 declares it as a line with the parameters \f$(\theta, r_{\theta})\f$ of the intersection point.
77 ### Standard and Probabilistic Hough Line Transform
79 OpenCV implements two kind of Hough Line Transforms:
81 a. **The Standard Hough Transform**
83 - It consists in pretty much what we just explained in the previous section. It gives you as
84 result a vector of couples \f$(\theta, r_{\theta})\f$
85 - In OpenCV it is implemented with the function **HoughLines()**
87 b. **The Probabilistic Hough Line Transform**
89 - A more efficient implementation of the Hough Line Transform. It gives as output the extremes
90 of the detected lines \f$(x_{0}, y_{0}, x_{1}, y_{1})\f$
91 - In OpenCV it is implemented with the function **HoughLinesP()**
93 ### What does this program do?
95 - Applies a *Standard Hough Line Transform* and a *Probabilistic Line Transform*.
96 - Display the original image and the detected line in three windows.
102 The sample code that we will explain can be downloaded from
103 [here](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgTrans/houghlines.cpp).
104 A slightly fancier version (which shows both Hough standard and probabilistic
105 with trackbars for changing the threshold values) can be found
106 [here](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgTrans/HoughLines_Demo.cpp).
107 @include samples/cpp/tutorial_code/ImgTrans/houghlines.cpp
111 The sample code that we will explain can be downloaded from
112 [here](https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java).
113 @include samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java
117 The sample code that we will explain can be downloaded from
118 [here](https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py).
119 @include samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py
128 @snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp load
132 @snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java load
136 @snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py load
139 #### Detect the edges of the image by using a Canny detector:
142 @snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp edge_detection
146 @snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java edge_detection
150 @snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py edge_detection
153 Now we will apply the Hough Line Transform. We will explain how to use both OpenCV functions
154 available for this purpose.
156 #### Standard Hough Line Transform:
157 First, you apply the Transform:
160 @snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp hough_lines
164 @snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java hough_lines
168 @snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py hough_lines
171 - with the following arguments:
173 - *dst*: Output of the edge detector. It should be a grayscale image (although in fact it
175 - *lines*: A vector that will store the parameters \f$(r,\theta)\f$ of the detected lines
176 - *rho* : The resolution of the parameter \f$r\f$ in pixels. We use **1** pixel.
177 - *theta*: The resolution of the parameter \f$\theta\f$ in radians. We use **1 degree**
179 - *threshold*: The minimum number of intersections to "*detect*" a line
180 - *srn* and *stn*: Default parameters to zero. Check OpenCV reference for more info.
182 And then you display the result by drawing the lines.
184 @snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp draw_lines
188 @snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java draw_lines
192 @snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py draw_lines
195 #### Probabilistic Hough Line Transform
196 First you apply the transform:
199 @snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp hough_lines_p
203 @snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java hough_lines_p
207 @snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py hough_lines_p
210 - with the arguments:
212 - *dst*: Output of the edge detector. It should be a grayscale image (although in fact it
214 - *lines*: A vector that will store the parameters
215 \f$(x_{start}, y_{start}, x_{end}, y_{end})\f$ of the detected lines
216 - *rho* : The resolution of the parameter \f$r\f$ in pixels. We use **1** pixel.
217 - *theta*: The resolution of the parameter \f$\theta\f$ in radians. We use **1 degree**
219 - *threshold*: The minimum number of intersections to "*detect*" a line
220 - *minLinLength*: The minimum number of points that can form a line. Lines with less than
221 this number of points are disregarded.
222 - *maxLineGap*: The maximum gap between two points to be considered in the same line.
224 And then you display the result by drawing the lines.
227 @snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp draw_lines_p
231 @snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java draw_lines_p
235 @snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py draw_lines_p
238 #### Display the original image and the detected lines:
241 @snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp imshow
245 @snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java imshow
249 @snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py imshow
252 #### Wait until the user exits the program
255 @snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp exit
259 @snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java exit
263 @snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py exit
270 The results below are obtained using the slightly fancier version we mentioned in the *Code*
271 section. It still implements the same stuff as above, only adding the Trackbar for the
274 Using an input image such as a [sudoku image](https://raw.githubusercontent.com/opencv/opencv/master/samples/data/sudoku.png).
275 We get the following result by using the Standard Hough Line Transform:
276 ![](images/hough_lines_result1.png)
277 And by using the Probabilistic Hough Line Transform:
278 ![](images/hough_lines_result2.png)
280 You may observe that the number of lines detected vary while you change the *threshold*. The
281 explanation is sort of evident: If you establish a higher threshold, fewer lines will be detected
282 (since you will need more points to declare a line detected).