Skip to content

Commit 9a2317e

Browse files
committed
Tutorial Hough Lines
1 parent d99ced6 commit 9a2317e

File tree

8 files changed

+419
-159
lines changed

8 files changed

+419
-159
lines changed

doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.markdown

Lines changed: 153 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
Hough Line Transform {#tutorial_hough_lines}
22
====================
33

4+
@prev_tutorial{tutorial_canny_detector}
5+
@next_tutorial{tutorial_hough_circle}
6+
47
Goal
58
----
69

710
In this tutorial you will learn how to:
811

9-
- Use the OpenCV functions @ref cv::HoughLines and @ref cv::HoughLinesP to detect lines in an
12+
- Use the OpenCV functions **HoughLines()** and **HoughLinesP()** to detect lines in an
1013
image.
1114

1215
Theory
@@ -79,54 +82,93 @@ a. **The Standard Hough Transform**
7982

8083
- It consists in pretty much what we just explained in the previous section. It gives you as
8184
result a vector of couples \f$(\theta, r_{\theta})\f$
82-
- In OpenCV it is implemented with the function @ref cv::HoughLines
85+
- In OpenCV it is implemented with the function **HoughLines()**
8386

8487
b. **The Probabilistic Hough Line Transform**
8588

8689
- A more efficient implementation of the Hough Line Transform. It gives as output the extremes
8790
of the detected lines \f$(x_{0}, y_{0}, x_{1}, y_{1})\f$
88-
- In OpenCV it is implemented with the function @ref cv::HoughLinesP
91+
- In OpenCV it is implemented with the function **HoughLinesP()**
92+
93+
### What does this program do?
94+
- Loads an image
95+
- Applies a *Standard Hough Line Transform* and a *Probabilistic Line Transform*.
96+
- Display the original image and the detected line in three windows.
8997

9098
Code
9199
----
92100

93-
-# **What does this program do?**
94-
- Loads an image
95-
- Applies either a *Standard Hough Line Transform* or a *Probabilistic Line Transform*.
96-
- Display the original image and the detected line in two windows.
97-
98-
-# The sample code that we will explain can be downloaded from [here](https://github.com/opencv/opencv/tree/master/samples/cpp/houghlines.cpp). A slightly fancier version
99-
(which shows both Hough standard and probabilistic with trackbars for changing the threshold
100-
values) can be found [here](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/HoughLines_Demo.cpp).
101-
@include samples/cpp/houghlines.cpp
101+
@add_toggle_cpp
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
108+
@end_toggle
109+
110+
@add_toggle_java
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
114+
@end_toggle
115+
116+
@add_toggle_python
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
120+
@end_toggle
102121

103122
Explanation
104123
-----------
105124

106-
-# Load an image
107-
@code{.cpp}
108-
Mat src = imread(filename, 0);
109-
if(src.empty())
110-
{
111-
help();
112-
cout << "can not open " << filename << endl;
113-
return -1;
114-
}
115-
@endcode
116-
-# Detect the edges of the image by using a Canny detector
117-
@code{.cpp}
118-
Canny(src, dst, 50, 200, 3);
119-
@endcode
120-
Now we will apply the Hough Line Transform. We will explain how to use both OpenCV functions
121-
available for this purpose:
122-
123-
-# **Standard Hough Line Transform**
124-
-# First, you apply the Transform:
125-
@code{.cpp}
126-
vector<Vec2f> lines;
127-
HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
128-
@endcode
129-
with the following arguments:
125+
#### Load an image:
126+
127+
@add_toggle_cpp
128+
@snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp load
129+
@end_toggle
130+
131+
@add_toggle_java
132+
@snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java load
133+
@end_toggle
134+
135+
@add_toggle_python
136+
@snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py load
137+
@end_toggle
138+
139+
#### Detect the edges of the image by using a Canny detector:
140+
141+
@add_toggle_cpp
142+
@snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp edge_detection
143+
@end_toggle
144+
145+
@add_toggle_java
146+
@snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java edge_detection
147+
@end_toggle
148+
149+
@add_toggle_python
150+
@snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py edge_detection
151+
@end_toggle
152+
153+
Now we will apply the Hough Line Transform. We will explain how to use both OpenCV functions
154+
available for this purpose.
155+
156+
#### Standard Hough Line Transform:
157+
First, you apply the Transform:
158+
159+
@add_toggle_cpp
160+
@snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp hough_lines
161+
@end_toggle
162+
163+
@add_toggle_java
164+
@snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java hough_lines
165+
@end_toggle
166+
167+
@add_toggle_python
168+
@snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py hough_lines
169+
@end_toggle
170+
171+
- with the following arguments:
130172

131173
- *dst*: Output of the edge detector. It should be a grayscale image (although in fact it
132174
is a binary one)
@@ -137,28 +179,35 @@ Explanation
137179
- *threshold*: The minimum number of intersections to "*detect*" a line
138180
- *srn* and *stn*: Default parameters to zero. Check OpenCV reference for more info.
139181

140-
-# And then you display the result by drawing the lines.
141-
@code{.cpp}
142-
for( size_t i = 0; i < lines.size(); i++ )
143-
{
144-
float rho = lines[i][0], theta = lines[i][1];
145-
Point pt1, pt2;
146-
double a = cos(theta), b = sin(theta);
147-
double x0 = a*rho, y0 = b*rho;
148-
pt1.x = cvRound(x0 + 1000*(-b));
149-
pt1.y = cvRound(y0 + 1000*(a));
150-
pt2.x = cvRound(x0 - 1000*(-b));
151-
pt2.y = cvRound(y0 - 1000*(a));
152-
line( cdst, pt1, pt2, Scalar(0,0,255), 3, LINE_AA);
153-
}
154-
@endcode
155-
-# **Probabilistic Hough Line Transform**
156-
-# First you apply the transform:
157-
@code{.cpp}
158-
vector<Vec4i> lines;
159-
HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
160-
@endcode
161-
with the arguments:
182+
And then you display the result by drawing the lines.
183+
@add_toggle_cpp
184+
@snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp draw_lines
185+
@end_toggle
186+
187+
@add_toggle_java
188+
@snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java draw_lines
189+
@end_toggle
190+
191+
@add_toggle_python
192+
@snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py draw_lines
193+
@end_toggle
194+
195+
#### Probabilistic Hough Line Transform
196+
First you apply the transform:
197+
198+
@add_toggle_cpp
199+
@snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp hough_lines_p
200+
@end_toggle
201+
202+
@add_toggle_java
203+
@snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java hough_lines_p
204+
@end_toggle
205+
206+
@add_toggle_python
207+
@snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py hough_lines_p
208+
@end_toggle
209+
210+
- with the arguments:
162211

163212
- *dst*: Output of the edge detector. It should be a grayscale image (although in fact it
164213
is a binary one)
@@ -172,23 +221,47 @@ Explanation
172221
this number of points are disregarded.
173222
- *maxLineGap*: The maximum gap between two points to be considered in the same line.
174223

175-
-# And then you display the result by drawing the lines.
176-
@code{.cpp}
177-
for( size_t i = 0; i < lines.size(); i++ )
178-
{
179-
Vec4i l = lines[i];
180-
line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA);
181-
}
182-
@endcode
183-
-# Display the original image and the detected lines:
184-
@code{.cpp}
185-
imshow("source", src);
186-
imshow("detected lines", cdst);
187-
@endcode
188-
-# Wait until the user exits the program
189-
@code{.cpp}
190-
waitKey();
191-
@endcode
224+
And then you display the result by drawing the lines.
225+
226+
@add_toggle_cpp
227+
@snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp draw_lines_p
228+
@end_toggle
229+
230+
@add_toggle_java
231+
@snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java draw_lines_p
232+
@end_toggle
233+
234+
@add_toggle_python
235+
@snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py draw_lines_p
236+
@end_toggle
237+
238+
#### Display the original image and the detected lines:
239+
240+
@add_toggle_cpp
241+
@snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp imshow
242+
@end_toggle
243+
244+
@add_toggle_java
245+
@snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java imshow
246+
@end_toggle
247+
248+
@add_toggle_python
249+
@snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py imshow
250+
@end_toggle
251+
252+
#### Wait until the user exits the program
253+
254+
@add_toggle_cpp
255+
@snippet samples/cpp/tutorial_code/ImgTrans/houghlines.cpp exit
256+
@end_toggle
257+
258+
@add_toggle_java
259+
@snippet samples/java/tutorial_code/ImgTrans/HoughLine/HoughLines.java exit
260+
@end_toggle
261+
262+
@add_toggle_python
263+
@snippet samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py exit
264+
@end_toggle
192265

193266
Result
194267
------
@@ -198,13 +271,11 @@ Result
198271
section. It still implements the same stuff as above, only adding the Trackbar for the
199272
Threshold.
200273

201-
Using an input image such as:
202-
203-
![](images/Hough_Lines_Tutorial_Original_Image.jpg)
204-
205-
We get the following result by using the Probabilistic Hough Line Transform:
206-
207-
![](images/Hough_Lines_Tutorial_Result.jpg)
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)
208279

209280
You may observe that the number of lines detected vary while you change the *threshold*. The
210281
explanation is sort of evident: If you establish a higher threshold, fewer lines will be detected
Loading
Loading

doc/tutorials/imgproc/table_of_content_imgproc.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ In this section you will learn about the image processing (manipulation) functio
125125

126126
- @subpage tutorial_hough_lines
127127

128+
*Languages:* C++, Java, Python
129+
128130
*Compatibility:* \> OpenCV 2.0
129131

130132
*Author:* Ana Huamán

samples/cpp/houghlines.cpp

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)