5.0

5.0 | 18 ratings Rate this file 191 downloads (last 30 days) File Size: 4.19 KB File ID: #3215

fit_ellipse

by Ohad Gal

 

31 Mar 2003 (Updated 02 Oct 2003)

Code covered by BSD License  

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

Download Now | 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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (21)
09 Apr 2003 Larry O'Neill

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

07 Jul 2003 Peter Delahunt

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

15 Jun 2004 P F

Does what it claims to. Quite helpful!

24 Aug 2004 Chaiyanan Sompong

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

26 Jan 2005 Eric Tittley  
28 Feb 2005 Antje _

Thanks!

13 May 2005 Gita Adur

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

18 Jul 2005 Willem Van der Merwe

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

22 Jan 2007 Heikki Suhonen

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

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

07 Sep 2007 Raimund Leitner

Incredible useful and practical m-file

12 Dec 2007 Roy Pur

THANKS

01 Mar 2008 a s  
21 May 2008 Daniel Nilsson

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

26 Jul 2008 Dave Peake

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

10 Oct 2008 Kevin Shaw

Love it. Works as advertised.

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)

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!

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

06 Feb 2009 Sophie

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

10 Oct 2009 Raymond Cheng

Thanks for your sharing.

Please login to add a comment or rating.
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.

02 Oct 2003

1. added a test to identify if the data is a hyperbola or parabola - returned in the "status" field
2. the routine finds now the center point of the original (tilted) ellipse as well (fields "X0_in","Y0_in")

Tag Activity for this File
Tag Applied By Date/Time
specialized Ohad Gal 22 Oct 2008 06:59:07
plotting Ohad Gal 22 Oct 2008 06:59:07
fit Ohad Gal 22 Oct 2008 06:59:07
ellipse Ohad Gal 22 Oct 2008 06:59:07
least squares Ohad Gal 22 Oct 2008 06:59:07
ls Ohad Gal 22 Oct 2008 06:59:07
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com