Find circles using circular Hough transform
finds circles with radii in the range specified by
additional output argument,
radii, contains the estimated radii
corresponding to each circle center in
also returns a column vector,
metric, containing the magnitudes of the
accumulator array peaks for each circle (in descending order). The rows of
radii correspond to the rows of
specifies additional options with one or more name-value arguments, using any of the
Detect Five Strongest Circles in an Image
This example shows how to find all circles in an image, and how to retain and display the strongest circles.
Read a grayscale image into the workspace and display it.
A = imread('coins.png'); imshow(A)
Find all the circles with radius
r pixels in the range [15, 30].
[centers, radii, metric] = imfindcircles(A,[15 30]);
Retain the five strongest circles according to the metric values.
centersStrong5 = centers(1:5,:); radiiStrong5 = radii(1:5); metricStrong5 = metric(1:5);
Draw the five strongest circle perimeters over the original image.
Draw Lines Around Bright and Dark Circles in Image
Read the image into the workspace and display it.
A = imread('circlesBrightDark.png'); imshow(A)
Define the radius range.
Rmin = 30; Rmax = 65;
Find all the bright circles in the image within the radius range.
[centersBright, radiiBright] = imfindcircles(A,[Rmin Rmax],'ObjectPolarity','bright');
Find all the dark circles in the image within the radius range.
[centersDark, radiiDark] = imfindcircles(A,[Rmin Rmax],'ObjectPolarity','dark');
Draw blue lines around the edges of the bright circles.
Draw red dashed lines around the edges of the dark circles.
A — Input image
grayscale image | truecolor image | binary image
Input image in which to detect circular objects, specified as a grayscale, truecolor (RGB), or binary image.
radius — Circle radius
Circle radius, or the approximate radius of the circular objects you want to detect, specified as a positive number.
radiusRange — Range of radii
2-element vector of positive integers
Range of radii for the circular objects you want to detect, specified as a 2-element
vector of positive integers of the form
[rmin rmax], where
rmin is less than
Specify optional pairs of arguments as
the argument name and
Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
centers = imfindcircles(A,radius,ObjectPolarity=bright)
specifies bright circular objects on a dark background.
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
centers = imfindcircles(A,radius,"ObjectPolarity","bright")
specifies bright circular objects on a dark background.
ObjectPolarity — Object polarity
"bright" (default) |
Object polarity, specified as one of the values in the table.
|The circular objects are brighter than the background.|
|The circular objects are darker than the background.|
Method — Computation method
"PhaseCode" (default) |
Computation method used to compute the accumulator array, specified as one of the values in the table.
|Atherton and Kerbyson's  phase-coding method.|
|The method used in the two-stage circular Hough transform , .|
"Method","PhaseCode" specifies the Atherton and
Kerbyson's phase-coding method.
Sensitivity — Sensitivity factor
0.85 (default) | number in the range [0, 1]
Sensitivity factor for the circular Hough transform accumulator array, specified
as a number in the range [0, 1]. As you increase the sensitivity factor,
imfindcircles detects more circular objects, including weak and
partially obscured circles. Higher sensitivity values also increase the risk of false
EdgeThreshold — Edge gradient threshold
number in the range [0, 1]
Edge gradient threshold for determining edge pixels in the image, specified as a
number in the range [0, 1]. Specify
0 to set the threshold to
zero-gradient magnitude. Specify
1 to set the threshold to the
maximum gradient magnitude.
imfindcircles detects more circular
objects (with both weak and strong edges) when you set the threshold to a lower value.
It detects fewer circles with weak edges as you increase the value of the threshold.
imfindcircles chooses the edge gradient threshold
automatically using the function
centers — Coordinates of circle centers
Coordinates of the circle centers, returned as a
2 matrix containing the
x-coordinates of the circle centers in the first column and the
y-coordinates in the second column. The number of rows,
P, is the number of circles detected.
is sorted based on the strength of the circles, from strongest to weakest.
radii — Estimated radii
The estimated radii of the circle centers, returned as a column vector. The radius
radii(j) corresponds to the circle centered at
metric — Circle strengths
Circle strengths provides the relative strengths of the circle centers, returned as
a column vector. The value at
metric(j) corresponds to the circle
radii(j) centered at
The accuracy of
imfindcirclesis limited when the value of
rmin) is less than or equal to 5.
The radius estimation step is typically faster if you use the (default)
"PhaseCode"method instead of
Both computation methods,
"TwoStage"are limited in their ability to detect concentric circles. The results for concentric circles can vary depending on the input image.
imfindcirclesdoes not find circles with centers outside the domain of the image.
imfindcirclesconverts truecolor images to grayscale using the function
rgb2graybefore processing them. Binary (
logical) and integer type images are converted to the data type
im2singlefunction before processing. To improve the accuracy of the result for binary images,
imfindcirclesalso applies Gaussian smoothing using
imfilteras a preprocessing step.
imfindcircles uses a Circular Hough Transform (CHT) based algorithm
for finding circles in images. This approach is used because of its robustness in the presence
of noise, occlusion and varying illumination.
The CHT is not a rigorously specified algorithm, rather there are a number of different approaches that can be taken in its implementation. However, there are three important steps which are common to all approaches.
Accumulator Array Computation
Foreground pixels of high gradient are designated as being candidate pixels and are allowed to cast ‘votes’ in the accumulator array. In a classical CHT implementation, the candidate pixels vote in pattern around them that forms a full circle of a fixed radius. Figure 1a shows an example of a candidate pixel lying on an actual circle (solid circle) and the classical CHT voting pattern (dashed circles) for the candidate pixel.
Classical CHT Voting Pattern
The votes of candidate pixels belonging to an image circle tend to accumulate at the accumulator array bin corresponding to the circle’s center. Therefore, the circle centers are estimated by detecting the peaks in the accumulator array. Figure 1b shows an example of the candidate pixels (solid dots) lying on an actual circle (solid circle), and their voting patterns (dashed circles) which coincide at the center of the actual circle.
If the same accumulator array is used for more than one radius value, as is commonly done in CHT algorithms, radii of the detected circles have to be estimated as a separate step.
imfindcircles provides two algorithms for finding circles in images:
phase-coding (default) and two-stage. Both share some common computational steps, but each has
its own unique aspects as well.
The common computational features shared by both algorithms are as follows:
Use of 2-D Accumulator Array
The classical Hough Transform requires a 3-D array for storing votes for multiple radii, which results in large storage requirements and long processing times. Both the phase-coding and two-stage methods solve this problem by using a single 2-D accumulator array for all the radii. Although this approach requires an additional step of radius estimation, the overall computational load is typically lower, especially when working over a large radius range. This is a widely adopted practice in modern CHT implementations.
Use of Edge Pixels
Overall memory requirements and speed are strongly governed by the number of candidate pixels. To limit their number, the gradient magnitude of the input image is thresholded so that only pixels of high gradient are included in tallying votes.
Use of Edge Orientation Information
Performance is also optimized by restricting the number of bins available to candidate pixels. This is accomplished by using locally available edge information to only permit voting in a limited interval along direction of the gradient (Figure 2). The width of the voting interval, between points cmin and cmax in the figure, is determined by the radius range defined by rmin and rmax.
Voting Mode: Multiple Radii, Along Direction of Gradient
|rmin||Minimum search radius|
|rmax||Maximum search radius|
|ractual||Radius of the circle that the candidate pixel belongs to|
|cmin||Center of the circle of radius rmin|
|cmax||Center of the circle of radius rmax|
|cactual||Center of the circle of radius ractual|
The two CHT methods employed by the function
fundamentally differ in the manner by which the circle radii are computed.
Radii are explicitly estimated using the estimated circle centers along with image information. The technique is based on computing radial histograms  .
Radii are estimated from complex values in the accumulator array, with the radius information encoded in the phase of the array entries . The votes cast by the edge pixels contain information not only about the possible center locations but also about the radius of the circle associated with the center location. Unlike the two-stage method where the radius has to be estimated explicitly using radial histograms, in phase-coding the radius can be estimated by simply decoding the phase information from the estimated center location in the accumulator array.
 T.J Atherton, D.J. Kerbyson. "Size invariant circle detection." Image and Vision Computing. Volume 17, Number 11, 1999, pp. 795-803.
 H.K Yuen, .J. Princen, J. Illingworth, and J. Kittler. "Comparative study of Hough transform methods for circle finding." Image and Vision Computing. Volume 8, Number 1, 1990, pp. 71–77.
 E.R. Davies, Machine Vision: Theory, Algorithms, Practicalities. Chapter 10. 3rd Edition. Morgan Kauffman Publishers, 2005.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
imfindcirclessupports the generation of C code (requires MATLAB® Coder™). Note that if you choose the generic
MATLAB Host Computertarget platform,
imfindcirclesgenerates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Types of Code Generation Support in Image Processing Toolbox.
When generating code, all character vector input parameters and values must be a compile-time constant.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
When the sensitivity factor is near or equal to 1, there may be minor mismatch between the results from the generated code and MATLAB simulation.
Version HistoryIntroduced in R2012a
R2022a: Generate CUDA code using GPU Coder
imfindcircles now supports the generation of
optimized CUDA® code (requires GPU Coder™).
R2019a: New filter size for logical images
Staring in R2019a, the
imfindcircles function uses a 5-by-5 filter
size for smoothing logical images.
imfindcircles may now return a
different answer than in previous releases, when the filter size was 6-by-6. For example, in
some instances, the function may return a different number of circles.