Probabilistic Hough Transform for Line Detection

In the previous tutorial we described Hough Transformation for Line Detection. In this tutorial we focus on how to use the Probabalistic Hough Transformation.

The Probabilistic Hough Transform is a variant of the Hough Transform that is used to detect lines in an image. It was developed to overcome some of the limitations of the traditional Hough Transform, which can be computationally expensive when dealing with large images or when trying to detect more complex shapes such as circles.

The Probabilistic Hough Transform works by randomly selecting a subset of the edge pixels in the image and then fitting lines to those pixels. This process is repeated multiple times, with the hope that each iteration will detect additional pixels that belong to the same line. Eventually, a consensus set of pixels is reached, and a line is fitted to that set using least-squares regression.

The main advantage of the Probabilistic Hough Transform is that it can be much faster than the traditional Hough Transform, especially when dealing with complex shapes. However, it may not be as accurate as the traditional Hough Transform, especially if the image contains many noisy or spurious edge pixels. In addition, the Probabilistic Hough Transform is limited to detecting straight lines and cannot be used to detect more complex shapes such as circles.

To use the Probabilistic Hough Transform in OpenCV, you can use the cv2.HoughLinesP() function. This function takes several parameters, including the image to be processed, the minimum line length and gap size, and the threshold value used to determine which edge pixels are considered part of a line. The function returns a list of line segments in the form of $(x_1,y_1,x_2,y_2) $ coordinates.

Here's an example code snippet that demonstrates how to use cv2.HoughLinesP() to detect lines in an image using the Probabilistic Hough Transform:

Code 1-1 Probabilistic Hough Line Transform
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import cv2
import numpy as np
dir = r'D:\\img\\'
imgname = 'shape.jpg'
path = dir  + imgname
imgOriginal = cv2.imread(path, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(imgOriginal,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5),0) 
edges = cv2.Canny(blur, 50, 200,apertureSize=3)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,9))
edges =  cv2.dilate(edges, kernel)
rho = 1
theta = np.pi/180
threshold = 200;
minLineLength = 80
maxLineGap = 10
lines = cv2.HoughLinesP(image=edges,rho=rho,theta=theta,threshold=threshold,minLineLength=minLineLength,maxLineGap=maxLineGap)
if(lines is not None):
    for line in lines:
        x1,y1,x2,y2 = line[0]
        cv2.line(imgOriginal,(x1,y1),(x2,y2),(0,255,0),2)      
    cv2.imshow("Hough Lines", imgOriginal)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print('No Lines found')

In this example, we first load an image of lines, convert it to grayscale, and apply Canny edge detection to obtain the edges in the image. We then call cv2.HoughLinesP() with the appropriate parameters to detect the lines in the image. The detected lines are returned as a list of line segments in the form of $(x_1,y_1,x_2,y_2)$ coordinates. We then loop through the list and draw lines on the original image using cv2.line(). Finally, we display the result using cv2.imshow(). Note that the values of rho, theta, threshold, minLineLength, and maxLineGap may need to be adjusted depending on the specific image and the lines you are trying to detect.The parameters we defines from Line 12- 16 are adjustable. According to your needs.

A sample output for cv2.HoughLinesP() is shown in Figure 1. Here you can see the online lines satisfying the given parameters are shown. For instance, it selects those lines which has at least 200 pixels on it (threshold=200).

No image
Figure 1: Output of Probabilistic Hough Transform for a sample image.

OpenCV Tutorials