OpenCV调整大小错误-215:断言失败

问题描述

这是我的代码,用于为图像构建线性分类器,而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)).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'

解决方法

您的图片路径似乎错误或无效。

将空检查集成到脚本中已读取的图片始终是一个好习惯:

例如:

import cv2
... 
img = cv2.imread("myImage.jpg")

if (img.size == 0):
   # error handling or throw exception here!

# do stuff with image here 

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...