File Exchange

## Ellipse Fit (Direct method)

version 1.1.0.0 (1.89 KB) by Nikolai Chernov

### Nikolai Chernov (view profile)

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

Updated 16 Jan 2009

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

### Cite As

Nikolai Chernov (2020). Ellipse Fit (Direct method) (https://www.mathworks.com/matlabcentral/fileexchange/22684-ellipse-fit-direct-method), MATLAB Central File Exchange. Retrieved .

Jan Keller

Yongming Ge

### Yongming Ge (view profile)

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

zhang xw

Thank you!!!

Mahsa

### Mahsa (view profile)

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

Will

### Will (view profile)

Nice ellipse fitting code

Mathias Funk

### Mathias Funk (view profile)

In addition to my comment at http://www.mathworks.com/matlabcentral/fileexchange/22683-ellipse-fit--taubin-method-

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

to

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

in order to fit exclusively to hyperbolae?

Does anybody know another method?

Thanks for any help!

Leo

### Leo (view profile)

Przemyslaw Baranski

### Przemyslaw Baranski (view profile)

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

Po-Yu

Tim

### Tim (view profile)

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

Christian

### Christian (view profile)

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.

K

### K (view profile)

Hello,

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?

Gavin

Nice

Leo

LuisCardona

### LuisCardona (view profile)

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

Shriniket Sarkar

### Shriniket Sarkar (view profile)

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?

GAURAV

### GAURAV (view profile)

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

Thank You,
Gaurav.

Ed Shen

### Ed Shen (view profile)

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

Faten

### Faten (view profile)

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

Laurens

### Laurens (view profile)

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

Francisco

### Francisco (view profile)

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.

Nikolai Chernov

### Nikolai Chernov (view profile)

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)

OH

### OH (view profile)

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