如何使用 x 和 y 坐标对点进行透视变换

dan*_*chy 5 python opencv

所以我写了这个小程序,它允许我在两个图像上选择 4 个点。

使用这些点我得到一个变换矩阵。之后,我在其中一张图像上选择一个点,并希望可视化该点在其他图像上的位置。

假设我的观点是这样标记的 -> (x,y)- 所以它是一个元组。我应该如何在图像上格式化这个“位置”,以便可以对其进行转换。

我查看了perspectiveTransform() 方法的文档,并认为我应该将其存储为以下形状:

numpy.array([
        [self.points[self.length-1][0]],
        [self.points[self.length-1][1]]
        ], dtype="float32")
Run Code Online (Sandbox Code Playgroud)

只需单击一下,我就会得到这种格式:

Point= [[ 2300.]
        [  634.]]
Run Code Online (Sandbox Code Playgroud)

这种格式似乎不起作用,我使用这个转换矩阵:

M = [[ -1.71913123e+00  -4.76850572e+00   5.27968944e+03]
     [  2.07693562e-01  -1.09738424e+01   6.35222770e+03]
     [  1.02865125e-04  -4.80067600e-03   1.00000000e+00]]
Run Code Online (Sandbox Code Playgroud)

在这种方法中(并得到以下错误):

cv2.perspectiveTransform(src, M)
OpenCV Error: Assertion failed (scn + 1 == m.cols) in cv::perspectiveTransform, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\core\src\matmul.cpp
Run Code Online (Sandbox Code Playgroud)

欢迎任何建议或提示。

dan*_*chy 7

我想出了答案。

在这个链接上找到了

关键是要这样说:

 pts = numpy.array([[x,y]], dtype = "float32")
Run Code Online (Sandbox Code Playgroud)

然后numpy.array在现有变量上调用另一个pts

 pts = numpy.array([pts])
Run Code Online (Sandbox Code Playgroud)

此后的程序相同。