PIXELBANKv9.1.0
Menu

Implement a Hough accumulator array for line detection given a list of edge points. The goal is to build a 2D voting array indexed by θθ and ρρ to detect lines in an image.

The Hough transform is a feature extraction technique used in image processing to detect lines, circles, and other shapes. It works by transforming image points into a parameter space, where each point in the image votes for all possible lines that could pass through it. The Hough accumulator is a 2D array where each cell represents a possible line in the image, parameterized by θθ (angle) and ρρ (distance from origin).

Here are the steps to build the accumulator:

  1. For each edge point (x,y)(x, y)
  2. For each θθ bin (0° to 180°)
  3. Compute ρ=x⋅cos(θ)+y⋅sin(θ)ρ = x·cos(θ) + y·sin(θ)
  4. Map ρρ to a bin index and increment that cell
ρ=x⋅cos(θ)+y⋅sin(θ)ρ = x·cos(θ) + y·sin(θ)

This technique is widely used in computer vision for line detection and image processing.

Example:

Input:
edge_points = [(0, 0), (1, 1), (2, 2)]
theta_bins = 4
rho_bins = 5
max_rho = 3
Output:
[[1, 1, 1, 1, 1], [0, 1, 2, 1, 0], [0, 0, 1, 2, 1], [0, 1, 1, 1, 1]]
Reasoning:
  • Theta angles: 0°, 45°, 90°, 135° (4 bins from 0 to π)

  • Rho range: [-3, 3] mapped to 5 bins

  • For point (0,0): ρ=0 for all θ → votes in middle bin

  • For point (1,1):

    • θ=0°: ρ=1 → bin 3
    • θ=45°: ρ=√2≈1.41 → bin 3-4
    • θ=90°: ρ=1 → bin 3
    • θ=135°: ρ=0 → bin 2
  • For point (2,2): similar pattern

The collinear points create a peak at θ=45° (or 135°), showing their line.

Constraints:

  • edge_points is a list of (x, y) coordinates
  • theta_bins is the number of θ bins (covering 0 to π)
  • rho_bins is the number of ρ bins
  • max_rho is the maximum absolute ρ value
  • Return 2D accumulator array of shape (theta_bins, rho_bins)
solution.py

Test Results

0/0
Run code to see test results.
Hough Accumulator - Medium | PixelBank