在 Python 中连接矩阵和向量

问题描述 投票:0回答:1

我是 MATLAB 用户,并尝试将旋转/平移代码转换为 Python:

from functools import reduce
import numpy as np
import matplotlib as plt
from math import pi as PI 
from math import sin as sin 
from math import cos as cos 

# plt.close('all')

#   coordinate in old system
x0 = np.array([ [10], 
                [5], 
                [0],])

x = x0


# translation
# origin of old coordinate system in relative to the new coordinate system 
T = np.array([  [10],
                [0],
                [0]])


## rotation
# rotate alpha, beta, and gamme about x-axis, y-axis, and z- axis,
# respectively, in relative to the new coordinate system

alpha = 0
beta  = 0
gamma = PI/2

# rotation matrices
Rx = np.array([ [1,           0,              0],
                [0,   cos(alpha),   -sin(alpha)], 
                [0,   sin(alpha),    cos(alpha)] ])

Ry = np.array([ [cos(beta),     0,       sin(beta)], 
                [0,             1,               0],
                [-sin(beta),    0,       cos(beta)]])

Rz = np.array([ [cos(gamma),   -sin(gamma),   0],
                [sin(gamma),    cos(gamma),   0], 
                [0,                      0,   1] ])

# R = Rz*Ry*Rx
R = reduce(np.dot, [Rz, Ry, Rx])

RT = np.array([ [R,       T],
                [0, 0, 0, 1]])

最后一行尝试通过堆叠 3X3 旋转矩阵

RT
和 3X1 平移向量
R
来创建齐次 4X4 矩阵
T
。矩阵底部的[0,0,0,1]只是将变换矩阵
RT
变成齐次格式。

代码将

R
T
[0,0,0,1]
堆叠在一起时存在问题。我想了解什么是正确的方法?谢谢!!

python matrix
1个回答
0
投票

尝试

intermediate = np.concatenate((R, T.T), axis=1)
RT = np.concatenate((intermediate, np.array([0, 0, 0, 1])), axis=0)
© www.soinside.com 2019 - 2024. All rights reserved.