Hello,
I’m trying to rotate sections of my images according to certain orientations. However, I noticed some discrepancies between the rotations of the Euler3DTransform object and scipy.spatial.transform.Rotation when given the same set of Euler Angles.
The following code reproduces problem, with the output just below. Given a set of Euler angles in XYZ, the resulting rotation matrices between what sitk and scipy differ, especially with rotations around both X and Y. I used the metric (from the cited paper) to compare the rotation matrices and print them when they differ significantly. It’s notable in the first error (rotation in only X and Y), that the matrices only differ in position. Also worth mentioning that this error is not unique to using angles of 0.5 radians, I only chose it for demonstration purposes.
import numpy as np
from scipy.spatial.transform import Rotation as R
from SimpleITK import Euler3DTransform
euler_angles = [ # euler angles 'xyz' in radians
(0, 0, 0),
(0.5, 0, 0),
(0, 0.5, 0),
(0, 0, 0.5),
(0.5, 0.5, 0),
(0.5, 0, 0.5),
(0, 0.5, 0.5),
(0.5, 0.5, 0.5)
]
t = Euler3DTransform()
for ea in euler_angles:
t.SetRotation(*ea)
msitk = np.array(t.GetMatrix()).reshape(3,3)
msci = R.from_euler("xyz", ea).as_matrix()
# Provides a metric for comparing two rotation matrices, results in values
# in the range of[0, 2√2]
# Huynh, D. Q. (2009). Metrics for 3D Rotations: Comparison and Analysis.
# Journal of Mathematical Imaging and Vision, 35(2), 155–164. https://doi.org/10.1007/s10851-009-0161-2
error = np.linalg.norm(np.identity(3) - msitk @ msci.T)
print(f"{ea}: error: {error:.4f}")
if error > 1e-4:
print("sitk:")
print(msitk)
print()
print("scipy:")
print(msci)
Output:
(0, 0, 0): error: 0.0000
(0.5, 0, 0): error: 0.0000
(0, 0.5, 0): error: 0.0000
(0, 0, 0.5): error: 0.0000
(0.5, 0.5, 0): error: 0.3456
sitk:
[[ 0.87758256 0. 0.47942554]
[ 0.22984885 0.87758256 -0.42073549]
[-0.42073549 0.47942554 0.77015115]]
scipy:
[[ 0.87758256 0.22984885 0.42073549]
[ 0. 0.87758256 -0.47942554]
[-0.47942554 0.42073549 0.77015115]]
(0.5, 0, 0.5): error: 0.0000
(0, 0.5, 0.5): error: 0.0000
(0.5, 0.5, 0.5): error: 0.3456
sitk:
[[ 0.65995575 -0.42073549 0.62244683]
[ 0.62244683 0.77015115 -0.13938128]
[-0.42073549 0.47942554 0.77015115]]
scipy:
[[ 0.77015115 -0.21902415 0.59907898]
[ 0.42073549 0.88034656 -0.21902415]
[-0.47942554 0.42073549 0.77015115]]
I’d label something like this as a bug somewhere, but I’d be very thankful to anyone who has a different explanation for this phenomenon.
Edit:
The correctly resulting rotation matrices from the first four sets of angles should showcase that it shouldn’t have anything to do with an incorrect order of Euler angles.