Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Hough transform

Hough transform is a method to detect parametric shapes (lines, circles, ellipses) in images by voting in a discretized parameter space so that points lying on a shape produce a peak in that space.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Hough transform is used by engineers to detect simple geometric shapes (most commonly straight lines and circles) in noisy or partially occluded images. It is a standard tool in machine vision systems (lane detection, sheet/part inspection, object alignment) and a building block before more advanced tasks like object recognition and tracking.

Key ideas

  • Parametrisations for a line (two commonly used forms):

    • Slope-intercept form: y = m·x + c. Here m is the slope and c is the intercept. This fails for vertical lines because m → ∞.
    • Normal (ρ–θ) form (preferred): ρ = x·cos θ + y·sin θ. Here ρ is the perpendicular distance from the origin to the line, and θ is the angle of the normal with the x-axis. This form represents any line uniquely for θ in [0, π) and ρ real.
  • Why normal form works (derivation exam-style):

    1. Draw a 2D Cartesian coordinate system (x axis to right; choose y upward or downward and state it clearly). In exams, either do the math in the same image coordinate system (e.g., origin at top-left, y downward) or in a standard right-handed Cartesian system (y upward); in both cases be explicit and be consistent about the convention.
    2. For a line L, choose the perpendicular (normal) vector n of unit length making angle θ with x-axis. Let ρ be the signed distance from origin to the line along n. Any point (x, y) on the line satisfies projection onto n equal to ρ: (x, y)·n = ρ. That gives x·cos θ + y·sin θ = ρ.
    3. For a fixed image point (x0, y0), the set of (ρ, θ) satisfying the equation form a sinusoid in (ρ, θ) space: ρ(θ) = x0·cos θ + y0·sin θ. Points on the same image line produce sinusoids that intersect at the (ρ, θ) corresponding to that line.
  • Accumulator and discretisation:

    • Define a discrete grid over parameter space: quantise θ into N_θ bins and ρ into N_ρ bins (or radius and center coordinates for circles). Create an array A[ρ_bin, θ_bin] initialised to zero — accumulator. Choose ρ range explicitly, typically ρ in [−ρ_max, ρ_max], where ρ_max = sqrt((W-1)^2 + (H-1)^2) for an image of width W and height H.
    • For each edge pixel (x, y), compute the set of parameters (ρ, θ) consistent with that pixel (for θ in sampled set) and increment the corresponding accumulator cells: A[bρ, bθ] += 1. Peaks in A indicate likely shapes.
  • Circle detection: circle equation (x - a)^2 + (y - b)^2 = r^2. The Hough circle transform votes in a 3D parameter space (a, b, r) if r unknown. If r known, voting reduces to (a, b) space: for an edge point (x, y), possible centers satisfy a = x - r·cos φ, b = y - r·sin φ for φ ∈ [0, 2π).

  • Tradeoffs and implementation details:

    • Resolution: finer bins give better accuracy but use more memory and increase noise sensitivity; coarse bins can merge distinct shapes.
    • Complexity: if E is number of edge points and M is number of parameter samples per point, complexity is O(E·M). Memory equals number of accumulator cells.
    • Use gradient direction at edge pixel to reduce θ search: the edge gradient is perpendicular to the line; so restrict θ to gradient angle ± tolerance — reduces M.
    • Variants: Probabilistic Hough Transform (PHT) samples a subset of edge points to reduce cost; Randomized Hough Transform (RHT) selects random point subsets to vote for parameters directly.
    • Post-processing: non-maximum suppression on accumulator peaks, thresholding, combining nearby peaks, and mapping back parameters to image for verification.
  • Assumptions: edges are reasonably detected (Hough does not find edges), shapes are well modelled by the chosen parametric family, and noise not so high that accumulator peaks vanish.

  • What to draw for exam answers:

    1. Image plane: draw a small pixel grid, mark a few edge points, label coordinates (x, y). State coordinate origin and axis directions.
    2. Parameter space: draw ρ–θ axes. For a single point, draw a sinusoid ρ(θ) = x·cos θ + y·sin θ. For three collinear points, sketch their three sinusoids intersecting at a common point (ρ0, θ0). Label the intersection as the detected line parameters.

Formulas

ρ = x·cos θ + y·sin θ — ρ is perpendicular distance from origin to line (pixels), θ is normal angle (radians or degrees, used consistently). Applies to normal form of line. Conversion to slope-intercept: for sin θ ≠ 0, m = -cos θ / sin θ and c = ρ / sin θ; for vertical lines (θ ≈ 0 or π) use x = ρ.

y = m·x + c — m is slope (unitless), c is y-intercept (pixels). Valid for non-vertical lines.

(x - a)^2 + (y - b)^2 = r^2 — a,b are circle center coordinates (pixels); r is radius (pixels). Exact circle equation.

A[bρ, bθ] += 1 — accumulator increment: bρ is the bin index containing ρ, bθ is the bin index containing θ. Unitless counts. Applies per edge pixel per θ sample.

a = x - r·cos φ and b = y - r·sin φ — for a circle of known radius r, candidate center (a,b) from edge point (x,y) at angle φ. Units: pixels.

Complexity ≈ O(E·Nθ) — E is number of edge pixels, Nθ is number of θ samples (for lines). If circle radius is unknown and Nr radius samples and Nφ angle samples per center are used (no gradient constraint), complexity expands to O(E·Nr·Nφ). Units: operations.

Worked examples

