Code Monkey home page Code Monkey logo

Comments (14)

dlegland avatar dlegland commented on June 23, 2024 1

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?

from morpholibj.

dlegland avatar dlegland commented on June 23, 2024 1

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.

from morpholibj.

dlegland avatar dlegland commented on June 23, 2024 1

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

from morpholibj.

dlegland avatar dlegland commented on June 23, 2024

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?

from morpholibj.

tischi avatar tischi commented on June 23, 2024

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

from morpholibj.

tischi avatar tischi commented on June 23, 2024

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)

from morpholibj.

dlegland avatar dlegland commented on June 23, 2024

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

from morpholibj.

tischi avatar tischi commented on June 23, 2024

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

from morpholibj.

tischi avatar tischi commented on June 23, 2024

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

from morpholibj.

tischi avatar tischi commented on June 23, 2024

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!

from morpholibj.

dlegland avatar dlegland commented on June 23, 2024

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.

from morpholibj.

dlegland avatar dlegland commented on June 23, 2024

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.

from morpholibj.

tischi avatar tischi commented on June 23, 2024

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?

from morpholibj.

dlegland avatar dlegland commented on June 23, 2024

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

from morpholibj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.