Code covered by the BSD License  

Highlights from
Ellipsoid fit

4.90909

4.9 | 11 ratings Rate this file 117 Downloads (last 30 days) File Size: 3.69 KB File ID: #24693
image thumbnail

Ellipsoid fit

by

 

10 Jul 2009 (Updated )

Fits an ellipsoid to data using linear least squares.

| Watch this File

File Information
Description

Fits an ellipsoid into a 3D set of points, allows some constraints, like orientation constraint and equal radii constraint. E.g., you can use it to fit a rugby ball, or a sphere. 'help ellipsoid_fit' says it all. Returns both the algebraic description of the ellipsoid (the nine coefficients of the quadratic form) and the geometric description (center, radii, principal axes).

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (18)
17 Sep 2014 Akeel  
08 Sep 2014 jerry  
30 Aug 2014 Corlan

Very useful piece of code!

evecs is eigenvectors matrix. Following the principal axis theorem, eigenvectors represent principal axes of ellipsoid. Regarding use with magnetometer calibration, those are needed to perform so called "soft iron" calibration, in order to properly shrink the ellipsoid into sphere. I would recommend taking a look at freescale's AN4246 & AN4248.

11 Mar 2014 Jean-Yves Tinevez  
15 Oct 2013 Tongtong

very useful and smart!

19 Jul 2013 Tucker McClure

This is well done. I especially appreciate the multiple input methods.

The flag=0 method can easily return imaginary results for noisy data. Note that the flag=0 method is also trying to find the primary axes of the ellipsoid. For those getting imaginary results when using flat=0 (the default), if your ellipsoid is expected to already be aligned with x, y, and z, then just use flag=1. This works quite well even when flag=0 falls apart.

I see some folks are using this for magnetometer data, which has a "hard iron" offset. To convert from raw data to points on a unit sphere:

[c, r] = ellipsoid_fit(raw, 1);

% For a single point, k:
unit_sphere(:, k) = (raw(:, k) - c)./r

% For multiple points, vectorized:
unit_sphere = diag(1./r) * (m - c * ones(1, size(m, 2)));

% Just for kicks using bsxfun:
unit_sphere = bsxfun(@times, 1./r, bsxfun(@minus, m, c))

24 Jun 2013 CrazyMaths_VM

Excellent code! however, for some ellipsoid data the radii values are imaginary what is the problem?. CAN ANYONE HELP PLEASE!!

07 May 2013 kostas

I used your code but the results are not correct with my data. The eigen values are very big. Should you normalise the Covariance matrix? I would also want to ask how did you derive from the 9 parameters of ellipsoid, the covariance matrix, center etc.. which book, notes did u use?

06 May 2013 Liang Sun

If the ellipsoid passes through the origin (0,0,0), how can the default equation "Ax^2 + By^2 + Cz^2 + 2Dxy + 2Exz + 2Fyz + 2Gx + 2Hy + 2Iz = 1" represent that? Thanks!

20 Nov 2012 Monica

Hi, did you find hot to determine those rotation angles?

20 Sep 2012 avcs asfasfa

Nice code, however
center--> Magnetometer offset
radii --> scaling factor
v---> the 9 parameters,
what is evecs?

what do i do next to get the calibrated values??? need help.....

27 Aug 2012 Daniel Lopes

Well done!

06 Aug 2012 Vincent

Hi, nice and efficient piece of code.
Nevertheless, I believe that the polynomial expression that you compute in a first step is not guaranteed to be one of an ellipsoid. I think that it could be any quadric (hyperboloid, paraboloid, etc), right?
It is probably no problem if you have much data (corresponding to a real ellipsoid) to fit your quadric on.
But in case of sparse data (for instance, data points only on a couple of planes), you could have bad surprises (and in particular negative eigenvalues).

26 Apr 2012 Brian

This is a fine piece of code

10 Feb 2012 Simone

Working very good

09 Sep 2011 Kevin Shaw

Works great.

21 Jun 2011 Hui Ma

I have a question: we can find the center, the radii of the ellipsoid and also the 3*3 direction matrix "evecs" using this program. Then how do you determine the three rotation angles using "evecs"?
Thanks for your help!

21 Jun 2011 Hui Ma

I am sorry, I meant how do you determine three rotation angles using "evecs" then?

Updates
17 Jul 2009

Just a better description.

Contact us