Code covered by the BSD License  

Highlights from
fitellipse.m

4.6

4.6 | 5 ratings Rate this file 50 Downloads (last 30 days) File Size: 44.84 KB File ID: #15125
image thumbnail

fitellipse.m

by Richard Brown

 

28 May 2007 (Updated 29 May 2007)

Fit ellipses to 2D points using linear or nonlinear least squares

| Watch this File

File Information
Description

There are two main methods for least squares ellipse fitting:
1) Minimise algebraic distance, i.e. minimise sum(F(x)^2) subject to some constraint, where F(x) = x'Ax + b'x + c

This is a linear least squares problem, and thus cheap to compute. There are many different possible constraints, and these produce different fits. fitellipse supplies two:
[z, a, b, al] = fitellipse(x, 'linear')
[z, a, b, al] = fitellipse(x, 'linear', 'constraint', 'trace')
See published demo file for more information.

2) Minimise geometric distance - i.e. the sum of squared distance from the data points to the ellipse. This is a more desirable fit, as it has some geometric meaning. Unfortunately, it is a nonlinear problem and requires an iterative method (e.g. Gauss Newton) to solve it. This is implemented as the default option in fitellipse. If it fails to converge, it fails gracefully (with a warning), returning the linear least squares estimate used to derive the start value

[z, a, b, alpha] = fitellipse(x)

plotellipse(z, a, b, alpha) can be used to plot the fitted ellipses

MATLAB release MATLAB 7.1.0 (R14SP3)
Other requirements R14 or later - uses nested functions, true false booleans
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (13)
25 Aug 2008 apiwat wisitty

It's quite fantastic. I'm studying this algorithm. This function help me much. ;-)

19 Oct 2009 Kristopher White

Awesome work. Solid implementation, very useful.

19 Apr 2010 Jenn Risk

When I run the demo file (using 2007a), I get an error message:

??? Error using ==> minus
Matrix dimensions must agree.

Error in ==> fitellipse at 87
x = x - repmat(centroid, 1, length(x));

Error in ==> ellipsedemo at 58
[zb, ab, bb, alphab] = fitellipse(x, 'linear');

21 Apr 2010 Richard Brown

Hi Jenn

I'll investigate this ... I'm not sure if I have access to a copy of 2007a, but I'll see if I can find one. It definitely works on newer versions!

regards,

Richard

29 Apr 2010 GAURAV

Hi!! I have one doubt.. Will it work if I have just four points in X-Y system? Because here it said that it has to have more than 5 points?

Regards,
Gaurav

17 Sep 2010 bear tiger

good!
Is there any reference for this algorithum

16 Dec 2010 nancy

Hi,Richard Brown
       I have read your code( fitellipse.m )on matlabcentral. Now I have some puzzle about ellipse detection in an image:
1) How can I get the point coordinate from an image? I have tried to use the edge function,but the result is not satisfactory.
2) If detect all edge points in an image, there are lots of points ,how can I select suitable points as input of the fitellipse function?
 
       
regards,

                    Lei Q

14 Mar 2011 Felipe Pedreros

Incredible implementation Richard, but I'd like to know the errors of the parameters. Maybe these are obvious but I can't find the way to get it.
Could you help me?

Regards,
Felipe

14 Mar 2011 Richard Brown

@nancy:
1. Not entirely sure what you mean, but if you've used edge to find the edges of your ellipse, then you can use find on the resulting black and white image to get the coordinates of the edge pixels. 2. You can fit an ellipse to all of the edge pixels if you like, you don't need to choose a subset.

@Felipe: No they're not obvious. I'll get back to you with a better answer a bit later - I'm from Christchurch New Zealand, and my university has its hands full getting our teaching back underway after the earthquake a few weeks ago.

23 May 2011 Felipe Pedreros

Hi Richard,

I understand your situation very well... my University was also down after our earthquake last year. I hope all is going well now in NZ.
Concerning my question, I'd like to ask if a modification to the inputs is possible, by leave fixed some of the parameters like the center (x0, y0) of the ellipse. The same for the your code fitcircle.m

Best regards
Felipe

19 Jan 2012 Pierre Roduit

First, thank you for this code.
Just a comment on this part of this code.

if ishandle(varargin{1})
    hAx = varargin{1};
    varargin(1) = [];
else
    hAx = gca();
end

If a figure 1 is open and you try to draw an ellipse with a center at [1,1], it will take take these coordinates as a reference. Not an obvious error...

20 Jan 2012 Valeria Rodríguez

Hi. It seems the code sometimes switch the major and minor axes. This is problematic for plotting. Besides this, the code is very useful.

Greetings.

25 Jan 2012 Richard Brown

@Pierre: Thanks, good catch! I'll put a new one up shortly that checks explicitly for an axes handle.

@Valeria: The order of the axes is not specified - there is no guarantee that the first will be the major axis. If you want to enforce this it's a trivial modification.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
approximation Richard Brown 22 Oct 2008 09:13:52
interpolation Richard Brown 22 Oct 2008 09:13:52
least squares Richard Brown 22 Oct 2008 09:13:52
ellipse Richard Brown 22 Oct 2008 09:13:52
fit Richard Brown 22 Oct 2008 09:13:52
conic Richard Brown 22 Oct 2008 09:13:52

Contact us at files@mathworks.com