OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Line detection is a fundamental operation in computer vision, often used in applications like road lane detection in autonomous vehicles, analyzing medical images, and many more. The Hough Line Transform is a popular technique to detect lines in an image.
The Hough Line Transform works by representing each point in the image in the Hough space, usually defined by parameters r and ��. Here, r is the perpendicular distance from the origin to the line, and �� is the angle formed by this perpendicular line and the horizontal axis.
In this tutorial, we will use the HoughLines
and HoughLinesP
functions provided by OpenCV to detect lines in an image.
pip install opencv-python
import cv2 import numpy as np
image = cv2.imread('path_to_image.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150, apertureSize=3)
Here, we first convert the image to grayscale and then use the Canny edge detection method to find edges in the image.
lines = cv2.HoughLines(edges, 1, np.pi/180, 200) 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(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
The HoughLines
function returns a two-element vector (��,��) that defines the line in polar coordinates. We then convert this representation to Cartesian coordinates to draw the line on the image.
cv2.imshow('Detected Lines', image) cv2.waitKey(0) cv2.destroyAllWindows()
A more efficient version, which doesn't return all the line segments but only a random subset of them, is HoughLinesP
:
minLineLength = 100 maxLineGap = 10 lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap) for line in lines: x1, y1, x2, y2 = line[0] cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
This version is particularly useful when you want to detect lines in a large image or video stream, as it's faster and can be tuned to be more selective.
HoughLines
(the fourth argument) based on your requirements. A higher threshold will detect fewer lines.HoughLinesP
to detect shorter or longer line segments.This concludes the tutorial on line detection using the Hough Line method in OpenCV!
Detecting lines in images using Hough Line method with OpenCV:
The Hough Line Transform is a popular technique for detecting lines in images. It represents lines in polar coordinates and uses voting to identify prominent lines.
Python OpenCV Hough Line Transform for line detection:
OpenCV provides the cv2.HoughLines
function to perform the Hough Line Transform. This function takes an edge-detected image and returns lines in polar coordinates.
Sample code for line detection using Hough Line in OpenCV:
Here's a basic code snippet:
import cv2 import numpy as np # Load an image img = cv2.imread('image.jpg', 0) # Apply edge detection (e.g., using Canny) edges = cv2.Canny(img, 50, 150) # Apply Hough Line Transform lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=100) # Draw the lines on the original image 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(img, (x1, y1), (x2, y2), (0, 0, 255), 2) # Display the result cv2.imshow('Hough Lines', img) cv2.waitKey(0) cv2.destroyAllWindows()
Python OpenCV line detection vs edge detection:
Edge detection identifies boundaries in an image, while line detection specifically focuses on detecting lines. Hough Line Transform is often applied to edge-detected images to identify and draw lines.