【问题标题】:OpenCV channels when trying to project point on image to 2d plane尝试将图像上的点投影到二维平面时的 OpenCV 通道
【发布时间】:2019-09-24 20:15:42
【问题描述】:

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

到下图中的二维平面:

对于“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.error: OpenCV(3.4.2) /io/opencv/modules/imgcodecs/src/utils.cpp:622:错误:(-15:错误号码 通道数)源图像在功能上必须有 1、3 或 4 个通道 'cvConvertImage'

由于某种原因,这个问题对我来说非常难以解决——可能是因为我是 OpenCV 的新手——但我似乎无法解决它。

看起来好像是在指代

pointsOut = cv2.perspectiveTransform(a, h)

我发现this answer 有一个类似的问题,但它并没有解决我的问题,因为我已经将它设置为浮点数。

如果我无法通过perspectiveTransform() 解决此问题,是否有人对我做错了什么或解决此问题的更好方法有任何建议?

编辑:我找到了this solution,但是当我添加额外的括号时,它会引发一个新错误。

pointsOut = cv2.perspectiveTransform(a, h) cv2.error: OpenCV(3.4.2) /io/opencv/modules/core/src/matmul.cpp:2268: 错误: (-215:Assertion 失败)scn + 1 == m.cols in function 'perspectiveTransform'

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

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    cv2.perspectiveTransform 是一个返回点而不是图像的函数。您必须绘制它返回的点。一个例子是,

    pointsOut = cv2.perspectiveTransform(a, H)
    cv2.polylines(im_src, [np.int32(pointsOut)], True, (0, 255, 0), 5)
    

    话虽如此,我的方法是先进行透视变换,然后将其包裹在图像周围。类似于这个sn-p的东西,

    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
    

    其中 maxWidth 和 maxHeight 是源图像。

    你可以在这里参考更多, https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 2015-10-10
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多