Skip to main content

Hough Line Transform in OpenCV

Introduction to Hough Transform

The Hough Transform is a popular technique to detect any shape, if you can represent that shape in a mathematical form. It can detect the shape even if it is broken or distorted a little bit. We will see how it works for a line using OpenCV.

To use OpenCV in Python, you need to install the openCV module using conda command.

conda install opencv

Now you can import OpenCV into your Python code.

import cv2 as cv
import numpy as np

We read the sudoku.png that store in your directory and convert it into gray scale. Next, we use cv.Canny() to detect the edge of the image.

For sudoku.png, you can choose any from Google.

img = cv.imread('path/to/sudoku.png')
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray,50,150,apertureSize = 3)

We use cv.HoughLines() function to perform Hough Transform. It returns array of line parameters (rho, theta) values.

  • edges : binary edge detected image input
  • 1 : rho value
  • np.pi/180 : theta
  • 200 : threshold
lines = cv.HoughLines(edges,1,np.pi/180,200)

Iterate through each (rho, theta) order paired and convert it back to the points on cartesian plane and draw the intersect line between two point on the input image. Finally, we save the output image to our preferred location.

for line in lines:
rho,theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cv.imwrite('houghlines3.jpg',img)
# cv.imshow("Lines",img)
# cv.waitKey(0)

Probabilistic Hough Transform

Probabilistic Hough Transform is an optimization of the Hough Transform we saw, it takes only a random subset of points which is sufficient for line detection.

In openCV we can perform the Probabilistic Hough Transform by using cv.HoughLinesP() function.

import cv2 as cv
import numpy as np
img = cv.imread('path/to/sudoku.png')
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray,50,150,apertureSize = 3)

cv.HoughLinesP() parameters if similar to cv.HoughLines() except it takes two new arguments.

  • minLineLength - Minimum length of line. Line segments shorter than this are rejected
  • maxLineGap - Maximum allowed gap between line segments to treat them as a single line
lines = cv.HoughLinesP(edges,1,np.pi/180,100,minLineLength=100,maxLineGap=10)

The cv.HoughLinesP() function returns the line's two endpoints, whereas the cv.HoughLines() function returns the rho, theta parameters. Then we iterate through each pair of endpoints and draw the line between two endpoints on the input image. Finally, we save our output image as a file.

for line in lines:
x1,y1,x2,y2 = line[0]
cv.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv.imwrite('houghlines5.jpg',img)
# cv.imshow("Lines",img)
# cv.waitKey(0)

Here are the tutorial for lane detection, one of the Hough Transform applications.

Additional Resources :

Acknowledgement : The content of this document has been adapted from the original Hough Line Transform.