Find the position in the original image from the coordinates after affine transformation (Python + OpenCV)

Find the position in the original image from the coordinates after affine transformation

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

Way of thinking

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.

code

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

Find the position in the original image from the coordinates after affine transformation (Python + OpenCV)
Find the part that is 575 from Wikipedia in Python
Python OpenCV tried to display the image in text.
Find the difference in Python
Pass OpenCV data from the original C ++ library to Python
I tried to find the affine matrix in image alignment (feature point matching) using affine transformation.
[Python] Using OpenCV with Python (Image transformation)
Find image similarity with Python + OpenCV
Image acquisition from camera with Python + OpenCV
Try to measure the position of the object on the desk (real coordinate system) from the camera image with Python + OpenCV
[Python] Download original images from Google Image Search
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
What is wheezy in the Docker Python image?
Calculation result after the decimal point in Python
[Python] Find coordinates from two angles and distance
Find the divisor of the value entered in python
I tried "binarizing" the image with Python + OpenCV
Find the solution of the nth-order equation in python
[Python] Find the transposed matrix in a comprehension
python xlwings: Find the cell in the last row
[Python + OpenCV] Whiten the transparent part of the image
Display n digits after the decimal point in python
[Python] Get the numbers in the graph image with OCR
[Python] Specify the range from the image by dragging the mouse
Convert the image in .zip to PDF with Python
Write letters in the card illustration with OpenCV python
Find the Hermitian matrix and its eigenvalues in Python
Rotate and scale the image before cropping [python] [OpenCV]
Find the maximum Python
Image format in Python
List find in Python
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
Find out the apparent width of a string in python
Python --Find out number of groups in the regex expression
I tried face recognition from the video (OpenCV: python version)
Get the value while specifying the default value from dict in Python
Grayscale image is displayed as a color image in OpenCV / Python
After enabling the python virtual environment in the batch file, run the python file
Find the average / standard deviation of the brightness values in the image
What beginners learned from the basics of variables in python
Find the eigenvalues of a real symmetric matrix in Python