Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Line detection in python with OpenCV?
In this article, we will learn how to detect lines in an image using the Hough Transform technique with OpenCV in Python.
What is Hough Transform?
Hough Transform is a feature extraction method used to detect simple geometric shapes in images. It can identify shapes even if they are broken or slightly distorted, making it particularly useful for line detection in computer vision applications.
A "simple" shape is one that can be represented by only a few parameters. For example:
- A line requires two parameters: slope and intercept
- A circle requires three parameters: center coordinates (x,y) and radius (r)
Mathematical Representation
In OpenCV, a line is represented in parametric form as x*cos(?) + y*sin(?) = ?, where:
- ? (rho): perpendicular distance from origin to the line
- ? (theta): angle formed by the perpendicular line and horizontal axis (counter-clockwise)
How the Accumulator Works
The Hough Transform uses an accumulator array to detect lines:
- Create a 2D accumulator array where rows represent ? values and columns represent ? values
- For each edge pixel (x,y), calculate all possible (?,?) combinations
- Increment the corresponding accumulator cell for each (?,?) pair
- Points on the same line will vote for the same (?,?) values
- Find peaks in the accumulator to identify lines
OpenCV Implementation
OpenCV provides two main functions for line detection:
-
cv2.HoughLines(): Standard Hough Transform -
cv2.HoughLinesP(): Probabilistic Hough Transform (more efficient)
Example: Line Detection
Here's a complete example that detects lines in an image using the Probabilistic Hough Transform ?
import cv2
import numpy as np
# Read the image
img = cv2.imread("parking_lot.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply edge detection
edges = cv2.Canny(gray, 75, 150)
# Detect lines using Probabilistic Hough Transform
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=30, maxLineGap=250)
# Draw detected lines
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 128), 2)
# Display results
cv2.imshow("Edge Detection", edges)
cv2.imshow("Lines Detected", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Parameters Explained
| Parameter | Description | Typical Value |
|---|---|---|
rho |
Distance resolution in pixels | 1 |
theta |
Angle resolution in radians | np.pi/180 |
threshold |
Minimum votes required | 30-100 |
maxLineGap |
Maximum gap between line segments | 50-300 |
Key Points
- Use
cv2.Canny()for edge detection before applying Hough Transform - Adjust threshold value based on image complexity
- Higher threshold reduces false positives but may miss weak lines
- Probabilistic Hough Transform is faster and returns line endpoints directly
Conclusion
The Hough Transform is a powerful technique for line detection in computer vision. OpenCV's implementation makes it easy to detect lines in images by converting the problem into a voting scheme in parameter space.
---