Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntertiaEllipsoid #27

Closed
tischi opened this issue Nov 30, 2018 · 14 comments
Closed

IntertiaEllipsoid #27

tischi opened this issue Nov 30, 2018 · 14 comments

Comments

@tischi
Copy link

tischi commented Nov 30, 2018

@dlegland
hi David, I have a question related to this:
https://github.com/ijpb/MorphoLibJ/blob/master/src/main/java/inra/ijpb/measure/region3d/InertiaEllipsoid.java#L302
Which convention do you follow for those angles? In other words, how would I have to rotate an object to align it with respect to those angles. There seem to be different versions as to which angle you rotate around which axis in which order. Thank you very much for your help!

@dlegland
Copy link
Contributor

dlegland commented Nov 30, 2018

Hi,
Euler angles are a nightmare... I have added a short explanation in the latest version of the manual (with a figure!)

In short:

  • three angles correspond to rotations around X, Y, and Z axis
  • order is as follow: first rotation around X by psi, then around Y by theta, then around Z by phi.
  • angles around axis i are positive when a rotation by 90 degrees transform axis (i+1) into the axis (i+2). Then, a rotation by 90 degrees around Y will map the Z axis onto the X axis.

Does it helps?

@tischi
Copy link
Author

tischi commented Nov 30, 2018

"Euler angles are a nightmare..." I could not agree more! Incredible how much time I spend lately understanding all the different ways of rotating objects in 3D.

My issue is that I am using basically using your code for "Ellipsoid fitting":
https://github.com/tischi/fiji-plugin-morphometry/blob/master/src/main/java/de/embl/cba/morphometry/geometry/Ellipsoids.java#L22

Related to this: how do I properly cite MorphoLibJ in my code?

And then I use this function to convert it to a AffineTransform:
https://github.com/tischi/fiji-plugin-morphometry/blob/master/src/main/java/de/embl/cba/morphometry/geometry/Ellipsoids.java#L187

This works almost always nice, but in some cases the transformation looks like as if the axes were not found properly. The longest axis is aligned but the shortest not.

Now I do not know whether something goes wrong during the moments computation or whether there is a problem with the way that I compute the transformation....

@tischi
Copy link
Author

tischi commented Nov 30, 2018

I added a test: https://github.com/tischi/fiji-plugin-morphometry/blob/master/src/test/java/TestEllipsoidFitting.java#L23

If you wish you could just clone my repo and try yourself.
I compare are two ellipsoids with 90 degrees rotated longest axes.

Running the MLJ code I just get different rotations of 180 degrees, which should do nothing in terms of aligning those axes, should it? I guess I am doing something wrong?

For the 3D ImageSuite, the last vector appears to point into the direction of the long axis.

MLJ angles (Phi, Theta, Psi):
-180.0
0.0
-0.0

3D suite vectors
(0, 0, 1)
(0, 1, 0)
(1, 0, 0)

// Second ellipsoid

MLJ angles (Phi, Theta, Psi):
0.0
0.0
-180.0

3D suite vectors
(0, 0, 1)
(1, 0, 0)
(0, 1, 0)

@dlegland
Copy link
Contributor

dlegland commented Dec 3, 2018

Hi,

well, ellipsoids are symmetric, so having a rotation by 180 around one of the principal axes of the ellipsoid does not fundamentally changes the result! Anyway, I think the differences come from the way the inertia matrix is converted into a triplet of angles. I will check more carefully, but this should be rather similar.
Note: I was envisioning some constraints, such as forcing the first vector to have positive x-coords. So maybe there will be changes in a future version, but results will always be consistent up to 180 rotations.

I have committed a small update, that isolates the computation of the inertia coefficients -> this should make it easier to test/check and/or adapt.

Also, to cite MorphoLibJ, the best is to use the paper published in BioInformatics in 2016: https://academic.oup.com/bioinformatics/article/32/22/3532/2525592

@tischi
Copy link
Author

tischi commented Dec 3, 2018

ellipsoids are symmetric, so having a rotation by 180 around one of the principal axes of the ellipsoid does not fundamentally changes the result

...that was my point: The angles that MLJ currently gives do not contain the information that the longest ellipsoid axis is relatively rotated by 90 degrees in the two examples that I tested (for both cases one just gets rotations around 180 degrees instead of one of them yielding a 90 degrees rotation).

First ellipsoid:

image

MLJ angles (Phi, Theta, Psi):
-180.0
0.0
-0.0

Second ellipsoid:

image

MLJ angles (Phi, Theta, Psi):
0.0
0.0
-180.0

@dlegland
Copy link
Contributor

dlegland commented Dec 3, 2018

Hi,
all right, I got it, sorry for comprehension delay!

I has a better look at the code, and found the problem. In fact it arise only when object is very well aligned with y axis...
I commited an updated version. I tested for various orientations of cuboids, it corresponds to what I expect. Can you check on your examples?

@tischi
Copy link
Author

