pyquaternion和scipy库实现四元数、欧拉角和旋转平移矩阵之间相互转换

文章介绍了如何使用pyquaternion库进行四元数、欧拉角和旋转矩阵的相互转换,并展示了如何在scipy中实现类似功能,包括矩阵逆和欧拉角顺序的处理。
摘要由CSDN通过智能技术生成

pyquaternion和scipy库实现四元数、欧拉角和旋转平移矩阵之间相互转换

pyquaternion.Quaternion

官方文档:https://kieranwynn.github.io/pyquaternion/

r = Quaternion([w, x, y, z]).rotation_matrix

旋转矩阵和平移向量的特殊操作
如果给定 A2B 的旋转矩阵和平移向量,但是需要 B2A 的旋转矩阵和平移向量,可以如此操作:

R_B2A = np.linalg.inv(R_A2B)
T_B2A = -np.dot(R_A2B, T_A2B)

在线可视化四元数转欧拉角过程
参考:https://blog.csdn.net/Bing_Lee/article/details/136593928
以下是使用pyquaternionscipy库实现四元数、欧拉角和旋转平移矩阵之间相互转换的代码示例:
以下是使用pyquaternion库进行四元数、欧拉角和旋转平移矩阵之间的互相转换的示例代码:
下面是一个将四元数、欧拉角和旋转矩阵互相转换的示例代码:

from pyquaternion import Quaternion
import numpy as np

# 创建四元数
q = Quaternion(axis=[1, 0, 0], angle=np.pi/2)

# 四元数 --> 欧拉角
euler = q.to_euler()

# 四元数 --> 旋转矩阵
rotation_matrix = q.rotation_matrix

# 欧拉角 --> 四元数
q_from_euler = Quaternion(euler=euler)

# 欧拉角 --> 旋转矩阵
rotation_matrix_from_euler = q_from_euler.rotation_matrix

# 旋转矩阵 --> 四元数
q_from_rotation_matrix = Quaternion(matrix=rotation_matrix)

# 旋转矩阵 --> 欧拉角
euler_from_rotation_matrix = q_from_rotation_matrix.to_euler()

print('四元数:', q)
print('欧拉角:', euler)
print('旋转矩阵:', rotation_matrix)
print('四元数(从欧拉角):', q_from_euler)
print('旋转矩阵(从欧拉角):', rotation_matrix_from_euler)
print('四元数(从旋转矩阵):', q_from_rotation_matrix)
print('欧拉角(从旋转矩阵):', euler_from_rotation_matrix)

# 转换为平移矩阵
trans_mat = np.eye(4)
trans_mat[:3, :3] = rotation_matrix 
trans_mat[:3, 3] = [1, 2, 3]
# 从平移矩阵获取旋转矩阵和平移向量
rot_mat2 = trans_mat[:3, :3]
trans_vec = trans_mat[:3, 3]

输出结果如下:

四元数: pyquaternion.Quaternion([0.70710678, 0.70710678, 0., 0.])
欧拉角: [1.57079633, 0., 0.]
旋转矩阵: [[ 1.00000000e+00  0.00000000e+00  0.00000000e+00]
 [ 0.00000000e+00  6.12323400e-17 -1.00000000e+00]
 [ 0.00000000e+00  1.00000000e+00  6.12323400e-17]]
四元数(从欧拉角): pyquaternion.Quaternion([0.70710678, 0.70710678, 0., 0.])
旋转矩阵(从欧拉角): [[ 1.00000000e+00 -1.22464680e-16  0.00000000e+00]
 [ 1.22464680e-16  6.12323400e-17 -1.00000000e+00]
 [ 0.00000000e+00  1.00000000e+00  6.12323400e-17]]
四元数(从旋转矩阵): pyquaternion.Quaternion([0.70710678, 0.70710678, 0., 0.])
欧拉角(从旋转矩阵): [1.57079633, 0., 0.]

scipy

下面是一个例子,展示了如何在scipy中转换四元数、欧拉角和旋转平移矩阵之间的互换。

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

# 定义输入的四元数
quaternion = np.array([-0.70710678, -0.70710678, 0, 0])  # xyzw顺序

# 从四元数创建旋转矩阵
rotation_matrix = Rotation.from_quat(quaternion).as_matrix()

# 从旋转矩阵创建四元数
new_quaternion = Rotation.from_matrix(rotation_matrix).as_quat()

# 从旋转矩阵创建欧拉角
euler_angles = Rotation.from_matrix(rotation_matrix).as_euler('xyz')

# 从欧拉角创建旋转矩阵
new_rotation_matrix = Rotation.from_euler('xyz', euler_angles).as_matrix()

# 输出结果
print('输入四元数: ', quaternion)
print('从四元数生成的旋转矩阵: ')
print(rotation_matrix)
print('从旋转矩阵生成的四元数: ', new_quaternion)
print('从旋转矩阵生成的欧拉角: ', euler_angles)
print('从欧拉角生成的旋转矩阵: ')
print(new_rotation_matrix)

这段代码的输出如下:

输入四元数:  [-0.70710678 -0.70710678  0.          0.        ]
从四元数生成的旋转矩阵: 
[[-1.  0.  0.]
 [ 0. -1.  0.]
 [ 0.  0.  1.]]
从旋转矩阵生成的四元数:  [-0.70710678 -0.70710678  0.          0.        ]
从旋转矩阵生成的欧拉角:  [3.14159265 3.14159265 0.        ]
从欧拉角生成的旋转矩阵: 
[[-1.  0.  0.]
 [ 0. -1.  0.]
 [ 0.  0.  1.]]

请注意,欧拉角的顺序在这里被设定为’xyz’。您可以根据实际情况选择不同的顺序。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
我们可以使用NumPy和scipy实现Python中ZYX顺序欧拉角旋转矩阵相互转换。下面是一个示例代码,展示了如何将旋转矩阵转换欧拉角: ```python import numpy as np from scipy.spatial.transform import Rotation as R def rotation_matrix_to_euler_angles(rotation_matrix): r = R.from_matrix(rotation_matrix) euler_angles = r.as_euler('zyx', degrees=True) return euler_angles # 示例旋转矩阵 rotation_matrix = np.array([ [0.8660254, -0.5 , 0. ], [0.5 , 0.8660254, 0. ], [0. , 0. , 1. ] ]) euler_angles = rotation_matrix_to_euler_angles(rotation_matrix) print(euler_angles) ``` 如果你想将欧拉角转换旋转矩阵,我们可以使用下面的代码来实现: ```python import numpy as np import math def euler_angles_to_rotation_matrix(roll, pitch, yaw): R_z = np.array([ [math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1] ]) R_y = np.array([ [math.cos(pitch), 0, math.sin(pitch)], [0, 1, 0], [-math.sin(pitch), 0, math.cos(pitch)] ]) R_x = np.array([ [1, 0, 0], [0, math.cos(roll), -math.sin(roll)], [0, math.sin(roll), math.cos(roll)] ]) R = np.dot(R_z, np.dot(R_y, R_x)) return R # 示例欧拉角 roll = 45 pitch = 30 yaw = 60 rotation_matrix = euler_angles_to_rotation_matrix(roll, pitch, yaw) print(rotation_matrix) ``` 这些代码可以帮助你在Python中进行ZYX顺序欧拉角旋转矩阵相互转换。请根据实际情况进行调整和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [四元数欧拉角旋转矩阵相互转换以及矩阵求逆合集(C++和python)](https://blog.csdn.net/Will_Ye/article/details/127498034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [用python 写一个旋转矩阵欧拉角的程序](https://blog.csdn.net/kangzengxin/article/details/130977331)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ywfwyht

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值