python旋转向量转为欧拉角的代码
时间: 2023-11-12 08:06:45 浏览: 55
以下是Python中将旋转向量转为欧拉角的代码:
```python
import numpy as np
from math import atan2, asin, pi
def rotation_matrix_to_euler_angles(R):
sy = np.sqrt(R[0,0]**2 + R[1,0]**2)
singular = sy < 1e-6
if not singular :
x = atan2(R[2,1] , R[2,2])
y = atan2(-R[2,0], sy)
z = atan2(R[1,0], R[0,0])
else :
x = atan2(-R[1,2], R[1,1])
y = atan2(-R[2,0], sy)
z = 0
return np.array([x, y, z])
# Example usage:
R = np.array([[0.0, 1.0, 0.0],
[-1.0, 0.0, 0.0],
[0.0, 0.0, 1.0]]) # 90 degree rotation about the z-axis
euler_angles = rotation_matrix_to_euler_angles(R)
print("Euler angles (radians):", euler_angles)
print("Euler angles (degrees):", np.degrees(euler_angles))
```
如果您有任何其他问题,我会尽力为您解答。