I want to check if the image cut out after affine transformation (cv2.warpAffine) fits in the original image. The coordinates of the four corners of the cut out image should be in the original image
Looking at the matrix passed to warpAffine, it is a (2 × 3) matrix. If you add the row of [0, 0, 1] to this and transpose it ~~, you can get the rotation matrix used first. Assuming that the coordinates after the affine transformation are (x, y), multiply ~~ [x, y, 1] by the inverse matrix of the rotation matrix ~~ ]]](3 × 1) I feel that the original coordinates can be obtained. I think there is a method that supports OpenCV, but ~~ it's a hassle to find ~~ I don't know which one to use or how to use it.
inverseWarpAffine.py
import cv2
import numpy as np
def getOriginalPosition(p, M):
origin = np.array([[p.x], [p.y], [1]])
rotateM = np.array([M[0], M[1], [0, 0, 1]])
invM = np.linalg.inv(rotateM.T)
return np.dot(invM, origin)[:2, 0]
Recommended Posts