Reputation: 31
I have an array with coordinates (2D) and want to calculate the new coordinates in a different quadrilateral. I do only know de corners of both quadrilaterals.
So for example, the old quadrilateral corner coordinates are
topleft(25,25), Topright(200,20), Botomleft(35,210), Botomright(215,200)
the new quadrilateral:
Topleft(-50,50), Topright(50,50), Botomleft(-50,-50), Botomright(-50,-50)
Is there a specific function in opencv (cv2) to do so, or even a formula.
I am searching for quite a while and I can only seem to find Matrix calculations or function to transform a whole image or array.
Upvotes: 2
Views: 4801
Reputation: 12627
If I understand correctly, opencv has what you need:
First, compute the transform matrix:
import cv2
import numpy as np
src = np.array(((25, 25), (200, 20), (35, 210), (215, 200)), dtype=np.float32)
dest = np.array(((-50, -50), (50, -50), (-50, 50), (50, 50)), dtype=np.float32)
mtx = cv2.getPerspectiveTransform(src, dest)
You will notice I took the liberty to make dest
's orientation match src
's one before computing the transform (inverted top and bottom).
Now that matrix can be used to convert any array of points (2D in our case):
original = np.array([((42, 42), (30, 100), (150, 75))], dtype=np.float32)
converted = cv2.perspectiveTransform(original, mtx)
Result:
>>> converted
>>> array([[[-41.06365204 -40.27705765]
[-49.48046112 -8.70405197]
[ 18.60642052 -19.92881393]]])
As a final advice, please note the shape of the input points array original
: it has to be a 3D array, as I found out here.
Upvotes: 2