Code covered by the BSD License  

Highlights from
fitcircle.m

4.6

4.6 | 5 ratings Rate this file 35 Downloads (last 30 days) File Size: 39.3 KB File ID: #15060
image thumbnail

fitcircle.m

by

 

20 May 2007 (Updated )

Fits circles to 2D data using nonlinear least squares to minimise geometric error

| Watch this File

File Information
Description

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

MATLAB release MATLAB 7.1.0 (R14SP3)
Other requirements Should work for R14 onwards. Changes to make it work for earlier releases are probably fairly straightforward (e.g. replacing boolean false and true with 0, 1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (18)
07 Sep 2012 Richard Brown

@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.

19 Aug 2012 Tolga Birdal

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,

21 May 2012 Richard Brown

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

21 May 2012 Richard Brown

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.

10 May 2012 Sharif

IGNORE last comment.
found your answer:-
http://www.mathworks.com/matlabcentral/fileexchange/15125-fitellipse-m

10 May 2012 Sharif

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

07 May 2012 Graeme Crouchley

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

04 Apr 2012 Daniel

thanks,

01 Apr 2012 Richard Brown

@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

29 Mar 2012 Daniel  
29 Mar 2012 Daniel

Great and simple. Any idea if it would be easy to force the center to zero,zero (or anything other coordinate)?

19 Mar 2012 Akash Yalagach  
18 Sep 2011 Richard Brown

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

30 Aug 2011 Brian Wheeler

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?

26 Apr 2010 SengChoy Liew

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 ?

19 Oct 2009 Kristopher White

Great work.

23 Mar 2009 Levi Kilcher

This function is fantastic!

Great work, thanks.

23 May 2007 Ali Özgül

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.

Contact us