I am currently filming with my camera a tag that is a square. When it is viewed from the side, and then looks like a parallelogram and no more a square, I want to find the transformation to turn it like it is viewed from the front keeping its scale.
I tried to do a homography and affine transformation (see code below), but for each, it made my tag square (good) and fit the size of the destination square I used (not good).
My goal is to calculate the size of the tag on the screen, but I need the size as viewed from the front, that is why I do not want to rescale it.
I cannot figure out what is the correct mathematics transform to use, and then the way to do it with OpenCV.
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Example of points extracted from image
pts_src = np.array([[395.7906189, 335.22460938],
[506.88623047, 376.4800415 ],
[511.29971313, 176.26641846],
[403.23400879, 174.1025238 ]])
pts_dst = np.array([[0, 0],[100, 0],[100, 100],[0, 100]])
# Calculate Homography
h, status = cv2.findHomography(pts_src, pts_dst)
# Calculate Affine
retval, _ = cv2.estimateAffine2D(pts_src, pts_dst)
transformed_points_h = cv2.perspectiveTransform(pts_src.reshape(-1, 1, 2), h)
transformed_points_affine = cv2.transform(pts_src.reshape(1, -1, 2), retval)
print(transformed_points_h)
print(transformed_points_affine)
I got as a result this, which is equivalent to the pts_dst:
[[[ 4.11799847e-09 -2.17596654e-09]]
[[ 1.00000000e+02 2.07253269e-09]]
[[ 1.00000000e+02 1.00000000e+02]]
[[ 8.72359544e-10 1.00000000e+02]]]
[[[ 3.31556294e-09 -2.25429631e-09]
[ 1.00000000e+02 2.66672373e-09]
[ 9.57205638e+01 1.23166307e+02]
[ 6.77971457e-10 1.00000000e+02]]]