Hough Transform
Hough transform is a voting-based computer vision technique that detects lines, circles, and shapes, even with gaps or noisy image edges.
The Hough transform is a geometric feature-extraction technique that detects lines, circles, and other parameterized shapes by letting image points vote for possible shapes. Points belonging to the same structure reinforce one another, producing a strong peak in a voting table. In computer vision, this helps recover shapes with gaps or noisy boundaries.
Paul V. C. Hough introduced the technique in 1959 in Machine Analysis of Bubble Chamber Pictures, addressing particle tracks in physics photographs. Richard Duda and Peter Hart introduced the widely used angle–distance representation in their 1972 line-and-curve detection paper.
How Hough Voting Works#
A typical workflow starts with data preprocessing and edge detection. An edge detector identifies local brightness changes; the Hough transform groups edge pixels into geometric structures. The Sobel operator estimates brightness gradients, while Canny produces a thin edge map suitable for line voting.
Each candidate line is represented by rho, its signed perpendicular distance from the image origin, and theta, the angle of that perpendicular relative to the x-axis. This handles vertical lines without an infinite slope. Every edge pixel votes for compatible rho–theta pairs in an accumulator, a table dividing parameter space into discrete bins. Collinear pixels contribute to the same bin, even when separated in the image. Peaks identify supported lines.
Geometry and voting rules determine the output. A deep learning detector learns visual patterns from training data; Hough detection uses a chosen shape model and numerical thresholds rather than learned model weights.
Line, Probabilistic, and Circle Transforms#
Three common implementations differ in their search and output:
- Standard line transform:
cv2.HoughLines()returns rho–theta pairs describing infinite lines. Segment endpoints require additional processing. - Probabilistic line transform:
cv2.HoughLinesP()reduces voting work through point sampling and returns segment endpoints directly.minLineLengthrejects short segments;maxLineGapcontrols how far apart aligned pieces may be joined. - Circle transform: Circle candidates require a center and radius.
cv2.HoughCircles()uses a gradient-based approach, with radius limits narrowing the search. Its input is grayscale, whereas the line functions take a binary image.
Detecting Line Segments in Python#
Install the dependencies with pip install opencv-python numpy. This example uses OpenCV to detect and draw line segments in the bus sample image.
from urllib.request import urlopen
import cv2
import numpy as np
with urlopen("https://ultralytics.com/images/bus.jpg") as response:
encoded = np.frombuffer(response.read(), dtype=np.uint8)
image = cv2.imdecode(encoded, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Extract edges for voting.
edges = cv2.Canny(gray, 50, 150)
# Detect finite segments supported by accumulator votes.
lines = cv2.HoughLinesP(
edges,
1,
np.pi / 180,
threshold=80,
minLineLength=80,
maxLineGap=10,
)
if lines is not None:
for x1, y1, x2, y2 in lines[:, 0]:
cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imwrite("hough_segments.jpg", image)The saved image overlays returned segments in red. Rho bins are one pixel wide, theta bins span one degree, and threshold=80 sets the accumulator vote threshold. Segment-length and gap settings are measured in pixels.
Applications and Practical Limits#
In document processing, extracting grid lines supports structural analysis: OpenCV demonstrates Hough detection on a Sudoku image. Circular voting locates coin centers and radii in scikit-image's circle-and-ellipse examples. These outputs describe geometry; object detection adds semantic categories, and instance segmentation assigns pixels to individual objects.
A steel-beam measurement prototype combines YOLOv8 masks, Canny edges, and probabilistic Hough lines to support corner extraction. Ultralytics YOLO26 segmentation provides masks for object isolation, enabling the same masked-input approach before separate geometric processing.
Finer accumulator bins increase memory use; coarse bins merge nearby candidates. In table-boundary extraction, incomplete borders reduce detectable boundaries, while unrelated lines can produce incorrect boundary corrections. Increasing maxLineGap joins larger breaks between aligned pieces; increasing minLineLength removes shorter segments.










