Implementation of Simple Hough Line Detection Algorithm in Python.
This is based on paper Use of the Hough Transformation To Detect Lines and Curves in Pictures by Richard O. Duda and Peter E. Hart.
A line can be represented in Cartesian Space by the following equation,
y = m * x + b where, m = gradient / slope of line.
b = y-intercept.
So given some set of points in binary image we can find lines connecting these points in Image Space. Lines in Cartesian Image Space will intersect at a point in m - b Parameter Space as shown in the figure.
But this fails for vertical lines, i.e. m = 0. So we use Hough Space instead of m - b Parameter Space.
A line can be represented in polar form as show in the figure,
The line from origin with distance rho has a slope of sin(theta) / cos(theta). The line of interest which is perpendicular to it will have negative reciprocal slope i.e. -cos(theta) / sin(theta).
The y-intercept of line is sin(theta) = rho / b. Thus inserting m = -cos(theta) / sin(theta) and b = rho / sin(theta) in the equation of line we get,
rho = x * cos(theta) + y * sin(theta) where, rho = distance from origin to line.
theta = angle from origin to line.
A Hough space is rho - theta space, Lines in Cartesian Image Space will intersect at a point in rho - theta Parameter Space.
''' python find_hough_lines.py ./images/ex1.png --num_rho 180 --num_theta 180 --bin_threshold 150 '''
The script requires one positional argument and 3 optional parameters:
- image_path - Complete path to the image file for line detection.
- num_rho - No. of Rhos in Rho-Theta Hough Space. Default 180.
- num_theta - No. of Thetas in Rho-Theta Hough Space. Default 180.
- bin_threshold - bin / vote values above or below the bin_threshold are shortlisted as lines. Default 150.
The output of the script would be two files:
- lines.txt - File containing list of lines in format (rho,theta,x1,y1,x2,y2)
- line_img.png - Image with the Lines drawn in Green color.
Sample Input Image | Sample Hough Space | Sample Output Image |
---|---|---|
Playing with the three input parameters is required to obtain desired lines in different images so its not adaptive. Also the code can be little more optimized.