Python 尝试将图像上的点投影到二维平面时打开CV通道

Python 尝试将图像上的点投影到二维平面时打开CV通道,python,opencv,Python,Opencv,我目前正试图在下图中投射黑点: 到下图上的二维平面: 对于“pts_dst”,我使用以下坐标: 从同一位置拍摄的场地照片的点 我的代码如下: import cv2 import numpy as np if __name__ == '__main__': # Read source image. im_src = cv2.imread('field2.png') # Four corners of the book in source image pts_

我目前正试图在下图中投射黑点:

到下图上的二维平面:

对于“pts_dst”,我使用以下坐标:

从同一位置拍摄的场地照片的点

我的代码如下:

import cv2
import numpy as np

if __name__ == '__main__':
    # Read source image.
    im_src = cv2.imread('field2.png')
    # Four corners of the book in source image
    pts_src = np.array([[436, 181], [319, 181], [539, 314], [180, 312]])

    # Read destination image.
    im_dst = cv2.imread('field1.png')
    # Four corners of the book in destination image.
    pts_dst = np.array([[297, 233], [297, 139], [55, 234], [54, 139]])

    # Calculate Homography
    h, status = cv2.findHomography(pts_src, pts_dst)

    # provide a point you wish to map from image 1 to image 2
    a = np.array([[493, 274]], dtype='float32')
    a = np.array([a])

    pointsOut = cv2.perspectiveTransform(a, h)

    # Display image
    cv2.imshow("Warped Source Image", pointsOut)

    cv2.waitKey(0)
但我遇到了以下错误:

cv2.imshow(“扭曲的源图像”,pointsOut)

cv2.2错误:OpenCV(3.4.2) /io/opencv/modules/imgcodecs/src/utils.cpp:622:错误:(-15:错误数字) 通道数)源图像必须有1个、3个或4个通道可用 “CVI图像”

出于某种原因,这个问题对我来说很难解决——可能是因为我是OpenCV新手——但我似乎无法解决它

它似乎是指

pointsOut = cv2.perspectiveTransform(a, h)
我发现了一个类似的问题,但它并没有解决我的问题,因为我已经将它设置为浮点

如果我不能使用
perspectiveTransform()
,有人对我做错了什么有什么建议,或者有更好的方法来解决这个问题吗

编辑:我找到了,但是当我添加额外的括号时,它抛出了一个新的错误

pointsOut=cv2.透视变换(a,h)cv2.错误:OpenCV(3.4.2) /io/opencv/modules/core/src/matmul.cpp:2268:error:(-215:Assertion) 失败)函数“perspectiveTransform”中的scn+1==m.cols


我还尝试在处理前将两幅图像转换为灰度,但仍然收到通道错误。

cv2。透视变换是一个返回点而不是图像的函数。您必须绘制它返回的点。例如

pointsOut = cv2.perspectiveTransform(a, H)
cv2.polylines(im_src, [np.int32(pointsOut)], True, (0, 255, 0), 5)
话虽如此,我的方法是先进行透视变换,然后将其环绕在图像上。类似于这个片段

M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
其中,maxWidth和maxHeight是源图像的

您可以在这里查阅更多信息,