Although a linear least squares fit of a circle to 2D data can be computed, this is not the solution which minimizes the distances from the points to the fitted circle (geometric error). The linear solution minimizes the algebraic error of a function something like
f(x) = ax'x + b'x + c = 0
Minising the geometric error is a nonlinear least squares problem. fitcircle allows you to compute either - it uses the algebraic fit as the initial guess for the geometric error minimization.
e.g.
x = randn(2, 10);
% Linear least squares fit
[z, r] = fitcircle(x, 'linear')
% True best fit (minimizing geometric error)
[z, r] = fitcircle(x)
For more information look at the published demo file.
This submission is based on the paper:
"Least-squares fitting of circles and ellipses", W. Gander, G. H. Golub, R. Strebel, BIT Numerical Mathematics, Springer 1994
A similar submission for ellipses should be forthcoming
Richard Brown (2020). fitcircle.m (https://www.mathworks.com/matlabcentral/fileexchange/15060-fitcircle-m), MATLAB Central File Exchange. Retrieved .
Create scripts with code, output, and formatted text in a single executable document.
Julian Guinane (view profile)
Jonghoon Ryu (view profile)
lone Ishfaq (view profile)
I am not getting plot of the circle, can anyone help?
PraveenKumar M (view profile)
jigsaw (view profile)
works better than CircleFitByTaubin, CircleFitByPratt, and Landau_Smith, does not overfit
Aaron Greenbaum (view profile)
Richard Brown (view profile)
@Tolga, thanks -- and yes, I know. I deliberately wanted to avoid any toolbox dependencies.
I haven't done any comparisons of efficiency -- if you do you could let us know! But accuracy is simply dependent on the tolerance you specify.
Tolga Birdal (view profile)
Richard,
I appreciate your effort and this great function. I would just want to remind that (and I am sure you already know) for the nonlinear part it is also possible to use:
options = optimset('Jacobian','on', 'Algorithm','levenberg-marquardt');
u = lsqnonlin(@sys,u,[],[],options);
Did you do any comparisons of accuracy? (Because Walter states that Gauss-Newton performed similarly, while being more efficient)
Cheers,
Richard Brown (view profile)
My previous comment got lost -- the 2 norm residual is the sum of the squared perpendicular distance from each data point to the fitted circle
Richard Brown (view profile)
Also, @Graeme, the accuracy of the fitted circle depends on the kinds of errors that are in your data. If you assume there is a true underlying set of parameters that you're trying to find, and that your data is normally distributed, then the accuracy will decrease with number of data points as something like s / sqrt(n), where s is the standard deviation of perpendicular distances to the fitted circle, and n the number of data points. The error will then probably follow some kind of t distribution.
Sharif (view profile)
IGNORE last comment.
found your answer:-
http://www.mathworks.com/matlabcentral/fileexchange/15125-fitellipse-m
Sharif (view profile)
Hi Richard,
Thanks you for sharing your script.
i was wondering if it possible to modify your script to fit an ellipse. if so, do you have any suggestions?
thanks again,
sharif
Graeme Crouchley (view profile)
Hi all,
I was wondering what exactly the 2 norm residual is? Is it the sum of the residuals?
Also how would I use this value (or any other) to determine the accuracy of the fitted circle. i.e what accuracy has the radius of the circle been determined to?
Thanks,
Graeme
Daniel (view profile)
thanks,
Richard Brown (view profile)
@Daniel. Yes, it is very easy to do this, and you don't need my file (it has a simple solution).
Assume that you want to force the centre to be 0,0. (if something else, then you can just change coordinates to solve the problem, then change back afterward).
Let your points be in a 2xN array called x. Then the objective you're minimising is
f(r) = \sum_i (\norm{x_i} - r)^2
where x_i is the i-th column of x.
You can minimise using basic calculus
(1) f'(r) = -2\sum_i (\norm{x_i} - r) = -2\sum_i \norm{x_i} + 2Nr
(2) f''(r) = 2N
(1) has a unique minimiser at
r = \frac{\sum_i \norm{x_i}}{N}
and (2) confirms that the function is convex
i.e. in Matlab notation:
r = sum(hypot(x(1, :), x(2, :)) / N;
cheers,
Richard
Daniel (view profile)
Daniel (view profile)
Great and simple. Any idea if it would be easy to force the center to zero,zero (or anything other coordinate)?
Akash Yalagach (view profile)
Richard Brown (view profile)
Hi Brian
This code computes a least squares fit, which is not robust to large outliers - the results you got are not unexpected, nor are they wrong.
Because the objective being minimised is a function of the square of the distance, big outliers will skew the fit. If your data is known to have significant outliers, least squares is not a good choice of objective to minimise.
cheers,
Richard
Brian Wheeler (view profile)
To test thus function I fed it data for a 360 point unit circle. As expected it returned a unit circle centered on zero.
Distorting one point of the circle to a radius of 6 gave expected results:
radius 1.01408739256055
center [ 3.76743045176789e-17 0.0281775669475842]
Distorting the same point of the circle to a radius of 7 gave UNEXPECTED results:
radius 3.258619140676808
center [1.25049755875451e-15 3.17478833187820]
Thoughts anyone?
SengChoy Liew (view profile)
if i have a gray scale picture with circles of various diameter touching each other. How can i make use of this fitting to evaluate the distribution in histogram of circles in that pictures ?
I could not find any codes in image processing toolbox for it.
Anyone out there for help ?
Kristopher White (view profile)
Great work.
Levi Kilcher (view profile)
This function is fantastic!
Great work, thanks.
Documentation:
+Publis M-files application and theory (5)
+Graphical program interface (5)
+Workable program description (5)
+Works references (3)
Programming:
+M-files complexity (3)
+Mlint error message (5)
+Help problems (4)
+Filename of clashes (5)
+Clone the code line (5)
+Program's symbolic manuscript (3)
+Program samples and runing test (5)
+System requirements (5)
+Program running speed (4)
Files archive:
Seleceted file name (5)
Per files comment (5)
All files usefuly (4)
Thanks:
Very nice application and documentation.
Good works.