tischi commented Dec 4, 2018

Thanks a lot!
There is conference for the rest of the week so I will only be able to check next week...

@tischi
Copy link
Author

tischi commented Dec 10, 2018

I tried it and it now handles the cases above properly!
Thanks!

However I am still struggling with one data set. I only manage to get it aligned along the axes of the coordinate system the second time I run it through the alignment procedure, which is weird:

Dataset: /Users/tischer/Documents/fiji-plugin-morphometry/target/test-classes/dapi_mask_2.zip

MLJ ellipsoid angles (Phi, Theta, Psi):
Phi: -115.77879789567466
Theta: 69.68447176166082
Psi: 26.0772742639629

3D suite vectors
(0.03, -0.95, 0.312)
(0.988, -0.019, -0.153)
(0.151, 0.313, 0.938)

Dataset (round 2): /Users/tischer/Documents/fiji-plugin-morphometry/target/test-classes/dapi_mask_2.zip

MLJ ellipsoid angles (Phi, Theta, Psi):
Phi: -0.08537533578628245
Theta: -0.14821951095224184
Psi: 127.80187706443633

3D suite vectors
(0, -0.79, -0.613)
(0.003, 0.613, -0.79)
(-1, 0.001, -0.003)

Dataset (round 3): /Users/tischer/Documents/fiji-plugin-morphometry/target/test-classes/dapi_mask_2.zip

MLJ ellipsoid angles (Phi, Theta, Psi):
Phi: -1.0086485768856053
Theta: -0.09333462259059956
Psi: 179.90747186886216

3D suite vectors
(0.002, -0.002, -1)
(-0.018, -1, 0.002)
(-1, 0.018, -0.002)

Only at "round 3" the axes are along the coordinate system => the first rotation does not suffice.

The data set is here: https://github.com/tischi/fiji-plugin-morphometry/blob/master/src/test/resources/dapi_mask_2.zip
The code is here: https://github.com/tischi/fiji-plugin-morphometry/blob/master/src/test/java/TestEllipsoidFitting.java#L32

On another note: what about also outputting the the 3 major axes as vectors that way that the 3d-ImageSuite is doing it? I think this would be great, because:

  • It is less error-prone than the EulerAngles, where one never is sure whether one gets the order and rotation direction right.
  • One could directly cross-check your code against the 3DImageSuite code. If both agree we are all sure that everything is correct!

@dlegland
Copy link
Contributor

Hi,
ok, good to see it quite works, I will investigate the remaining case. I suspect some kind of "gimbal lock" effect (the first ellispoid axis is vertical, this may cause some inconsistencies in resulting angles).

So, I agree with you, returning the major axes as vectors is a good idea! I try to include it soon.

@dlegland
Copy link
Contributor

Hi!
I pushed a commit that allows to display the three vectors obtained from the Inertia Matrix computation. It is shown with the InertiaEllipsoid plugin, but can be accessed programatically as well: https://github.com/ijpb/MorphoLibJ/blob/master/src/main/java/inra/ijpb/measure/region3d/InertiaEllipsoid.java#L349
I used the convention to keep the order of the SVD: by decreasing order of eigen values. I have the impression that the 3D ImageSuite uses the reverse order?

I have also started an implementation of an utility "EulerAnglesXYZ" class, to isolate parts of the computation. It can be found in the "geometry" branch.

By the way, what is the goal of your script? Do you want to perform principal axis transform? in that case, I agree: the best isto keep the vectors, build the transform matrix, and apply the transform (or its inverse) to the image.

@dlegland
Copy link
Contributor

Here is a link on a pdf I used for writing the conversion code from rotation matrix to Euler angles:
Computing Euler angles from a rotation matrix, by Gregory Slabaugh.

@dlegland
Copy link
Contributor

Just to finalize the thread, I have included modification on computation of equivalent ellipsoid into latest version of MorphoLibJ (1.4.1). The user manual was also updated to include all the necessary explanations.
Note that MorphoLibJ uses an equivalent ellipsoid that follows statistical conventions for extracting eigen vectors. This results in a different ellipsoid than the inertia ellipsoid.

Best,
David

@tischi
Copy link
Author

tischi commented Jul 30, 2019

Excellent! Thank you!
I have to admit that I still find the difference between the "inertia ellipsoid" and the "statistical ellipsoid" confusing. Do you have a reference for a comparison of the two?

@dlegland
Copy link
Contributor

dlegland commented Aug 1, 2019

Hi,
not really... The axis definitions I was using was a natural extension of 2D to 3D. It can be generalized to more dimensions as well. Most (all?) multivariate statistical analysis techniques order the principal axes from largest eigen value to lowest, so I followed the convention. It is also used by the "regionprops3" function from Matlab (with a different axis scaling).

For the "inertia ellipsoid", it seems definitions are more related to mechanics. Main source is Wikipedia. You can also see https://uk.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/48913/versions/4/screenshot.jpg , or issue #30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants