I am currently trying to project the black dot in the following image:
To a 2D plane on the following image:
For "pts_dst" I am using the following coordinates:
With the points for the photo of the field being taken from the same location.
My code is as follows:
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)
But I am encountering the following error:
cv2.imshow("Warped Source Image", pointsOut)
cv2.error: OpenCV(3.4.2) /io/opencv/modules/imgcodecs/src/utils.cpp:622: error: (-15:Bad number of channels) Source image must have 1, 3 or 4 channels in function 'cvConvertImage'
For some reason this issue has been very difficult for me to solve - likely as I am new to OpenCV - but I can't seem to tackle it.
It looks as though it is referring to
pointsOut = cv2.perspectiveTransform(a, h)
I found this answer to a similar question but it doesn't solve my issue as I already have it set as floating point.
Does anyone have any suggestions as to what I am doing wrong or a better approach to solving this issue if I can't do so with perspectiveTransform()
?
EDIT: I found this solution but when I add the extra brackets, it throws a new error.
pointsOut = cv2.perspectiveTransform(a, h) cv2.error: OpenCV(3.4.2) /io/opencv/modules/core/src/matmul.cpp:2268: error: (-215:Assertion failed) scn + 1 == m.cols in function 'perspectiveTransform'
I also tried converting both images to grayscale before processing but still receive the channel error.