Code covered by the BSD License

### Highlights from fitellipse.m

4.85714
4.9 | 14 ratings Rate this file 104 Downloads (last 30 days) File Size: 85 KB File ID: #15125 Version: 1.0

# fitellipse.m

### Richard Brown (view profile)

28 May 2007 (Updated )

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

File Information
Description

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

MATLAB release MATLAB 9.0 (R2016a)
MATLAB Search Path
```/
/demo
/demo/html
/test```
Other requirements R14 or later - uses nested functions, true false booleans
15 Jun 2016 David Jessop

### David Jessop (view profile)

Works like a charm.

29 Feb 2016 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.

04 Dec 2015 kabla002

### kabla002 (view profile)

18 Nov 2015 Indra Ginanjar A.T

### Indra Ginanjar A.T (view profile)

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?

Comment only
18 Oct 2015 HARSHA BIKKAVILLI

### HARSHA BIKKAVILLI (view profile)

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!

20 Mar 2015 Davide Costamagna

### Davide Costamagna (view profile)

11 Dec 2014 Fritz

### Fritz (view profile)

27 Sep 2014 Richard Clarke

### Richard Clarke (view profile)

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

Comment only
10 Jul 2014 Christian

### Christian (view profile)

02 Mar 2014 Manuela

### Manuela (view profile)

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?

13 Feb 2014 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!

Comment only
05 Nov 2013 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!

Comment only
23 Aug 2013 Georgi

### Georgi (view profile)

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

Comment only
05 Mar 2013 leon

### leon (view profile)

Richard Brown,
how to segment the images?

Comment only
04 Mar 2013 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.

Comment only
27 Feb 2013 leon

### leon (view profile)

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

Comment only
08 Oct 2012 Sepehr Farhand

### Sepehr Farhand (view profile)

28 Jun 2012 Richard Brown

### Richard Brown (view profile)

@Irfan: x and y values!

Comment only
20 Jun 2012 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?

Comment only
25 Jan 2012 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.

Comment only
20 Jan 2012 Valeria Rodríguez

### Valeria Rodríguez (view profile)

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.

19 Jan 2012 Pierre Roduit

### Pierre Roduit (view profile)

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

Comment only
23 May 2011 Felipe Pedreros

### Felipe Pedreros (view profile)

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

Comment only
14 Mar 2011 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.

Comment only
14 Mar 2011 Felipe Pedreros

### Felipe Pedreros (view profile)

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

16 Dec 2010 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

17 Sep 2010 bear tiger

### bear tiger (view profile)

good!
Is there any reference for this algorithum

Comment only
29 Apr 2010 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

Comment only
21 Apr 2010 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

Comment only
19 Apr 2010 Jenn Risk

### Jenn Risk (view profile)

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

Comment only
19 Oct 2009 Kristopher White

### Kristopher White (view profile)

Awesome work. Solid implementation, very useful.

25 Aug 2008 apiwat wisitty

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