结合两个warpAffine,平移和旋转图像

时间:2019-01-30 11:01:59

标签: 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, (y_size, x_size),flags=cv2.INTER_LANCZOS4)

我认为可以以某种方式将两个矩阵相乘,而只执行一个操作而不是两个warpAffine,我正在寻找一些指导,因为我确实很喜欢数学。

谢谢!

1 个答案:

答案 0 :(得分:0)

您必须将矩阵相乘。将第三行[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
相关问题