![](/img/trans.png)
[英]OpenCV-(-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'
[英]error: (-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'
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)
我收到此代碼的以下錯誤:
error: (-215:Assertion failed) _src.total() > 0 in function 'cv::warpPerspective'
我正在嘗試鳥瞰圖像。 有人能看到我做錯了什么,或者我沒有做什么嗎?
一個可能的原因是 opencv 找不到您要打開的圖像。 嘗試打開不存在的圖像不會出錯; 它會返回一個空數組。
但我相信您的代碼中的問題是您需要重新排序pts1
中的點以匹配您構建pts2
的方式。 嘗試應用此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)
另請注意,在您的代碼中,您使用了帶有小寫k
的cv2.waitkey
。 它會給你一個屬性錯誤,因為它應該是帶有大寫k
的cv2.waitKey
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.