【发布时间】:2019-09-24 20:15:42
【问题描述】:
场地照片的积分是从同一位置拍摄的。
我的代码如下:
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'
我还尝试在处理之前将两张图像都转换为灰度,但仍然收到通道错误。
【问题讨论】: