v9.1.1
Menu

Circle Hough Transform

hardPROHough TransformsHoughCV: Feature Detection and Matching

Implement the Hough transform for circle detection, a technique used in computer vision to identify circular shapes in images. The goal is to detect circles of varying radii in an image by transforming the image into a parameter space where circles are represented as peaks.

The Hough transform is a feature extraction technique that uses a voting system to detect shapes, such as lines, circles, and ellipses, in an image. For circle detection, the parameter space is 3D, represented as (a,b,r)(a, b, r), where (a,b)(a, b) is the center of the circle and rr is the radius. The equation of a circle is given by (x−a)2+(y−b)2=r2(x-a)^2 + (y-b)^2 = r^2.

  1. For each edge pixel (x,y)(x, y),
  2. For each possible radius rr,
  3. Vote for all centers (a,b)(a, b) on the circle,
  4. Find peaks in the accumulator space.
(x−a)2+(y−b)2=r2(x-a)^2 + (y-b)^2 = r^2

This technique is widely used in object detection and recognition applications.

Output

Return a list of (a, b, r, votes) tuples, where aa is the centre's column (xx) and bb its row (yy), for every (a,b,r)(a, b, r) with at least threshold votes. Sort them by vote count, highest first, and return at most the 10 strongest.

Example

Input
edges = binary image with circle edges
r_min = 10, r_max = 50
threshold = 100
Output
[(center_x, center_y, radius, vote_count), ...]
Reasoning
  • Inputs: edges is an H×WH \times W binary image (1 marks an edge), r_min and r_max bound the radii tried (inclusive), and threshold is the minimum vote count; results look like [(center_x, center_y, radius, vote_count), ...].
  • Take a 100×100100 \times 100 image whose 146 edge pixels trace a radius-20 circle around (50,50)(50, 50), with radii 15 to 25 and threshold 30.
  • Edge pixel (x,y)(x, y) fits any centre with (x−a)2+(y−b)2=r2(x-a)^2 + (y-b)^2 = r^2, a ring of radius rr around it; each cell (a,b,r)(a, b, r) on that ring gains a vote.
  • At r=20r = 20 the rings cross near the true centre, but they only touch whole pixels, so not all meet in one cell: the peak is (49,49,20)(49, 49, 20) with 36 votes (one pixel off, as positions round down).
  • Neighbour (50,49,20)(50, 49, 20) gets 18 and radii 19 or 21 reach at most 15, all under 30, leaving only (49,49,20,36)(49, 49, 20, 36).
  • Notice: at threshold = 100 this circle would not be found, and an image with no edges gives an empty list.

Constraints

  • edges: Binary edge image (H, W)
  • r_min, r_max: Range of radii to detect
  • threshold: Minimum votes to detect circle
  • Return: List of (a, b, r, votes) detected circles

Learn this in

More from CV: Feature Detection and Matching

PRO

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

Sign in to run your code against the test cases.