Example 1 — detecting a vertical line in a 5×5 image (numerical, step-by-step)

Setup and coordinate convention:

  • Image pixel coordinates (x,y) with origin at top-left; x increases rightwards, y increases downwards. Units: pixels.
  • Edge points: P1=(2,0), P2=(2,1), P3=(2,2). These three are on the vertical line x=2.
  • Choose θ samples: θ = 0°, 45°, 90° (i.e., 0, π/4, π/2). Compute ρ = x·cos θ + y·sin θ.

Step 1: compute cosine and sine values (approx):

  • cos 0° = 1, sin 0° = 0
  • cos 45° = 0.7071, sin 45° = 0.7071
  • cos 90° = 0, sin 90° = 1

Step 2: compute ρ for each point and θ

  • For P1=(2,0):
    • θ=0°: ρ = 2·1 + 0·0 = 2
    • θ=45°: ρ = 2·0.7071 + 0·0.7071 = 1.4142
    • θ=90°: ρ = 2·0 + 0·1 = 0
  • For P2=(2,1):
    • θ=0°: ρ = 2·1 + 1·0 = 2
    • θ=45°: ρ = 2·0.7071 + 1·0.7071 = 2.1213
    • θ=90°: ρ = 2·0 + 1·1 = 1
  • For P3=(2,2):
    • θ=0°: ρ = 2·1 + 2·0 = 2
    • θ=45°: ρ = 2·0.7071 + 2·0.7071 = 2.8284
    • θ=90°: ρ = 2·0 + 2·1 = 2

Step 3: discretise ρ bins (for simplicity assume bins centred at integer ρ values)

  • When θ=0°, all three ρ values are exactly 2 → accumulator cell (ρ=2, θ=0°) gets 3 votes.
  • For other θ, votes spread across different ρ bins.

Conclusion: accumulator has a clear peak at (ρ=2, θ=0°) representing vertical line x=2. Final answer (bold): detected line parameters ρ=2 pixels, θ=0°.

Example 2 — Hough circle with known radius (numerical)

Setup:

  • Edge points assumed from a circle with center (a,b)=(5,5) and radius r=3 pixels: Q1=(8,5), Q2=(5,8), Q3=(2,5), Q4=(5,2).
  • Use angle samples φ = 0°, 90°, 180°, 270° (cos,sin values exact: 1,0,-1,0 and 0,1,0,-1).

For each edge point and each φ compute candidate center (a_candidate, b_candidate) = (x - r·cos φ, y - r·sin φ).

  • Q1=(8,5): φ=0° → center = (8 - 3·1, 5 - 3·0) = (5,5).
  • Q2=(5,8): φ=90° → center = (5 - 3·0, 8 - 3·1) = (5,5).
  • Q3=(2,5): φ=180° → center = (2 - 3·(-1), 5 - 3·0) = (5,5).
  • Q4=(5,2): φ=270° → center = (5 - 3·0, 2 - 3·(-1)) = (5,5).

All four votes accumulate into accumulator cell corresponding to center (5,5) (and radius r=3). Bold final: detected circle center (5,5) radius 3 pixels.

Notes: In practice φ is sampled more densely and accumulator cells are discretised; the edge gradient (radial normal) lets you restrict φ to be near the gradient angle or its opposite (±π), so votes are concentrated.

Common mistakes

  • Forgetting to discretise and quantise accumulator indices, or using inconsistent bin width between ρ and θ.
  • Not using edge gradient information to reduce θ range; this makes algorithm slow and noisy.
  • Not performing non-maximum suppression on accumulator peaks — reporting many spurious detections instead of distinct shape parameters.
  • Using too coarse bins (merging shapes) or too fine bins without smoothing (spreading votes due to pixel noise).

For your exam

Types of questions you will see:

  • Definitions: define Hough transform, accumulator, parameter space, normal form of a line.
  • Short notes: describe Hough transform algorithm steps, advantages and disadvantages, and variants (probabilistic, randomized).
  • Derivations: derive the normal form equation ρ = x·cos θ + y·sin θ and show why a single image point maps to a sinusoid in (ρ, θ) space.
  • Numericals: compute accumulator votes for small sets of points with given θ sampling; detect a line or circle and give parameters.
  • Diagrams: draw image plane with points and the corresponding parameter-space sinusoids showing intersection.

What a full-marks answer includes:

  • Clear definition of terms used (accumulator, parameterisation). State coordinate convention explicitly.
  • Step-by-step algorithm (edge detection → discretise parameter space → vote → find peaks → back-project and verify).
  • Correct derivation for the normal form and explanation of how intersections of sinusoids correspond to lines.
  • For numerical problems, show computations for each point and each sampled parameter value, show accumulator updates, and give the final parameters with units (pixels, degrees/radians).
  • Draw and label required diagrams exactly as described in the Key ideas section.

Quick check

  1. Write the normal form equation of a line and name its parameters.
  2. Why is slope-intercept form unsuitable for full Hough line detection?
  3. For circle detection with known radius r, what parameter space do you vote in and how do you compute candidate centers from edge point (x,y)?

Answers: 1) ρ = x·cos θ + y·sin θ, where ρ is perpendicular distance (pixels) and θ is normal angle (radians or degrees). 2) Because slope-intercept (y = m·x + c) fails for vertical lines where slope m → infinity. 3) Vote in (a,b) center space (pixels); candidate centers are a = x - r·cos φ, b = y - r·sin φ for φ sampled over [0, 2π).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?