Hough Lines Transform
An example using the Hough line detector.
This program demonstrates line finding with the Hough transform. We show how to use the OpenCV functions cv.HoughLines and cv.HoughLinesP to detect lines in an image.
Sources:
- https://github.com/opencv/opencv/blob/3.2.0/samples/cpp/houghlines.cpp
- https://github.com/opencv/opencv/blob/3.2.0/samples/python/houghlines.py
- https://docs.opencv.org/3.2.0/d9/db0/tutorial_hough_lines.html
- https://github.com/opencv/opencv/blob/3.2.0/samples/cpp/tutorial_code/ImgTrans/HoughLines_Demo.cpp
- https://docs.opencv.org/3.2.0/d6/d10/tutorial_py_houghlines.html
Contents
Theory
The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.
The Hough Line Transform is a transform used to detect straight lines. To apply the Transform, first an edge detection pre-processing is desirable.
As you know, a line in the image space can be expressed with two variables. For example:
- In the Cartesian coordinate system: Parameters: .
- In the Polar coordinate system: Parameters:
For Hough Transforms, we will express lines in the Polar system. Hence, a line equation can be written as:
Arranging the terms:
In general for each point , we can define the family of lines that goes through that point as:
Meaning that each pair represents each line that passes by .
If for a given we plot the family of lines that goes through it, we get a sinusoid. For instance, for and we get the following plot (in a plane - ):
We consider only points such that and .
We can do the same operation above for all the points in an image. If the curves of two different points intersect in the plane - , that means that both points belong to a same line. For instance, following with the example above and drawing the plot for two more points: , and , , we get:
The three plots intersect in one single point , these coordinates are the parameters () or the line in which , and lay.
What does all the stuff above mean? It means that in general, a line can be detected by finding the number of intersections between curves. The more curves intersecting means that the line represented by that intersection have more points. In general, we can define a threshold of the minimum number of intersections needed to detect a line.
This is what the Hough Line Transform does. It keeps track of the intersection between curves of every point in the image. If the number of intersections is above some threshold, then it declares it as a line with the parameters of the intersection point.
Standard and Probabilistic Hough Line Transform
OpenCV implements two kind of Hough Line Transforms:
1) The Standard Hough Transform
- It consists in pretty much what we just explained in the previous section. It gives you as result a vector of couples
- In OpenCV it is implemented with the function cv.HoughLines
2) The Probabilistic Hough Line Transform
- A more efficient implementation of the Hough Line Transform. It gives as output the extremes of the detected lines
- In OpenCV it is implemented with the function cv.HoughLinesP
Code
This program:
- Loads an image
- Applies either a Standard Hough Line Transform or a Probabilistic Line Transform.
- Display the original image and the detected line in two windows.
You may observe that the number of lines detected vary while you change the threshold. The explanation is sort of evident: If you establish a higher threshold, fewer lines will be detected (since you will need more points to declare a line detected).
Input image
if true fname = fullfile(mexopencv.root(), 'test', 'sudoku.jpg'); thresh = 200; threshP = 100; minlen = 100; else fname = fullfile(mexopencv.root(), 'test', 'pic1.png'); thresh = 85; threshP = 50; minlen = 50; end src = cv.imread(fname, 'Color',true);
Edge Detection
gray = cv.cvtColor(src, 'RGB2GRAY'); edges = cv.Canny(gray, [50, 150], 'ApertureSize',3); imshow(edges), title('Edges')
HoughLines: Standard Hough Line Transform
tic lines = cv.HoughLines(edges, 'Rho',1, 'Theta',pi/180, 'Threshold',thresh); toc
Elapsed time is 0.040363 seconds.
draw the lines, and display the result
lines = cat(1, lines{:}); rho = lines(:,1); theta = lines(:,2); a = cos(theta); b = sin(theta); x0 = a.*rho; y0 = b.*rho; pt1 = round([x0 + 1000*(-b), y0 + 1000*(a)]); pt2 = round([x0 - 1000*(-b), y0 - 1000*(a)]); out = cv.line(src, pt1, pt2, ... 'Color',[0 255 0], 'Thickness',2, 'LineType','AA'); figure, imshow(out), title('Detected Lines')
HoughLinesP: Probabilistic Hough Line Transform
tic linesP = cv.HoughLinesP(edges, 'Rho',1, 'Theta',pi/180, ... 'Threshold',threshP, 'MinLineLength',minlen, 'MaxLineGap',10); toc
Elapsed time is 0.048381 seconds.
draw the line segments, and display the result
linesP = cat(1, linesP{:}); outP = cv.line(src, linesP(:,1:2), linesP(:,3:4), ... 'Color',[0 255 0], 'Thickness',2, 'LineType','AA'); figure, imshow(outP), title('Detected Line Segments')