Detect Lines in Particular Direction using Probabilistic Hough Transform

In our previous post, Probabilistic Hough Transform for Line Detection, we showed detecting lines in all direction. But, in this post let us find the lines in a particular direction.

Probabilistic Hough Transform is a popular technique used for detecting lines in an image. Here's how you can use it to detect lines in a particular direction:

  1. 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.
  2. Next, you need to perform edge detection on the grayscale image using an edge detection algorithm such as the Canny edge detector. This will give you an image with edges highlighted.
  3. Once you have the edge image, you can apply the Probabilistic Hough Transform algorithm to detect lines. The Probabilistic Hough Transform works by randomly sampling points along the edges and looking for pairs of points that have a similar distance and orientation to a line. It then fits a line to these points.
  4. To detect lines in a particular direction, you need to set the theta parameter of the Probabilistic Hough Transform to the angle of the direction you want to detect. For example, if you want to detect lines that are vertical, you would set theta to PI Radians or 90 degrees.
  5. You can also set the minLineLength and maxLineGap parameters of the Probabilistic Hough Transform to control the minimum length of the lines you want to detect and the maximum gap between segments of the same line.

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

Code 1-1 Detecting only vertical 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
28
import cv2
import numpy as np
import math
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
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,0,255),2)
        print(theta)
    cv2.imshow("Hough Lines", imgOriginal)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print('No Lines found')
Notice the Line#14, we set the theta to np.pi radians (180 degrees) to detect the vertical lines only. The rest of the parameters such as minLineLength and maxLineGap to control the minimum length and maximum gap between segments of the same line. The detected lines are drawn in red on the original image. A sample output is shown in Figure 1.
No image
Figure 1: Detecting only vertical lines using Probabilistic Hough Transform.
If you are interested in detecting line in both hoironzal and vertical lines then you need to set the Line#14 as np.pi/2. Now we have only two range of theta, 180 and 90 degrees. A sample output is shown below.
No image
Figure 2: Detecting only horizontal and vertical lines using Probabilistic Hough Transform.
You can see in the above figure that not only vertical lines are detected, but also horizontal lines are also detected. If we want horizontal, vertical, and diagonal lines, then we have to change Line#14 to np.pi/4.

OpenCV Tutorials