图像旋转:如何取非方阵的逆矩阵?

时间:2021-05-08 05:48:42

标签: python opencv

我正在剪下一张动物的图像,对其进行对齐、修改,然后我想将其粘贴回原始图像中。所以工作流程是这样的:

原图:
Original image

检测到动物:
Animal detected

动物对齐:
Animal aligned

动物提取:
Animal extracted

改良动物:
Modified Animal

重食动物:
Repasted Animal

我有检测和对齐动物的代码:

# get rotation matrix
M = cv2.getRotationMatrix2D(eyesCenter, angle, scale)
# update the translation component of the matrix
tX = w * 0.5
tY = h * LeftEye[1]
M[0, 2] += (tX - eyesCenter[0])
M[1, 2] += (tY - eyesCenter[1])
# apply the affine transformation
output = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC)

我尝试使用

Mp = np.linalg.inv(M)

但我收到错误:

Last 2 dimensions of the array must be square

如何将猫头鹰放回原始图像?

1 个答案:

答案 0 :(得分:1)

添加第三行 [0, 0, 1]。这会使乘积 y = A x 成为一个二维齐次向量(即一个具有三个分量的向量),其前两个分量与您没有添加行一样。

相关问题