[英]OpenCV resize error(-215:Assertion Failed)
這是我為圖像構建線性分類器的代碼,並且 cv2.resize() 在將圖像從原始大小轉換為 (32,32) 時拋出錯誤
import numpy as np
import cv2
labels = ["dog","cat","panda"]
np.random.seed(1)
W = np.random.randn(3,3072)
b = np.random.randn(3)
orig = cv2.imread("panda_00001.png")
image = cv2.resize(orig,(32,32)).flatten()
scores = W.dot(image) + b
for (label,score) in zip(labels,scores):
print("[INFO] {}:{:.2f}".format(label,score))
cv2.putText(orig,"Label:{}".format(labels[np.argmax(scores)]),(10,30),cv2.FONT_HERSHEY_SIMPLEX,0.9,(0,255,0),2)
cv2.imshow("Image",orig)
cv2.waitkey(0)
執行時出現此錯誤
Traceback (most recent call last):
File "linearclassifier.py", line 11, in <module>
image = cv2.resize(orig,(32,32)).flatten()
cv2.error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:3929: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
您的圖片路徑似乎錯誤或無效。
將 null 檢查集成到腳本中的讀取圖片始終是一種好習慣:
例如:
import cv2
...
img = cv2.imread("myImage.jpg")
if (img.size == 0):
# error handling or throw exception here!
# do stuff with image here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.