如何用Python基于已有旋转/平移矩阵将3D物体绕X轴旋转5度?
解决方法
要实现物体绕自身X轴旋转5度的位姿精修,核心是将局部旋转操作整合到原有的相机-物体位姿变换矩阵中,用Python结合NumPy就能完成,步骤如下:
1. 坐标变换逻辑说明
现有rotation_matrix是物体坐标系到相机坐标系的旋转矩阵,translation_matrix是相机坐标系下物体原点的平移向量。物体绕自身X轴旋转时,仅需调整旋转矩阵,平移向量保持不变(仅旋转不改变物体整体位置)。
绕自身X轴逆时针旋转θ的旋转矩阵(右手坐标系)公式为:
$$
R_x(\theta) = \begin{bmatrix}
1 & 0 & 0 \
0 & \cos\theta & -\sin\theta \
0 & \sin\theta & \cos\theta
\end{bmatrix}
$$
由于是在物体局部坐标系执行旋转,需将该旋转矩阵的转置(正交矩阵的逆等于转置)与原有旋转矩阵相乘,得到新的旋转矩阵。
2. Python代码实现
import numpy as np # 原有位姿矩阵 rotation_matrix = np.array([ [0.99781346, -0.06608013, -0.00129879], [0.00725005, 0.12896627, -0.99162245], [0.06569404, 0.98944485, 0.12916337] ]) translation_matrix = np.array([ [-13.7971115], [-7.9274826], [423.7128] ]) # 将5度转换为弧度 theta = np.deg2rad(5) # 生成绕自身X轴旋转5度的旋转矩阵 R_x = np.array([ [1, 0, 0], [0, np.cos(theta), -np.sin(theta)], [0, np.sin(theta), np.cos(theta)] ]) # 计算新的旋转矩阵:原有旋转矩阵 乘以 局部旋转矩阵的转置 new_rotation_matrix = rotation_matrix @ R_x.T # 平移矩阵保持不变 new_translation_matrix = translation_matrix # 输出结果 print("新的旋转矩阵:") print(new_rotation_matrix) print("\n新的平移矩阵:") print(new_translation_matrix)
3. 补充说明
- 若需顺时针旋转5度,直接将
theta设为np.deg2rad(-5)即可,无需修改矩阵结构。 - 矩阵运算使用NumPy的
@运算符,确保运算效率和正确性。
内容的提问来源于stack exchange,提问作者smoothumut
相关产品推荐
相关产品推荐

