Code covered by the BSD License  

Highlights from
Ellipse Fit (Direct method)

3.0

3.0 | 2 ratings Rate this file 62 Downloads (last 30 days) File Size: 1.89 KB File ID: #22684
image thumbnail

Ellipse Fit (Direct method)

by Nikolai Chernov

 

14 Jan 2009 (Updated 16 Jan 2009)

Fits an ellipse to a set of points on a plane; returns the coefficients of the ellipse's equation

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

This is a fast and non-iterative ellipse fit. Usage:

   A = EllipseDirectFit(XY)

Input: XY(n,2) is the array of coordinates of n points
            x(i)=XY(i,1), y(i)=XY(i,2)

 Output: A = [a b c d e f]' is the vector of coefficients
 of the equation of the best fitting ellipse:

   ax^2 + bxy + cy^2 + dx + ey + f = 0,

To convert this vector to the geometric parameters (semi-axes, center, etc.) use standard formulas, e.g., (19) - (24) in Wolfram Mathworld:
       http://mathworld.wolfram.com/Ellipse.html

This ellipse fit was proposed in article
A. W. Fitzgibbon, M. Pilu, R. B. Fisher
"Direct Least Squares Fitting of Ellipses"
IEEE Trans. PAMI, Vol. 21, pages 476-480 (1999)

The authors called it "Direct Ellipse Fit".

My code is based on a numerically stable version
of this fit published by R. Halir and J. Flusser. I only
added data centering to further improve performance.

Note: this fit returns ellipses only! You will get an ellipse
even if points can be better approximated by a hyperbola.

This fit is somewhat biased toward smaller ellipses.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Ellipse Fit

MATLAB release MATLAB 6.0 (R12)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
16 Jan 2009 OH

??? Input argument "XY" is undefined.

Error in ==> EllipseDirectFit at 17
centroid = mean(XY); % the centroid of the data set
 
??? Input argument "XY" is undefined.

Error in ==> EllipseDirectFit at 17
centroid = mean(XY); % the centroid of the data set

16 Jan 2009 Nikolai Chernov

from Author: XY is the Nx2 input array of coordinates of N points,
    x(i)=XY(i,1), y(i)=XY(i,2). To use the program, type
      A = EllipseDirectFit(XY)

30 Apr 2009 Francisco

The parameters in your equation (i.e.: a, b, c, d, e and f) do not fit with equation 15 in Wolfram's Mathworld.
This needs further explanation.

04 Sep 2009 Laurens

The Author (Fitzgibbon) posted some new code of his implementation at his site:

http://research.microsoft.com/en-us/um/people/awf/ellipse/

might be of interest

06 Apr 2010 Faten

I tried to use this code for image and it did not work
Normal jpge image , binary image and gray image all faild

23 Apr 2010 Ed Shen

The code works, however you must divde A(2), A(4) and A(5) by 2 to be able to use it with the Mathworld equations. I've verified with points given in http://research.microsoft.com/en-us/um/people/awf/ellipse/ and the results are almost identical

29 Apr 2010 GAURAV

I have three points say (a1,b1),(a2,b2),(a3,b3)..
How do I paas this as input?? Please let me know..

Appreciate your help.

Thank You,
Gaurav.

14 Sep 2010 Shriniket Sarkar

I tried using an XY array of
575 562
589 201
495 614
799 443
729 540
Using this set it provides exponential values for a,b and c return parameters and we cant use these parameters for a plot.

Is there something i am missing in this ? Can someone help?

23 Apr 2012 ICESI

Excellent implementation. Some people want to see the plot of the fit, you just have to add the following code in the end of the script.

hold on

%Convert the A to str
a = num2str(A(1));
b = num2str(A(2));
c = num2str(A(3));
d = num2str(A(4));
e = num2str(A(5));
f = num2str(A(6));

%Equation
eqt= ['(',a, ')*x^2 + (',b,')*x*y + (',c,')*y^2 + (',d,')*x+ (',e,')*y + (',f,')'];
xmin=0.7*min(XY(:,1));
xmax=1.3*max(XY(:,2));
ezplot(eqt,[xmin,xmax])
scatter(XY(:,1),XY(:,2))
hold off

Please login to add a comment or rating.
Updates
16 Jan 2009

Added the description of input and output.

Tag Activity for this File
Tag Applied By Date/Time
image processing Nikolai Chernov 15 Jan 2009 14:34:20
statistics Nikolai Chernov 15 Jan 2009 14:34:20
optimization Nikolai Chernov 15 Jan 2009 14:34:20
least squares Nikolai Chernov 15 Jan 2009 14:34:20
potw Shari Freedman 20 Apr 2009 12:44:06
pick of the week Jiro Doke 11 Feb 2011 20:12:17

Contact us at files@mathworks.com