Code covered by the BSD License  

Highlights from
fitellipse.m

4.75

4.8 | 8 ratings Rate this file 160 Downloads (last 30 days) File Size: 44.8 KB File ID: #15125
image thumbnail

fitellipse.m

by

 

28 May 2007 (Updated )

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   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (25)
27 Sep 2014 Richard Clarke

Dear Richard, is there any particular language you would prefer when referencing your code in an article?

10 Jul 2014 Christian  
02 Mar 2014 Manuela

Hello Richard, if I want to use the 'a' and 'b' values given by this program to find the area of the ellipse what would be the units of the area? Pixels perhaps?

13 Feb 2014 Ross

Hi Richard, thank you for this code. Quick question: how can I estimate the error of the fit? Something like an R-value... Thank you!

05 Nov 2013 K

Hello,

This code was exactly what I was looking for to fit ellipses to a set of data points (hysteresis loops). I may have some questions in the future, but for now, thanks!

23 Aug 2013 Georgi

Hello,
First of all, my compliments about this work-excellent!
I am trying to write the code and have some questions because i am beginner with matlab.When i try to declare the function(function [z, a, b, alpha] = fitellipse(x, varargin)) i become an error like this:
Error: Function definitions are not permitted in this context.
Without this later i can't use the function because it isn't defined~!
I would be gratefull if you help me,because i really need this.

Thanks in advance

05 Mar 2013 leon

Richard Brown,
how to segment the images?

04 Mar 2013 Richard Brown

@leon, not directly. It fits ellipses to pairs of points that are assumed to represent the ellipse boundary. So, you need to segment your images first, then extract the boundary points and fit the ellipse to those.

27 Feb 2013 leon

Richard Brown,

your program can fitting an ellipse to mango image?

http://snag.gy/NbIES.jpg

http://snag.gy/tWFMb.jpg

http://snag.gy/b3X7E.jpg

http://snag.gy/lRlfX.jpg

http://snag.gy/fzAVq.jpg

http://snag.gy/qq5WN.jpg

http://snag.gy/bSjbb.jpg

http://snag.gy/aEvwq.jpg

http://snag.gy/S0VNB.jpg

http://snag.gy/6Q6uA.jpg

http://snag.gy/Ov0br.jpg

http://snag.gy/uRbKw.jpg

Thank you.

08 Oct 2012 Sepehr Farhand  
28 Jun 2012 Richard Brown

@Irfan: x and y values!

20 Jun 2012 Irfan

Hi Richard,
just a silly question about the mode of 2d data matrix. 1st & 2nd rows of x[] matrix are correspond to x and y values, or both rows represent y_min & y_max values resp. in the leading +x direction?

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.

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.

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

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

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.

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

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

17 Sep 2010 bear tiger

good!
Is there any reference for this algorithum

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

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

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');

19 Oct 2009 Kristopher White

Awesome work. Solid implementation, very useful.

25 Aug 2008 apiwat wisitty

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

Contact us