Code covered by the BSD License  

Highlights from
fit_ellipse

5.0

5.0 | 41 ratings Rate this file 171 Downloads (last 30 days) File Size: 4.19 KB File ID: #3215
image thumbnail

fit_ellipse

by

 

31 Mar 2003 (Updated )

Find the best fit for an ellipse using a given set of points (a closed contour).

| Watch this File

File Information
Description

This function uses the Least-Squares criterion for estimation of the best fit to an ellipse from a given set of points (x,y). The LS estimation is done for the conic representation of an ellipse (with a possible tilt).

Conic Ellipse representation = a*x^2+b*x*y+c*y^2+d*x+e*y+f=0
  (Tilt/orientation for the ellipse occurs when the term x*y exists (i.e. b ~= 0))
 
 Later, after the estimation, the tilt is removed from the ellipse (using a rotation matrix) and then, the rest of the parameters which describes an ellipse are extracted from the conic representation.

For debug purposes, the estimation can be drawn on top of a given axis handle.

Note:
1) This function does not work on a three-dimensional axis system. (only 2D)
2) At least 5 points are needed in order to estimate the 5 parameters of the ellipse.
3) If the data is a hyperbola or parabula, the function return empty fields and a status indication

MATLAB release MATLAB 6.1 (R12.1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (51)
09 Jul 2014 Christian

For my tasks this function works better than the others I've tried so far. Thank you very much Ohad!!

@Aritra: To run this function on a binary image you have to run:
[X Y] = ind2sub(size(img),find(img));
E = fit_ellipse(x,y);

then you can do:
if E.long_axis > 0
[X, Y] = calcEllipse(E, 360);
end

To plot it the ellipse:
plot(Y, X);

The function calcEllipse:
function [X,Y] = calcEllipse(varargin)
% function [X,Y] = calculateEllipse(x, y, a, b, angle, steps)
%# This functions returns points to draw an ellipse
%#
%# @param x X coordinate
%# @param y Y coordinate
%# @param a Semimajor axis
%# @param b Semiminor axis
%# @param angle Angle of the ellipse (in rad)
%#
% Source: http://stackoverflow.com/questions/2153768/draw-ellipse-and-ellipsoid-in-matlab/24531259#24531259
% Modified by Christian Fässler

steps = 360;

if nargin == 1 || nargin == 2
x = varargin{1}.X0_in;
y = varargin{1}.Y0_in;
a = varargin{1}.a;
b = varargin{1}.b;
angle = varargin{1}.phi;
if nargin == 2
steps = varargin{2};
end
else if nargin == 5 || nargin == 6
x = varargin{1};
y = varargin{2};
a = varargin{3};
b = varargin{4};
angle = varargin{5};
if nargin == 6
steps = varargin{6};
end
else
error('Wrong input');
end
end

beta = -angle;
sinbeta = sin(beta);
cosbeta = cos(beta);

alpha = linspace(0, 2*pi, steps)';
sinalpha = sin(alpha);
cosalpha = cos(alpha);

X = round(x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta));
Y = round(y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta));

if nargout==1, X = [X Y]; end
end

07 Jul 2014 Aritra Das

Can anyone provide a clear idea about how to implement this practically?
For example if I want to find ellipses in an binary image say bw, how to run this code on the image to get the ellipses?
As I see there is no way to provide the matrix name as a input argument.
And if somebody could explain the input arguments "Input: x,y - a set of points in 2 column vectors. AT LEAST 5 points are needed !"
I mean this statement a bit elaborately it will be very helpful.

13 Dec 2013 Marcello

Great code, very useful!
However, I found a bug related to the orientation of the ellipse and I read some people had the same problem. The author wrote which "to correct that, the test for the orientation_tolerance should be normalized".
How can I solve the problem? May anyone help me? I would be very grateful.
Thanks in advantage, and sorry for my English

15 Oct 2013 dingding

thank you very much! very good work! help me so much!

30 Jul 2013 Shuqing

Thank you very much! Very helpful.

24 Apr 2013 Amir  
23 Nov 2012 Luigi Sanguigno

many thanks !!

05 Oct 2012 Martina Callaghan  
17 Apr 2012 Jeff Anderson

Great code! Works great...I'll have to spend some time with it. For my problem, I need to force the origin to 0,0 and orthogonal axes.

17 Apr 2012 Jeff Anderson  
06 Mar 2012 Navneet Viswan

I'm n0t able to plot the output results..Where do i go wrong?
%%
h=plot(ydata,zdata);
ellipse_t = fit_ellipse(ydata,zdata,h)

12 Feb 2012 Gilad Kapel  
21 Dec 2011 Amit Ruf

Nice code, however, you probably have a bug related to the orientation of the ellipse, see:

http://www.mathworks.com/matlabcentral/fileexchange/22423

I fixed it and added a computation of the residual of the fit which provides a quality measure for the fit.
If you want my version, please mail to:
amitruf@gmail.com

04 Nov 2011 Paulo

How to use this? I mean, the inputs x and y? Can I have an example? Thanks! This is very useful for my project!

17 Oct 2011 David Scaduto  
22 Aug 2011 Steven Dakin

Works out of the box. Very useful. Thanks.

14 Apr 2011 Hassan Naseri

