Home > Back-end >  Why different results for Euler to Rotation Matrix in Matlab and Python
Why different results for Euler to Rotation Matrix in Matlab and Python

Time:08-04

When I use Matlab and Python to transform from Euler to Rotation Matrix, I get different results, and I can't figure out why.

Python code

from scipy.spatial.transform import Rotation as R
cam_angle = 45
R.from_euler('xyz', [-90-cam_angle, 0, -90], degrees=True).as_matrix()

gives:

array([[ 0.        , -0.70710678,  0.70710678],
       [-1.        ,  0.        ,  0.        ],
       [ 0.        , -0.70710678, -0.70710678]])

While Matlab code

cam_angle = 45
eul2rotm(deg2rad([-90-cam_angle, 0, -90]),'xyz')

gives:

0.0000    1.0000         0
0.7071   -0.0000    0.7071
0.7071   -0.0000   -0.7071

Anyone have a idea?

CodePudding user response:

In your Python code, use an uppercase 'XYZ' for the seq argument for from_euler to use intrinsic rotations, which is what MATLAB uses.

from scipy.spatial.transform import Rotation as R
cam_angle = 45
R.from_euler('XYZ', [-90-cam_angle, 0, -90], degrees=True).as_matrix()

Result:

array([[ 0.        ,  1.        ,  0.        ],
       [ 0.70710678,  0.        ,  0.70710678],
       [ 0.70710678,  0.        , -0.70710678]])

From the scipy docs:

Parameters: seq string
Specifies sequence of axes for rotations. Up to 3 characters belonging to the set {‘X’, ‘Y’, ‘Z’} for intrinsic rotations, or {‘x’, ‘y’, ‘z’} for extrinsic rotations. Extrinsic and intrinsic rotations cannot be mixed in one function call.

  • Related