错误:(-215:断言失败)scn + 1 == m.cols 在函数“cv::perspectiveTransform”中

Kha*_*led 7 python opencv numpy computer-vision homography

下面是一个Python脚本,它计算两个图像之间的单应性,然后将所需的点从一个图像映射到另一个图像

import cv2

import numpy as np


if __name__ == '__main__' :

  # Read source image.
  im_src = cv2.imread(r'C:/Users/kjbaili/.spyder-py3/webcam_calib/homography/khaledd 35.0 sec.jpg')

  # Five corners of the book in source image
  pts_src = np.array([[281, 238], [325, 297], [283, 330],[248, 325],[213, 321]])

  # Read destination image.
  im_dst = cv2.imread(r'C:/Users/kjbaili/.spyder-py3/webcam_calib/homography/20.jpg')

  # Five corners of the book in destination image.
  pts_dst = np.array([[377, 251],[377, 322],[316, 315],[289, 284],[263,255]])



  # Calculate Homography

  h, status = cv2.findHomography(pts_src, pts_dst)


  
  # provide a point i wish to map from image 1 to image 2
  a = np.array([[260, 228]])


  

  pointsOut = cv2.getPerspectiveTransform(a, h)

  # Display image
  cv2.imshow("treced_point_image", pointsOut)


  cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

但是,当我显示包含映射点的图像时,它返回以下错误:

error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\core\src\matmul.dispatch.cpp:531: 
error: (-215:Assertion failed) scn + 1 == m.cols in function 'cv::perspectiveTransform'
Run Code Online (Sandbox Code Playgroud)

据我所知,这个错误意味着分配给函数透视变换的参数不正确或未被读取。我在阅读步骤中检查了这两个图像,一切都很好。那么有人知道为什么会发生这种情况吗?

预先感谢哈立德

小智 2

您正在向 传递错误的参数cv2.getPerspectiveTransform()。该函数需要原始图像中的一组四个坐标和转换图像中的新坐标。pts_src您可以直接将和传递pts_dst给函数,您将得到变换矩阵。然后,您可以通过矩阵乘法(如 )获得点“a”的变换坐标a_transformed = np.dot(matrix, a)