Excellent deployment, easy to use and well commented ... THANKS

13 Feb 2011 Mohammed El-Said

Works Great,
Thanks for sharing...

28 Sep 2010 Tima Tima

Thank you. It is vary helped for my work

16 Sep 2010 Thanos

Hi, I am using the script and it works fine for my data. I am trying to work the math a little bit and I am struggling at one point. why we assume that the f=-1? As far as I can see from the code, the value we assign to f affects the a, b, c, d, e which in turn affect the ellipse properties. Is that something mathematically trivial? I am looking forward for your answers
Cheers
Thanos

14 Sep 2010 bear tiger  
10 Sep 2010 Rafal

Hello. Can someone help me and give description of algorith used in this matlab code. Maybe a link to publication. If someone has also block scheme of this algorithm send it to me please. My mail :
Monter70@gmail.com

Best Regards

06 Sep 2010 Rafal

Hello i use this script and it's working perfectly,but unfortunately i have one problem ...

I load signals ,it draws an ellipse , when i want to put near ellipse points to which there is aproximation they are moved. I think it could be connected with new coordinates for ellipse. cause i use cmd plot(x,y,'b'); and there is ideal bow as ellipse has,but it's moved. Could anyone helps me?

13 Aug 2010 Rakesh

Thanks a lot!! nice work!

23 Jun 2010 Roy

thanks for sharing.

23 Jun 2010 Rafal

Hello. I get all the ellipse parameters.I load two orthogonal signals as a x=b1ch3(:,1), y=b1ch4(:,1), there is problem because it's not drawing and fitting ellipse to this points.

14 May 2010 scaramanga

I'm using your function in a loop, is it possible to draw ellipses with a different color at every iteration of the loop ?

16 Mar 2010 Mustafa  
25 Dec 2009 Evgeny Pr  
03 Dec 2009 Sieun

thank you :D

10 Oct 2009 Raymond Cheng

Thanks for your sharing.

06 Feb 2009 Sophie

I found.. it's just a question of angle: should have done:
t = - ellipse_t.phi;

05 Feb 2009 Sophie

@Samuel: did you resolve your problem?
I do not know your exact problem but maybe what you want is this:
handle= subplot(221); % or something like this
ellipse_t = fit_ellipse(x,y,handle); % the ellipse should be drawn on the subplot else your ellipse is out of range if I am not wrong

05 Feb 2009 Sophie

Thanks a lot for this script which is really useful!
I have one question because I get into trouble: I would like to get all pixels that are inside the ellipse.. I was doing something like that but it does not work:

x0 = ellipse_t.X0_in;
y0 = ellipse_t.Y0_in;
a = ellipse_t.a;
b = ellipse_t.b;
t = ellipse_t.phi;
for x=1:size(I,1)
for y=1:size(I,2)
X = (x-x0)*cos(t)+(y-y0)*sin(t);
Y = -(x-x0)*sin(t)+(y-y0)*cos(t);
if (X^2/a^2+Y^2/b^2)>1 % outside ellipse
I(y,x) = 0;
end
end
end

Any idea? Thanks in advance!

19 Jan 2009 Samuel

Hi, I have one question.... When a run the code the program shows the ellipse result, but don't plot the graphic with the points and ellipse fit curve (the same graphic above). I would like to see the fit curve, but I don't know whats is the problem... Please, anybody help-me.... (sorry my english)

10 Oct 2008 Kevin Shaw

Love it. Works as advertised.

26 Jul 2008 Dave Peake

Exactly what I was after, perfect. Does what it says, does it well and easily.

21 May 2008 Daniel Nilsson

Awesome! I just had to modify the program to plot the non-rotated ellipse instead, otherwise this was perfect!

01 Mar 2008 a s  
12 Dec 2007 Roy Pur

THANKS

07 Sep 2007 Raimund Leitner

Incredible useful and practical m-file

14 Feb 2007 Sophie Jarlier

It's grest to have your code! Just a question: I randomly selected 5points and execute your function but I get the following error:
In fit_ellipse at 155
stopped because of a warning regarding matrix inversion
But I have 4 points that have same y=320 which is the maximum size of my image.. Should these 5points have 5different y and 5different x?
Thanks for your answer
Sophie

22 Jan 2007 Heikki Suhonen

Works well for me. Easy interface, and nice optional visualization.

18 Jul 2005 Willem Van der Merwe

Great. The plot function was a great help as well.

13 May 2005 Gita Adur

Execellent!!! Very well written and extremely easy to use.

28 Feb 2005 Antje _

Thanks!

26 Jan 2005 Eric Tittley  
24 Aug 2004 Chaiyanan Sompong

Thank you. In present-day, i do my research about face detection in color image.

15 Jun 2004 P F

Does what it claims to. Quite helpful!

07 Jul 2003 Peter Delahunt

Thanks Ohad - it works well with color discrimination data. Best wishes, Peter

09 Apr 2003 Larry O'Neill

This method works excellent with all my noisy data. Also well written and extremely easy to use.

Updates
21 Apr 2003

for ellipse with large axes, parameters "a" and/or "b", the fitting might not locate the orientation of the ellipse, especially if the angle is small.

to correct that, the test for the orientation_tolerance should be normalized.

Contact us