メインコンテンツ

fitcircle

R2026b

Measure circle in image using circle tool

Since R2026b

Description

measurementData = fitcircle(I,position) measures a circle in a grayscale or RGB input image, I, by positioning a circle tool at the position position. This function performs a semi-automatic circle measurement when the tool snaps to circular edges detected near the specified position.

example

measurementData = fitcircle(I,position,Name=Value) specifies options using one or more name-value arguments. For example, SnapNeighborhood=1 specifies the snapping neighborhood as 1 pixel.

Examples

collapse all

Read a test image into the workspace.

A = imread("metalplate.png");

Adjust the image contrast.

A = imadjust(A);

Display the adjusted image.

hIm = imageshow(A);

Measure the circle at the position [1372,1873,61.5] using the fitcircle function.

measuredCircle= fitcircle(A,[1372,1873,61.5])
measuredCircle = struct with fields:
           Center: [1.3683e+03 1.8812e+03]
           Radius: 43.4954
             Area: 5.9434e+03
    Circumference: 273.2894
         Position: [1.3683e+03 1.8812e+03 43.4954]

Input Arguments

collapse all

Input image in which to perform the circle measurement, specified as one of these values:

Image TypeData Format
Grayscale H-by-W numeric matrix
RGB H-by-W-by-3 numeric array

Note

By default, spatial image coordinates are in the intrinsic coordinate system, where the center of the upper left pixel has intrinsic coordinates (1.0, 1.0). If you perform a coordinate transformation on the image input I, units are defined in the world coordinate system. To learn more, see Image Coordinate Systems.

Position of the circle tool, specified as one of these options:

Position formatDescription
3-element row vector

The vector is of the form [xCenter yCenter R], where the [xCenter yCenter] coordinate is the center of the circle and R is the circle radius.

N-by-2 matrix

Each row in the matrix represents a coordinate point to which the circle is fit.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is 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.

Example: fitcircle(hImage,position,SnapNeighborhood=1) specifies the snapping neighborhood as 1 pixel.

Snap the circle to edges, specified as a numeric or logical 1 (true) or 0 (false). By default, the tool detects nearby circular edges using the method specified by the EdgeMap argument, and then fits the circle to the edges using the RANSAC algorithm.

Specify the snapping neighborhood using the SnapNeighborhood argument.

Snapping neighborhood, specified as a positive scalar. SnapNeighborhood specifies a band-shaped search region for edge snapping, specified as a fraction of the circle radius. The algorithm searches for edges within an annular neighborhood extending from (1 – S)R to (1 + S) R around the circle’s circumference. S is the value of SnapNeighborhood, and R is the radius of the circle. Units are in pixels.

Edge detection method for snapping, specified as a logical array or one of these options.

MethodDescription
"canny"

Finds edges by looking for local maxima of the gradient of the image. The edge function calculates the gradient using the derivative of a Gaussian filter. This method uses two thresholds to detect strong and weak edges, including weak edges in the output if they are connected to strong edges. By using two thresholds, the Canny method is less likely than the other methods to be fooled by noise, and more likely to detect true weak edges.

"sobel"

Finds edges at those points where the gradient of the image is maximum, using the Sobel approximation to the derivative.

"prewitt"

Finds edges at those points where the gradient of the image is maximum, using the Prewitt approximation to the derivative.

"roberts" Finds edges at those points where the gradient of the image is maximum, using the Roberts approximation to the derivative.
"log" Finds edges by looking for zero-crossings after filtering the image with a Laplacian of Gaussian (LoG) filter.
"zerocross" Finds edges by looking for zero-crossings after filtering the image.
"approxcanny"

Finds edges using an approximate version of the Canny edge detection algorithm that provides faster execution time at the expense of less precise detection.

If you specify EdgeMap as a logical array, you define a binary image that contains precomputed edge locations for snapping. Select this option to avoid repeated edge detection when making multiple measurements on the same image.

Number of RANSAC iterations to run to detect the most accurately snapped circle, specified as a positive integer. The Random Sample Consensus (RANSAC) algorithm for circle estimation involves repeated random sampling steps to robustly estimate the parameters of a circle from a set of measurement points. Increase the number of iterations to increase the chance that at least one sample contains only inliers (points in the true circle), leading to a good model at the expense of longer computation time.

Inlier distance threshold, specified as a positive numeric scalar. This value defines the maximum allowable distance, in pixels, between a measurement point and the estimated circle for that point to be considered an inlier. Increase the RansacInlierDistance value to count more noisy or slightly off-circle points as inliers, at the expense of a less accurate circle fit and measurement.

Angle defining the circumference arc, specified as a 2-element numeric vector. The vector is in the form [startAngle, stopAngle], where startAngle and stopAngle represent the initial and final angle of the arc, respectively, measured counterclockwise from the positive x-axis. Both angles are unbounded. For example, [0, 360] and [-180, 180] define the same full circle. Specify the AngleRange to snap to an obstructed circle or circle arc.

Geometric transformation for the input image data that defines a 2-D world coordinate system, specified as an affinetform2d object, an imref2d object, a rigidtform2d object, a simtform2d object, or a transltform2d object.

If you perform a coordinate transformation on the input image data, the units of measurementData are defined in the world coordinate system. To learn more about image coordinate systems, see Image Coordinate Systems.

Output Arguments

collapse all

Circle measurement result, returned as a structure with these fields:

measurementData FieldDescription
PositionMeasured circle position, stored as a 3-element row vector. The vector is of the form [xCenter yCenter R], where the [xCenter yCenter] coordinate is the center of the circle and R is the circle radius.
Center

Measured circle center, stored as a 2-element row vector. The vector is of the form [xCenter yCenter], where the xCenter and yCenter coordinates define the center of the circle.

Radius

Measured circle radius, stored as a positive numeric scalar.

AreaMeasured circle area, stored as a positive numeric scalar.
CircumferenceMeasured circle circumference, stored as a positive numeric scalar

Extended Capabilities

expand all

Version History

Introduced in R2026b