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?
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.