version 1.0.0.0 (85 KB) by
Richard Brown

Fit ellipses to 2D points using linear or nonlinear least squares

There are two main methods for least squares ellipse fitting:

1) Minimise algebraic distance, i.e. minimise sum(F(x)^2) subject to some constraint, where F(x) = x'Ax + b'x + c

This is a linear least squares problem, and thus cheap to compute. There are many different possible constraints, and these produce different fits. fitellipse supplies two:

[z, a, b, al] = fitellipse(x, 'linear')

[z, a, b, al] = fitellipse(x, 'linear', 'constraint', 'trace')

See published demo file for more information.

2) Minimise geometric distance - i.e. the sum of squared distance from the data points to the ellipse. This is a more desirable fit, as it has some geometric meaning. Unfortunately, it is a nonlinear problem and requires an iterative method (e.g. Gauss Newton) to solve it. This is implemented as the default option in fitellipse. If it fails to converge, it fails gracefully (with a warning), returning the linear least squares estimate used to derive the start value

[z, a, b, alpha] = fitellipse(x)

plotellipse(z, a, b, alpha) can be used to plot the fitted ellipses

Richard Brown (2021). fitellipse.m (https://www.mathworks.com/matlabcentral/fileexchange/15125-fitellipse-m), MATLAB Central File Exchange. Retrieved .

Created with
R2016a

Compatible with any release

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Renyuan LIULns Kwhat's about a subpixel accuracy, ? I would like know the paper with which you were elaborated this good work

Paul KaneAjay KBumju Kimtanks for this code

plasmageekThis works pretty well most of the time. I found it got hung up on a complicated data set (lots of noise/uncertainty in the exact edges). I'm not sure how to fix that issue or why it happened, and had to switch to a difference fitting program.

Nilay KhatriThis is a great contribution to the routines available on FEX for ellipse fitting. However, I have a small request to make and would appreciate if someone could respond to it.

I am using nonlinear method for ellipse fitting and so interested in knowing the value for the minimum geometric error the algorithm has estimated. Can any one point out how I can extract the same from this routine?

Nilay KhatriplasmageekSibel CakirGreat Work! Thank you very much!

I need to detect ellipses for example in the picture which is in the attached link. How can I use your code to do that?

https://de.mathworks.com/matlabcentral/answers/311011-detection-of-ellipses

Thank you very much in advance!

David JessopWorks like a charm.

JohnVery complete and well documented. Easy for grab and go usage. My first use is with input data from the perimeter of a circle measured by camera where there are missing data points to complete the perimeter. The fit and plot routines together give me what I need to move forward.

kabla002Indra Ginanjar A.TFor glaucoma diagnosis it is common to determine a "cup to disk ratio" which compares the diameter of the optic disk (VDD) and optic cup (VCD). The optical disk is visible as a circular red feature (red channel) and the optic cup shows up as a yellow circle (green channel). How can I calculate the diameter ratio between optic disk and optic cup?

I am able to detect the optic disk with canny edge detection, but I have not found a way to calculate the cup to disk ratio. How might I do that?

<</matlabcentral/answers/uploaded_files/40421/2-5.jpg>>

HARSHA BIKKAVILLIDear Richard,this exactly helps my purpose. But i am not sure how to get data points of a binary image in the matrix format you mentioned in your code? I have converted an image into a binary image and was struck in finding the data points in the required format. Please help!

Davide CostamagnaFritzRichard ClarkeDear Richard, is there any particular language you would prefer when referencing your code in an article?

ChristianManuelaHello Richard, if I want to use the 'a' and 'b' values given by this program to find the area of the ellipse what would be the units of the area? Pixels perhaps?

RossHi Richard, thank you for this code. Quick question: how can I estimate the error of the fit? Something like an R-value... Thank you!

KHello,

This code was exactly what I was looking for to fit ellipses to a set of data points (hysteresis loops). I may have some questions in the future, but for now, thanks!

GeorgiHello,

First of all, my compliments about this work-excellent!

I am trying to write the code and have some questions because i am beginner with matlab.When i try to declare the function(function [z, a, b, alpha] = fitellipse(x, varargin)) i become an error like this:

Error: Function definitions are not permitted in this context.

Without this later i can't use the function because it isn't defined~!

I would be gratefull if you help me,because i really need this.

Thanks in advance

leonRichard Brown,

how to segment the images?

Richard Brown@leon, not directly. It fits ellipses to pairs of points that are assumed to represent the ellipse boundary. So, you need to segment your images first, then extract the boundary points and fit the ellipse to those.

leonRichard Brown，

your program can fitting an ellipse to mango image?

http://snag.gy/NbIES.jpg

http://snag.gy/tWFMb.jpg

http://snag.gy/b3X7E.jpg

http://snag.gy/lRlfX.jpg

http://snag.gy/fzAVq.jpg

http://snag.gy/qq5WN.jpg

http://snag.gy/bSjbb.jpg

http://snag.gy/aEvwq.jpg

http://snag.gy/S0VNB.jpg

http://snag.gy/6Q6uA.jpg

http://snag.gy/Ov0br.jpg

http://snag.gy/uRbKw.jpg

Thank you.

Sepehr FarhandRichard Brown@Irfan: x and y values!

IrfanHi Richard,

just a silly question about the mode of 2d data matrix. 1st & 2nd rows of x[] matrix are correspond to x and y values, or both rows represent y_min & y_max values resp. in the leading +x direction?

Richard Brown@Pierre: Thanks, good catch! I'll put a new one up shortly that checks explicitly for an axes handle.

@Valeria: The order of the axes is not specified - there is no guarantee that the first will be the major axis. If you want to enforce this it's a trivial modification.

Valeria Rodríguez-FajardoHi. It seems the code sometimes switch the major and minor axes. This is problematic for plotting. Besides this, the code is very useful.

Greetings.

Pierre RoduitFirst, thank you for this code.

Just a comment on this part of this code.

if ishandle(varargin{1})

hAx = varargin{1};

varargin(1) = [];

else

hAx = gca();

end

If a figure 1 is open and you try to draw an ellipse with a center at [1,1], it will take take these coordinates as a reference. Not an obvious error...

Felipe PedrerosHi Richard,

I understand your situation very well... my University was also down after our earthquake last year. I hope all is going well now in NZ.

Concerning my question, I'd like to ask if a modification to the inputs is possible, by leave fixed some of the parameters like the center (x0, y0) of the ellipse. The same for the your code fitcircle.m

Best regards

Felipe

Richard Brown@nancy:

1. Not entirely sure what you mean, but if you've used edge to find the edges of your ellipse, then you can use find on the resulting black and white image to get the coordinates of the edge pixels. 2. You can fit an ellipse to all of the edge pixels if you like, you don't need to choose a subset.

@Felipe: No they're not obvious. I'll get back to you with a better answer a bit later - I'm from Christchurch New Zealand, and my university has its hands full getting our teaching back underway after the earthquake a few weeks ago.

Felipe PedrerosIncredible implementation Richard, but I'd like to know the errors of the parameters. Maybe these are obvious but I can't find the way to get it.

Could you help me?

Regards,

Felipe

nancyHi,Richard Brown

I have read your code( fitellipse.m )on matlabcentral. Now I have some puzzle about ellipse detection in an image:

1) How can I get the point coordinate from an image? I have tried to use the edge function,but the result is not satisfactory.

2) If detect all edge points in an image, there are lots of points ,how can I select suitable points as input of the fitellipse function?

regards,

Lei Q

bear tigergood!

Is there any reference for this algorithum

GAURAVHi!! I have one doubt.. Will it work if I have just four points in X-Y system? Because here it said that it has to have more than 5 points?

Regards,

Gaurav

Richard BrownHi Jenn

I'll investigate this ... I'm not sure if I have access to a copy of 2007a, but I'll see if I can find one. It definitely works on newer versions!

regards,

Richard

Jenn RiskWhen I run the demo file (using 2007a), I get an error message:

??? Error using ==> minus

Matrix dimensions must agree.

Error in ==> fitellipse at 87

x = x - repmat(centroid, 1, length(x));

Error in ==> ellipsedemo at 58

[zb, ab, bb, alphab] = fitellipse(x, 'linear');

Kristopher WhiteAwesome work. Solid implementation, very useful.

apiwat wisittyIt's quite fantastic. I'm studying this algorithm. This function help me much. ;-)