This is a fast non-iterative ellipse fit, and among fast non-iterative ellipse fits this is the most accurate and robust.
It takes the xy-coordinates of data points, and returns the coefficients of the equation of the ellipse:
ax^2 + bxy + cy^2 + dx + ey + f = 0,
i.e. it returns the vector A=(a,b,c,d,e,f). To convert this vector to the geometric parameters (semi-axes, center, etc.), use standard formulas, see e.g., (19) - (24) in Wolfram Mathworld: http://mathworld.wolfram.com/Ellipse.html
This fit was proposed by G. Taubin in article "Estimation Of Planar Curves, Surfaces And Nonplanar Space Curves Defined By Implicit Equations, With Applications To Edge And Range Image Segmentation", IEEE Trans. PAMI, Vol. 13, pages 1115-1138, (1991).
Note: this method fits a quadratic curve (conic) to a set of points; if points are better approximated by a hyperbola, this fit will return a hyperbola. To fit ellipses only, use "Direct Ellipse Fit".
thanks very much
This worked very well, thanks, and thanks also to David Christian Berg for his conversion code.
Would it be possible to also calculate and output the MSE (goodness of fit)?
Great file. Very reliable fit to almost every data, even the smallest section of an ellipse.
I've got a small question though:
Is it possible to modify the script to fit to hyperbolas only?
I'd be more than thankful for any pointers.
Well, found it a bit impractical to not have the code for conversion standard form here. That's why I would like to share it:
A = EllipseFitByTaubin(points);
a = A(1); b = A(2)/2; c = A(3); d = A(4)/2; f = A(5)/2; g = A(6);
center(1) = (c*d - b*f)/(b^2-a*c);
center(2) = (a*f - b*d)/(b^2-a*c);
sem(1) = sqrt( 2*(a*f^2+c*d^2+g*b^2-2*b*d*f-a*c*g) / ((b^2-a*c)*(sqrt((a-c)^2+4*b^2)-(a+c))));
sem(2) = sqrt( 2*(a*f^2+c*d^2+g*b^2-2*b*d*f-a*c*g) / ((b^2-a*c)*(-sqrt((a-c)^2+4*b^2)-(a+c))));
if b == 0 && a < c
phi = 0;
elseif b == 0 && a > c
phi = 0.5*pi;
elseif b ~= 0 && a < c
phi = 0.5* acot((a-c)/(2*b));
phi = 0.5*pi + 0.5* acot((a-c)/(2*b));
Hope this makes it a bit faster to implement.
The information from Ed Shen (in the comments about Ellipse Fit (Direct method)) is crucial for this function too: "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.
Inspired by: Ellipse Fit