将两个warpAffine图像进行平移和旋转合并

3

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

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)

我认为可以通过某种方式将两个矩阵相乘,只进行一次操作而不是两个warpAffine操作。我需要一些指导,因为我非常不擅长数学。

谢谢!

1个回答

3

您需要对矩阵进行乘法运算。将第三行 [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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接