File Exchange

image thumbnail

Ellipse Fit (Direct method)

version (1.89 KB) by Nikolai Chernov
Fits an ellipse to a set of points on a plane; returns the coefficients of the ellipse's equation


Updated 16 Jan 2009

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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:

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.

Cite As

Nikolai Chernov (2020). Ellipse Fit (Direct method) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (24)

Jan Keller

Yongming Ge

I'm wondering the Value of R-square???

zhang xw

Thank you!!!


I was wondering if this code can be used for 3D ellipse fitting. When our initial points are X Y


Nice ellipse fitting code

In addition to my comment at

Is it right to assume that it would be sufficient to change the "discriminant condition" of this file

4*evec(1,:).*evec(3,:) - evec(2,:).^2


- 4*evec(1,:).*evec(3,:) + evec(2,:).^2

in order to fit exclusively to hyperbolae?

Does anybody know another method?

Thanks for any help!


Thanks for the implementation and bringing my atention to this direct fit approach.



I get a filled structure, but somehow it doesn't draw the plot. Am I missing something?


The information from Ed Shen is crucial: "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."
Without these divisions the rusulting elippse does not really fit to the points.



I am trying to use this file to fit an ellipse to an elliptical hysteresis loop from data points in a 2X200 array. I get the error "Attempted to access A(4); index out of bounds because numel(A)=0." I am not sure what the problem is- can you help?





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

eqt= ['(',a, ')*x^2 + (',b,')*x*y + (',c,')*y^2 + (',d,')*x+ (',e,')*y + (',f,')'];
hold off

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?


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,

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 and the results are almost identical


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


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

might be of interest


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.

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)


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


Added the description of input and output.

MATLAB Release Compatibility
Created with R12
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Ellipse Fit