Python 组合两个仿射、平移和旋转图像

Python 组合两个仿射、平移和旋转图像,python,numpy,opencv,Python,Numpy,Opencv,我目前使用以下代码序列首先移动图像,然后旋转同一图像: M = np.float32([[1,0,20],[0,1,10]]) shifted = cv2.warpAffine(img,M,(y_size,x_size),flags=cv2.INTER_LANCZOS4) center_of_rot = (500, 500) M = cv2.getRotationMatrix2D(center_of_rot, 1.23, 1.0) rotated = cv2.warpAffine(shifted

我目前使用以下代码序列首先移动图像,然后旋转同一图像:

M = np.float32([[1,0,20],[0,1,10]])
shifted = cv2.warpAffine(img,M,(y_size,x_size),flags=cv2.INTER_LANCZOS4)
center_of_rot = (500, 500)
M = cv2.getRotationMatrix2D(center_of_rot, 1.23, 1.0)
rotated = cv2.warpAffine(shifted, M, (y_size, x_size),flags=cv2.INTER_LANCZOS4)
我认为有可能以某种方式将两个矩阵相乘,只做一个运算而不是两个扭曲仿射运算,我正在寻找一些指导,因为我的数学真的很烂


谢谢

必须将矩阵相乘。将第三行[0,0,1]添加到2x3矩阵。这称为齐次坐标

M = np.float32([[1,0,20],[0,1,10],[0,0,1]]) // shift matrix
center_of_rot = (500, 500)
Rot = cv2.getRotationMatrix2D(center_of_rot, 1.23, 1.0)
Rot = np.vstack([Rot, [0,0,1]])
TransfomMatrix = np.matmul(M, Rot)
rotated = cv2.warpPerspective(img, TransformMatrix, (y_size, x_size),flags=cv2.INTER_LANCZOS4) //  note - warpPerspective is used here, because the matrix is now 3x3 not 3x2