File Exchange

image thumbnail

fitellipse.m

version 1.0 (85 KB) by

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

4.72222
20 Ratings

66 Downloads

Updated

View License

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

Comments and Ratings (38)

Bumju Kim

tanks for this code

plasmageek

This 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 Khatri

This 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 Khatri

plasmageek

Sibel Cakir

Great 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 Jessop

Works like a charm.

John

John (view profile)

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

kabla002

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

Dear 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!

Fritz

Fritz (view profile)

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

Christian

Manuela

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

Ross

Ross (view profile)

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

K

K (view profile)

Hello,

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!

Georgi

Georgi (view profile)

Hello,
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

leon

leon (view profile)

Richard Brown,
how to segment the images?

Richard Brown

Richard Brown (view profile)

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

Richard Brown

Richard Brown (view profile)

@Irfan: x and y values!

Irfan

Irfan (view profile)

Hi 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

Richard Brown (view profile)

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

Hi. 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 Roduit

First, 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...

Hi 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

Richard Brown (view profile)

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

Incredible 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

nancy

nancy (view profile)

Hi,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 tiger

good!
Is there any reference for this algorithum

GAURAV

GAURAV (view profile)

Hi!! 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 Brown

Richard Brown (view profile)

Hi 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 Risk

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

Awesome work. Solid implementation, very useful.

apiwat wisitty

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

Updates

1.0

MathWorks update: Added Live Script.

MATLAB Release
MATLAB 9.0 (R2016a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video