4

I am currently trying to project the black dot in the following image: enter image description here

To a 2D plane on the following image: enter image description here

For "pts_dst" I am using the following coordinates: enter image description here

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
DougM
  • 920
  • 1
  • 9
  • 21

1 Answers1

1

cv2.perspectiveTransform is a function that returns the point and not the image. You have to plot the points that are returned by it. An example would be,

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

Having said that, my approach would be to get perspective transform first and then wrap it around the image. Something similar to this snippet,

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

where maxWidth and maxHeight are of the source image.

You can refer for more here, https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Chandan M S
  • 391
  • 1
  • 6