Road Line Extraction using Hough Transformation

Hough Transform is a popular technique for detecting lines in an image. Here's how you can use it to extract road lines using OpenCV:

First, you need to read in your image and convert it to grayscale. You can use any programming language or library of your choice to achieve this step.

Next, you need to apply some preprocessing steps to enhance the features of the image, which will help in detecting the road lines more accurately. You can use techniques like Gaussian blur, thresholding, and edge detection to achieve this step.

Once you have the preprocessed image, you can apply the Hough Transform algorithm to detect road lines. The Hough Transform works by first creating a Hough space, which is a 2D array with the same dimensions as the input image. For each edge point in the image, a set of lines that pass through that point are voted on in the Hough space. The lines with the most votes are then considered to be the detected road lines.

To detect road lines using the Hough Transform, you need to set suitable parameters for the cv2.HoughLinesP() method. This method uses a probabilistic approach to detect lines. You also need to set the rho, theta, and threshold parameters to appropriate values. These parameters control the resolution of the Hough space, the angle resolution, and the threshold for line detection.

Here's some sample Python code to perform road line extraction using the Hough Transform algorithm using cv2.HoughLinesP() function:

Code 1-1 Detecting road lines using 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
27
import cv2
import numpy as np
dir = r'D:\\img\\'
imgname = 'road1.jpg'
path = dir  + imgname
imgOriginal = cv2.imread(path, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(imgOriginal,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3),0) 
sobel_edges = cv2.Sobel(gray,-1,1,0)
edges = cv2.convertScaleAbs(sobel_edges,alpha=1, beta=0)
thresh,edges =  cv2.threshold(edges,50,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)
edges = cv2.dilate(edges,(3,3))
rho = 1
theta = np.pi/180
threshold = 20
minLineLength = 100
maxLineGap = 20
lines = cv2.HoughLinesP(image=edges_filter,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),4)      
    cv2.imshow("Hough Lines", imgOriginal)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print('No Lines found')

In this code, we first apply Gaussian blur (Line #8), Sobel edge detection to detect only vertical lines (Line#9), then we convert the any negative pixel values to positive by applying convertScaleAbs() function. Thresholding algorithm is applied (Line#11), to get strong potential vertical edges. To overcome any gaps between lines, we applied dilation (Line#12). We then use the cv2.HoughLinesP() function to detect road lines in the edge image. We set the rho parameter to 1, which means that the resolution of the Hough space is 1 pixel. We set the theta parameter to np.pi/180, which means that the angle resolution is 1 degree. We set the threshold parameter to 20, which is the minimum number of votes (or edge pixels) needed to consider a line for detection. We set the minLineLength parameter to 100, which is the minimum length of a line. We set the maxLineGap parameter to 1, which is the maximum gap allowed between line segments to treat them as a single line.

Finally, we draw the detected road lines on the original image using the cv2.line() function and display the result using the cv2.imshow function.

Note that the Hough Transform algorithm is sensitive to parameter settings and may require some tuning to achieve optimal results. Additionally, the Hough Transform algorithm is not suitable for detecting curved lines, such as those found in some roads, so other techniques may need to be used in those cases.

Overall, the Hough Transform algorithm is a useful tool for road line extraction and can be easily implemented using the OpenCV library. A sample output for the above code is shown in the Figure 1.

No image
Figure 1: Road line extraction using probabilistic Hough Transformation.
It is worth mentioning that here we employed Sobel vertical line detector to help detect vertical lines, as we know that the road lines are almost vertical. You can also use the Canny edge detector. However, this detector detects edges in all directions. Whichever gives you better results can be used for this purpose.

OpenCV Tutorials