简体   繁体   English

具有坐标的数组的透视变换

[英]Perspective transform of an array with coordinates

I have an array with coordinates (2D) and want to calculate the new coordinates in a different quadrilateral. 我有一个坐标为(2D)的数组,想在不同的四边形中计算新坐标。 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. 在opencv(cv2)中是否有特定的功能,甚至没有公式。

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. 我搜索了很长时间,但似乎只能找到Matrix计算或函数来转换整个图像或数组。

If I understand correctly, opencv has what you need: 如果我理解正确,opencv将满足您的需求:

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). 您会注意到,在计算转换(上下颠倒)之前,我采取了使dest的方向与src匹配的自由。

Now that matrix can be used to convert any array of points (2D in our case): 现在,该矩阵可用于转换任何点数组(在我们的例子中为2D):

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 . 作为最后的建议,请注意输入点数组original的形状:它必须是3D数组,正如我在此处发现的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM