Function File: centers = imfindcircles (im, radius)
Function File: centers = imfindcircles (im, RadiusRange)
Function File: centers = imfindcircles (…, property, value)
Function File: [centers, radii, strengths] = imfindcircles (…)

Find circles in image using the circular Hough transform.

This function finds circles in a given 2d image im. The image data can be grayscale, rgb color, or binary. The search is done only for circular shapes of approximatly the given radius (single value) or RadiusRange (a two element vector [r_min, r_max]).

The output centers is a two column matrix, where each row contains the (fractional) x and y coordinates of the center of one found circle. The output rows are ordered according the strengths of the circles, the strongest circle first.

The output radii is a column vector of the (fractional) radii of the found circles, in the same order as the circle centers. If radius is specified, instead of RadiusRange, then all values in radii are the same as radius.

The output strengths is a measure of how "strong" the corresponding circle is. (A sharp, smooth and full circle gives roughly a value of one. The minimum value is zero.)

Additionally the following optional property-value-pairs can be used:

ObjectPolarity

Either "bright" circles on darker background can be searched (default), or "dark" circles on brighter background.

Method

The default method "PhaseCode" is faster as the (currently unimplemented) method "TwoStage". For literature and details on the algorithm, see comments in the code.

Sensitivity

A value between 0 and 1 to say how strong a circle must be in order to be found. With a value of 1 no circles are discarded, default value is 0.85. (Use bigger values of Sensitivity if your circles are not properly found, until unwanted "ghost" circles start to appear.)

EdgeThreshold

A value between 0 and 1 to say how strong an edge point of the image must be, to be considered as a possible point on a circle circumference. With a value of 0 all possible edge points are considered, with a value of 1 only the edge point with the stronges gradient will be considered. As default value the output of graythresh (Otsu’s threshold) of the gradient image is taken.

Notes:

  • Only center points inside the image region can be found.
  • For concentric cirles the output is unpredictable.
  • For optimal speed, keep the radius range and the image size as small as possible.
  • For radius values above 100 pixels the sensitivity and accuracy of this algorithm starts to decrease.
  • For big radius ranges the sensitivity decreases. Try to keep r_max < 3 * r_min .
  • RGB color images will automatically be converted to grayscale using the rgb2gray function.
  • Binary images will automatically be converted to grayscale and be slightly smoothed before processing.

Compatibility note: The centers and radii outputs have good compatibility to the Matlab outputs. The strengths outputs differ a bit, but have mostly the same ordering. Currently only the (default) Matlab algorithm "PhaseCode" is implemented in Octave.

See also: hough, hough_circle.

Demonstration 1

The following code

 ## First generate an input image:
 model = [ 1.0   0.2   0.2   0.2   0.5   0
           1.0   0.3   0.3  -0.1  -0.2   0
          -0.5   0.7   0.7  -0.5   0.5   0];
 im = phantom (model);
 im(170:230,170:230) = 1;
 im = imfilter (im, fspecial ("average", 3));
 im = imnoise (im, "salt & pepper");
 imshow (im);

 ## Find and show circles with radius between 20 and 50:
 [centers, radii] = imfindcircles (im, [20 50]);
 viscircles (centers, radii)
 title ("found circles in red")

Produces the following figure

Figure 1

Package: image