The Hough transform: lines and circles
| Problem | Find straight lines and circles in an edge map, even when they are broken, partly hidden or surrounded by clutter |
| Devised by | Paul Hough (1959, patented 1962), for bubble-chamber tracks; Richard Duda and Peter Hart (1972), the form and circles |
| Accuracy | The strongest lines and circles agree with scikit-image’s (see the chapter overview); positions to the accumulator’s resolution, one pixel and one degree by default |
| Cost | for lines and per radius for circles, for edge pixels |
| Read first | Segmentation: Otsu’s threshold, Canny edges and the watershed |
| API | hough_transform, HoughLines, hough_line_peaks, hough_lines, hough_circles |
Overview
An edge detector reports pixels, not shapes. The Hough transform turns the search for shapes into voting. Each edge pixel votes for every shape that could pass through it, and votes accumulate in an array over the shapes’ parameters. A shape supported by many edge pixels collects many votes and appears as a peak. Pixels need not be adjacent or in order, so gaps and occlusions only reduce a peak, and scattered clutter spreads its votes thinly. Lines have two parameters, and circles of a known radius have two (the centre).
Where it is used
The Hough transform finds shapes by voting, which makes it robust to gaps and clutter.
- Driver assistance and autonomous vehicles. Lane markings are detected as lines in an edge map.
- Industrial inspection. Finding circular holes, bolts and rims in manufactured parts.
- Medical imaging. Circle detection locates cells, the iris of an eye or the head of a bone in X-ray.
- Document analysis. Skew detection and straightening of scanned pages.
History
Paul Hough devised the method in 1959 to find the straight tracks of particles in bubble-chamber photographs, and patented it in 1962. His version parametrised lines by slope and intercept, which are unbounded for near-vertical lines. Richard Duda and Peter Hart (1972) replaced them with the normal form , which is bounded, and extended the method to circles. Dana Ballard (1981) generalised it to arbitrary shapes given as templates. The Hough transform remains a standard tool in document analysis, lane detection and industrial inspection.
The idea
Lines in normal form
Every line in the plane can be written
where is the direction of the line’s normal and its signed distance from the origin. Here is the column and the row, increasing downwards. Through a fixed point passes one line for each , so the point votes along the
sinusoid in the plane. The sinusoids of collinear points all cross at the line’s parameters. hough_transform quantises into num_angles steps
and into bins of rho_resolution pixels, covering the image diagonal.
Figure 17.9. Hough accumulator.
Finding peaks
One physical line spreads its votes over neighbouring cells, so hough_line_peaks takes the strongest cell, suppresses cells within min_rho_bins and min_theta_bins of it, and repeats.
The neighbourhood wraps across , where the same line reappears with negated. hough_lines does both steps with one-degree, one-pixel bins.
Circles
A circle of radius centred at passes through if . For a known radius, each edge pixel votes for every centre on the circle of radius around itself. The votes of a true circle’s
pixels meet at its centre. hough_circles does this for each candidate radius. It reports centres whose votes exceed min_support times the circumference , strongest first, and keeps only centres at least min_distance apart.
The support of a complete circle is about 0.9 for an 8-connected outline and up to 1.3 for a thicker, 4-connected one.
Figure 17.10. Hough transform.
The two strongest lines have 55 and 52 votes, at (θ, ρ) = (82°, 81) and (140°, 1); the clutter’s best cell has 9. The best circle is centred at row 32, column 66 with radius 15 and support 0.71 (the true circle: row 32, column 66, radius 15, one third missing).
Algorithm
Line transform, on a binary edge image.
- Allocate an accumulator with one row for each quantised distance and one column for
each of the
num_anglesangles , all zero. Tabulate and . - For each edge pixel and each angle: compute , find its bin, and add 1 to that cell.
The cost is (number of edge pixels) × (number of angles).
Peak finding.
- List the cells with at least
min_votesvotes, sorted by decreasing votes. - Take each in turn. If it has not been suppressed, report it as a line, and suppress every cell within a given neighbourhood of it. The neighbourhood wraps round in : the lines and are the same line, so beyond the last angle the neighbourhood continues at the first, with mirrored.
Circle transform, for a list of candidate radii.
- For each radius, list the distinct integer pixel offsets that lie on a circle of that radius.
- For each edge pixel and each offset, add 1 to the accumulator cell (for that radius) at the pixel displaced by the offset. A true centre collects a vote from every edge pixel on its circle.
- Keep the cells whose votes are at least
min_supporttimes the circumference , which is about the number of votes a complete circle would collect. - Sort them by strength, and discard any centre closer than
min_distanceto a stronger one.
Implementation
for ((r, c), &e) in edges.indexed_iter() {
if !e {
continue;
}
let (x, y) = (c as f64, r as f64);
for (j, (cos, sin)) in trig.iter().enumerate() {
let bin = ((x * cos + y * sin - rho_min) / rho_resolution).round() as usize;
acc[(bin, j)] += 1;
}
}
HoughLinesholds the accumulator, the angles and the axis;rho(i)gives the centre of bin .- A circle’s offsets are deduplicated, so each edge pixel votes once for each candidate centre.
- For unknown radii, pass a range: the cost grows linearly with the number of radii.
In practice
//! The Hough transform for lines and circles.
use lib_jsl_dsp::discrete::image::feature_extraction::hough_transform::{hough_circles, hough_lines};
use ndarray::Array2;
use std::f64::consts::PI;
fn main() {
let mut edges = Array2::from_elem((64, 64), false);
// The line y = x / 2 + 10 (x the column, y the row), with a gap, and a circle of radius 9.
for x in 0..64 {
if !(20..30).contains(&x) {
edges[(10 + x / 2, x)] = true;
}
}
for k in 0..360 {
let a = k as f64 * PI / 180.0;
edges[((45.0 + 9.0 * a.sin()).round() as usize, (20.0 + 9.0 * a.cos()).round() as usize)] = true;
}
for line in hough_lines(&edges, 20, 1).unwrap() {
println!("line: θ = {:.0}°, ρ = {:.1}, {} votes", line.theta.to_degrees(), line.rho, line.votes);
}
// The line's direction is (2, 1), so its normal is (−1, 2)/√5: θ = atan2(2, −1), ρ = 20/√5.
println!("exact: θ = {:.1}°, ρ = {:.2}", 2f64.atan2(-1.0).to_degrees(), 20.0 / 5f64.sqrt());
for c in hough_circles(&edges, &[7, 9, 11], 0.6, 5).unwrap().iter().take(1) {
println!("circle: centre (row {}, column {}), radius {}, support {:.2}", c.row, c.col, c.radius, c.support);
}
}
line: θ = 117°, ρ = 8.5, 50 votes
exact: θ = 116.6°, ρ = 8.94
circle: centre (row 45, column 20), radius 9, support 1.27
The rasterised line lies on average a quarter of a pixel above the exact line , which lowers by . The nearest bin centre to the result is 8.5. The cell has 50 votes; the other few of the 54 line pixels fell in neighbouring bins. The circle, drawn densely, is thicker than 8-connected and scores 1.27.
Strengths
- Robust to gaps, occlusion and clutter; finds several shapes at once.
- Simple; the line accumulator is a useful picture in itself.
Weaknesses
- Memory and time grow with the number of parameters (a circle of unknown radius needs three, an ellipse five).
- Resolution is fixed by the bins; peaks of nearby or short shapes merge, and textured regions create false peaks.
Exercises
- Show that the sinusoids of two points cross at the parameters of the line through them.
- Why does the slope-intercept form need an unbounded accumulator, and the normal form not?
- Use the gradient direction at each edge pixel to vote for only one (or one centre for circles), and compare the cost and the peaks.
- Show that a line of length pixels at angle gives about votes in one cell only if the bin is at least about one pixel wide.
- Extend
hough_circlesto estimate the radius by fitting the votes of each centre over a range of radii.
Hints and answers
- The line through points and satisfies for both points, so the two sinusoids intersect at that .
- Slope-intercept has for vertical lines. The normal form has and the image diagonal, both bounded.
- With the gradient direction the edge pixel votes only for along the gradient (plus or minus a small tolerance), so the cost per pixel falls from the number of angle bins to one or a few and the peaks are sharper.
- A line of length gives pixels whose values are spread over an interval of about for a small angular error ; a bin of at least one pixel keeps them in a single cell.
- For each centre candidate, accumulate votes at the distances to the edge pixels; the peak of this one-dimensional histogram gives the radius.
Further reading
- P. V. C. Hough, “Method and means for recognizing complex patterns”, US patent 3 069 654, 1962.
- R. O. Duda and P. E. Hart, “Use of the Hough transformation to detect lines and curves in pictures”, Communications of the ACM 15 (1972) 11–15.
- D. H. Ballard, “Generalizing the Hough transform to detect arbitrary shapes”, Pattern Recognition 13 (1981) 111–122.
- R. Szeliski, Computer Vision: Algorithms and Applications, 2nd ed., Springer, 2022, section 7.4.