question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: Rotation matrix is changed with scipy.spatial.transform.Rotation

See original GitHub issue

Describe your issue.

Greetings, I think I encountered some bug in processing a 3D rotation matrix. I tried to retrieve Euler angles, but they made the wrong rotation. Even when I retrieve the matrix back, it is altered incorrectly. I think this failure has something to do with the internal storage of rotation objects as quaternions. But I don’t have expertise enough to debug through the source code, and I haven’t found description in the documentation about conversion limitations between rotation formats, except gimbal lock. There is a code example of the initial rotation matrix and the wrong output during conversions. Even the first retrieved matrix rotmatrix11 (without conversions) is wrong. I dubbed my question on StackOverflow https://stackoverflow.com/questions/74265765/rotation-matrix-is-changed-with-scipy-spatial-transform-rotation The rotation matrix seems correct in terms of orthonormality and the determinant.

Reproducing Code Example

from scipy.spatial.transform import Rotation as R
import numpy as np

rotmatrix1 = np.array([[ 0.86345719, -0.48700394,  0.13141101],
       [ 0.02362667, -0.221185  , -0.97494563],
       [-0.5038685 , -0.84492861,  0.1794775 ]])

print(f"rotmatrix1:\n  {rotmatrix1}")

r1 = R.from_matrix(rotmatrix1)
rotmatrix11 = r1.as_matrix()
angles1 = r1.as_euler("xyz")
print(f"angles1:  {angles1}")
print(f"rotmatrix11:\n  {rotmatrix11}")
print("-------------------------------")

r2 = R.from_euler("xyz", angles1)
rotmatrix2 = r2.as_matrix()
print(f"rotmatrix2:\n  {rotmatrix2}")

r3 = R.from_matrix(rotmatrix2)
angles2 = r3.as_euler("xyz")
print(f"angles2:  {angles2}")
r4 = R.from_euler("xyz", angles2)
rotmatrix3 = r4.as_matrix()
print(f"rotmatrix3:\n  {rotmatrix3}")

Error message

Output (all matrices are expected to be the same or at least do the same rotation job, but they differ):
rotmatrix1:
  [[ 0.86345719 -0.48700394  0.13141101]
 [ 0.02362667 -0.221185   -0.97494563]
 [-0.5038685  -0.84492861  0.1794775 ]]
angles1:  [ 2.91754028  0.33066594 -0.51471512]
rotmatrix11:
  [[ 0.82327846 -0.4171921  -0.3849199 ]
 [-0.46561791 -0.88418854 -0.03755775]
 [-0.32467296  0.21014609 -0.9221855 ]]
-------------------------------
rotmatrix2:
  [[ 0.82327846 -0.4171921  -0.3849199 ]
 [-0.46561791 -0.88418854 -0.03755775]
 [-0.32467296  0.21014609 -0.9221855 ]]
angles2:  [ 2.91754028  0.33066594 -0.51471512]
rotmatrix3:
  [[ 0.82327846 -0.4171921  -0.3849199 ]
 [-0.46561791 -0.88418854 -0.03755775]
 [-0.32467296  0.21014609 -0.9221855 ]]

SciPy/NumPy/Python version information

1.7.3 1.21.6 sys.version_info(major=3, minor=7, micro=15, releaselevel=‘final’, serial=0)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
rkerncommented, Nov 9, 2022

rotmatrix1 is not a rotation matrix. It incorporates a reflection.

|2> np.linalg.det(rotmatrix1)                                                                                                                                                                                        
-1.0000000047994289
0reactions
akvilonBrowncommented, Nov 10, 2022

Thank you for your clarification and for your time. My knowledge gap didn’t allow me to reveal it in time. I was misled by some custom code snippets on the web which can handle negative determinants and stated that it was OK for rotation. Now I see that in scipy documentation that determinant +1 is a mandatory condition. Indeed reflection is quite likely to occur when I use PCA results as a basis for the rotational matrix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rotation.from_matrix() function returning error using Scipy 1.7 ...
Describe your issue. The Rotation.from_matrix() function is returning error messages using Scipy 1.7.2 but works fine using Scipy 1.5.2.
Read more >
Rotation matrix is changed with scipy.spatial.transform.Rotation
I perform PCA on 3D images after semantic segmentation to define the orientation of objects and align them. It works fine, produced eigenvectors ......
Read more >
scipy.spatial.transform.Rotation — SciPy v1.9.3 Manual
This class provides an interface to initialize from and represent rotations with: Quaternions. Rotation Matrices. Rotation Vectors. Modified Rodrigues ...
Read more >
Different Rotation Matrices between Euler3DTransform and ...
Hello, I'm trying to rotate sections of my images according to certain orientations. However, I noticed some discrepancies between the ...
Read more >
Finding optimal rotation and translation between ... - Nghia Ho
We want to find the best rotation and translation that will align the points in dataset A to dataset B. Here, 'optimal' or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found