1
import cv2
import numpy as np

img = cv2.imread("images/Back.jpg")

width,height = 250,350
pts1 = np.float32([[111,219],[287,188],[154,482],[352,440]])
pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])
matrix = cv2.getPerspectiveTransform(pts1,pts2)
imgOutput = cv2.warpPerspective(img,matrix,(width,height))


cv2.imshow("Image", img)
cv2.imshow("Output",imgOutput)

cv2.waitkey(0)

I am receiving the following error for this code:

error: (-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'

I am trying to get a birds eye view of an image. Can someone see what I am doing wrong, or what I am failing to do?

Red
  • 26,798
  • 7
  • 36
  • 58
  • Does this answer your question? [OpenCV-(-215:Assertion failed) \_src.total() > 0 in function 'cv::warpPerspective'](https://stackoverflow.com/questions/65919661/opencv-215assertion-failed-src-total-0-in-function-cvwarpperspective) – mutantkeyboard Apr 26 '21 at 08:09

1 Answers1

1

One possible reason is that opencv couldn't find the image you are trying to open. You won't get an error from trying to open an image that doesn't exist; it will return you an empty array.

But I believe the problem in your code is that you'll need to reorder the points in pts1 to match the way you structured pts2. Try applying this reorder function:

import cv2
import numpy as np

def reorder(pts):
    pts = np.array(pts).reshape((4, 2))
    pts_new = np.zeros((4, 1, 2), np.int32)
    add = pts.sum(1)
    pts_new[0] = pts[np.argmin(add)]
    pts_new[3] = pts[np.argmax(add)]
    diff = np.diff(pts, axis=1)
    pts_new[1] = pts[np.argmin(diff)]
    pts_new[2] = pts[np.argmax(diff)]
    return pts_new

width, height = 250, 350
img = cv2.imread("images/Back.jpg")

pts1 = np.float32(reorder([[111, 219], [287, 188], [154, 482], [352, 440]]))
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])

matrix = cv2.getPerspectiveTransform(pts1, pts2)
imgOutput = cv2.warpPerspective(img, matrix, (width, height))

cv2.imshow("Image", imgOutput)
cv2.waitKey(0)

Also note that in your code, you used cv2.waitkey with a lowercase k. It will give you an attribute error, as it's supposed to be cv2.waitKey with an uppercase k.

Red
  • 26,798
  • 7
  • 36
  • 58