Below is a python script that calculates the homography between two images and then map a desired point from one image to another
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()
However, when i display the image that contains the mapped point it returns the following error:
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'
According to my knowledge this error means that parameter assigned to the function perspective transform is not correct or not being read. I checked the two images at the reading step and everything is fine. So anyone knows why this happens?
Thanks in advance Khaled