Reputation: 21
I perform PCA on 3D images after semantic segmentation to define the orientation of objects and align them.
It works fine, produced eigenvectors form a valid rotation matrix that aligns objects correctly
But I would like to have rotation angles from the matrix.
When I use scipy.spatial.transform.Rotation.from_matrix
and convert to angles with r.as_euler("xyz")
it gives me the wrong angles.
Also, returning the matrix back from the same rotation object gives me a different matrix that rotates objects wrong
In short:
r = R.from_matrix(rotation_matrix) r.as_matrix()
gives me a matrix not equal to the initial matrix, and it is not some variant of correct rotation. It is the wrong rotation.
For example, the matrix.
[[ 0.86345719 -0.48700394 0.13141101]
[ 0.02362667 -0.221185 -0.97494563]
[-0.5038685 -0.84492861 0.1794775 ]]
changes into the altered matrix
[[ 0.82327846 -0.4171921 -0.3849199 ]
[-0.46561791 -0.88418854 -0.03755775]
[-0.32467296 0.21014609 -0.9221855 ]]
The initial rotation matrix looks ok, it is orthonormal, and the determinant is 1. And it is not singular, gimbal locks are not present.
By the way, the second matrix, given by scipy is "stable"; it remains the same after further retrieving.
The problem is permanent; every rotation matrix produced from PCA is mistreated by scipy What do I miss?
I tried other python tools, like transforms3d
, and converted the matrix to Euler angles and back to the matrix - the matrix becomes different (although it also differs from scipy case).
UPD: transforms3d
gives a "secondary" matrix that makes the same correct rotation, so I think this is just an optimized variant of the original matrix.
I checked the matrices for orthogonality and calculated determinants - they are fine. Besides, initial rotation matrices work as expected when I apply them to the data.
I expect that scipy would give me the correct extrinsic Euler angles or at least don't alter the rotation matrix, which I use as an indicator for wrongdoing.
Upvotes: 1
Views: 2259
Reputation: 21
Solved! My fault, the matrix contains reflection, so it is not a proper rotational matrix. https://github.com/scipy/scipy/issues/17324
Upvotes: 1