File Exchange

image thumbnail

Ellipsoid fit

version 1.3 (4.01 KB) by

Fits an ellipsoid / sphere / paraboloid / hyperboloid to data using linear least squares.

4.93103
29 Ratings

59 Downloads

Updated

View License

Fits an ellipsoid or other conic surface into a 3D set of points approximating such a surface, 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).

Comments and Ratings (38)

Well Gerrie

Can you give a pdf which you referenced ?

nay zhao

Lots of questions below about how to use the output to correctly map points onto a unit sphere. Here's the answer:

corrected_point = C * (raw_point - center)

where corrected_point, raw_point, center are 3x1 vectors (x,y,z) and C = evecs*diag(1./radii)*evecs' is a symmetric 3x3 matrix.

If you'd rather have the points mapped onto a sphere of radius k (rather than unit sphere), then use k*C in the above instead of just C.

MA AG

MA AG (view profile)

Eomzi Yang

thank you!

Works good, very useful. Thank you.

Ho King

excellent job

Zhang

Zhang (view profile)

very nice.
But I have the same question that is how to get three rotation angles,or how to calibrate the values? help for your help

zhang

zhang (view profile)

good work!

Amy Estes

Very nice work!
Is there any hack to constrain the conic type? I would like to fit my data on an ellipsoid, not on a paraboloid nor an hyperboloid.

Stefan

Stefan (view profile)

Very useful, thank you! Is there a paper available which describes the math behind this code?

I also think it would be convenient to have an 'yz' constraint included.

Chani

Chani (view profile)

kassem kalo

hello, how to apply this code to a csv (x,y,z) file data

Akeel

Akeel (view profile)

jerry

jerry (view profile)

Corlan

Corlan (view profile)

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.

Jean-Yves Tinevez

Tongtong

very useful and smart!

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

CrazyMaths_VM

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

kostas

kostas (view profile)

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?

Liang Sun

Liang Sun (view profile)

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!

Monica

Monica (view profile)

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

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

Daniel Lopes

Daniel Lopes (view profile)

Well done!

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

Brian

Brian (view profile)

This is a fine piece of code

Simone

Simone (view profile)

Working very good

Kevin Shaw

Works great.

Hui Ma

Hui Ma (view profile)

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!

Hui Ma

Hui Ma (view profile)

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

Updates

1.3

Indicated that the code fits surfaces, not volumes.

1.3

Updated the function to give correct radii and chi2 when fitting other conics beside ellipsoids.

1.2

Fixed instability arising for data points near zero.
Added equal radii constraints for an ellipsoid of an arbitrary orientation.

1.1

Just a better description.

MATLAB Release
MATLAB 8.6 (R2015b)

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

» Watch video