In this Python OpenCV article we are going to talk about Line Detection with HoughLines algorithm. first of all let’s talk about Line Detection.
Python OpenCV Tutorial: Line Detection With HoughLines Algorithm
Line detection has it own technique that is called the Hough transform, it was invented by Richard Duda and Peter Hart, who extended the work done by Paul Hough in the early 1960s. so now we are using HoughLines and HoughLinesP for detecting lines so the difference is that. HoughLines uses the standard HoughTransForm and HoughLinesP probabilistic Hough Transform.
So now this is the complete code for Line Detection In Python OpenCV With HoughLines
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 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import cv2 import numpy as np # Read image image1 = cv2.imread('dave2.jpg') # Convert image to grayscale gray = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) # Apply Canny edge detection canimg = cv2.Canny(gray, 50, 200) # Detect lines using Hough Transform # The HoughLines function detects straight # lines in the Canny edge-detected image. # It returns a list of lines, where # each line is represented by rho # and theta values. lines = cv2.HoughLines(canimg, 1, np.pi/180.0, 120, np.array([])) # Iterate through detected lines 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)) # Draw lines on the original image cv2.line(image1, (x1, y1), (x2, y2), (0, 0, 255), 2) # Display the images with detected lines cv2.imshow('Lines Detected', image1) cv2.imshow("Canny Detection", canimg) cv2.waitKey(0) cv2.destroyAllWindows() |
So at the top these are our imports that we are going to use in this article.
1 2 3 |
import cv2 import numpy as np import math |
You need to have an image in your directory, iam using this image.
In here we have loaded our image and we have converted the image to gray scale.
1 2 |
image1 = cv2.imread('dave2.jpg') gray=cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY) |
Also in this line of code we have created our Canny algorithm , you can check this article on Python OpenCV Canny Edge Detection.
1 |
canimg = cv2.Canny(gray, 50, 200) |
So now this is our HoughLine algorithm for Line Detection.
1 |
lines= cv2.HoughLines(canimg, 1, np.pi/180.0, 120, np.array([])) |
in here the first parameter is the image that we want to process, another parameter is The geometrical representations of the lines, rho and theta, which are usually 1 and np.pi/180.
Also we we want to write color lines on the line this code is for that
1 2 3 4 5 6 7 8 9 10 11 12 |
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)) cv2.line(image1,(x1,y1),(x2,y2),(0,0,255),2) |
This is for showing of our image
1 2 3 4 |
cv2.imshow('Lines Detected',image1) cv2.imshow("Canny Detection", canimg) cv2.waitKey(0) cv2.destroyAllWindows() |
So now run the complete code and this will be the result
FAQs:
How do you detect lines using Hough transform?
For detecting lines using Hough transform, you can follow these steps:
- Preprocess the Image: Convert the image to grayscale and apply any necessary preprocessing, such as edge detection using techniques like Canny edge detection.
- Apply Hough Transform: Use Hough transform, which is a technique for detecting straight lines in an image. Hough transform maps points in the image space to lines in the parameter space (Hough space).
- Detect Lines: The peaks in the Hough space correspond to the parameters (rho and theta) of the lines in the image space. These peaks are detected to identify the lines present in the image.
- Draw Detected Lines: Once the lines are detected, they can be drawn on the original image to visualize the detected features.
How to detect a line in OpenCV Python?
For detecting a line in OpenCV Python, you can use cv2.HoughLines function, which implements the Hough transform for line detection.
What is the HoughLines function in OpenCV?
In OpenCV cv2.HoughLines function is used to detect lines in an image using the Hough transform. It takes as input the edge-detected image (commonly obtained using Canny edge detection) and returns a list of lines detected in the image. Each line is represented by its parameters rho and theta.
What is syntax of Hough transform?
The syntax of the Hough transform in OpenCV Python using the cv2.HoughLines function is as follows:
1 |
lines = cv2.HoughLines(image, rho_resolution, theta_resolution, threshold) |
- image: The edge-detected image (e.g., obtained using Canny edge detection).
- rho_resolution: Resolution of the rho parameter in pixels.
- theta_resolution: Resolution of the theta parameter in radians.
- threshold: The minimum number of votes (or intersections) required to detect a line.
Subscribe and Get Free Video Courses & Articles in your Email