PIXELBANKv9.1.0
Menu

Implement a function to find the top-k peaks in a Hough accumulator, which represents the parameter space of lines in an image. The goal is to identify the most prominent lines, characterized by their Hough transform parameters θθ and ρρ, where θθ is the angle and ρρ is the distance from the origin.

The Hough transform is a feature extraction technique used to detect lines in images by transforming the image into a parameter space, where each cell represents a line in the original image. Peaks in the accumulator correspond to lines that received the most votes from edge points, indicating their presence in the image.

To find these peaks, follow these steps:

  1. Collect all cells with their values and coordinates
  2. Sort by vote count (descending)
  3. Return the top k (θ,ρ)(θ, ρ) pairs
P(θ,ρ)=∑(x,y)H(x,y)⋅δ(ρ−xcos⁡(θ)−ysin⁡(θ))P(θ, ρ) = \sum_{(x, y)} H(x, y) \cdot δ(ρ - x \cos(θ) - y \sin(θ))

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

Example:

Input:
accumulator = [[1, 2, 1],
               [3, 1, 3],
               [1, 5, 1]]
k = 2
Output:
[(2, 1), (1, 0)]
Reasoning:

Flattening with coordinates: (0,0)=1, (0,1)=2, (0,2)=1 (1,0)=3, (1,1)=1, (1,2)=3 (2,0)=1, (2,1)=5, (2,2)=1

Sorting by value (descending):

  1. (2,1)=5
  2. (1,0)=3
  3. (1,2)=3
  4. (0,1)=2 ...

Top 2: [(2, 1), (1, 0)] Note: (1,0) and (1,2) tie at 3, but (1,0) comes first due to smaller rho_idx.

Constraints:

  • accumulator is a 2D array (theta_bins × rho_bins)
  • k is the number of peaks to find
  • Return list of (theta_idx, rho_idx) tuples
  • Sorted by votes (highest first)
  • If tied, prefer smaller theta_idx, then smaller rho_idx
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

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