我在 OpenCV 中的函数“resize"中遇到了这个错误(-215:Assertion failed)!ssize.empty() [英] I am having trouble with this error (-215:Assertion failed) !ssize.empty() in function 'resize' in OpenCV

查看:69
本文介绍了我在 OpenCV 中的函数“resize"中遇到了这个错误(-215:Assertion failed)!ssize.empty()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Keras 的图像分类器应用于我的项目,但在此过程中我陷入了困境.虽然以前使用相同的代码我可以使用 OpenCV 来读取和训练图像,但是在切换到一批新图像后,它被错误捕获.所以我的猜测是我的文件类型有问题:

I am trying to apply a Keras' image classifier to my project, but down the road I got stuck with this. Though previously, with the same code I could use OpenCV to read and train images, but after switching to a new batch of images it got caught with the error. So my speculation is that there's something wrong with my file type:

这是来自出现错误的批次:

This is from the batch that got the error:

traf.204.jpg:JPEG 图像数据,JFIF 标准 1.01,纵横比,密度 1x1,段长 16,基线,精度 8,480x294,帧1

traf.204.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 480x294, frames 1

这是来自没有被错误捕获的批次:

This is from the batch that didn't get caught with the error:

bear.290.jpg:JPEG 图像数据,JFIF 标准 1.01,纵横比,密度 1x1,段长 16,基线,精度 8,224x224,帧3

bear.290.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 224x224, frames 3

但是文件类型好像完全一样(除了分辨率).我该如何解决这个问题?

But the file type seems to be exactly the same (except for the resolution). How can I fix this problem?

推荐答案

我应该添加一个 try/exception 以便我的代码可以绕过丑陋"的图片:

I was supposed to add a try/exception so my code could bypass "ugly" images:

try:
    path=os.path.join(mypath, n)
    img=cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    img=cv2.resize(img, (img_rows, img_cols))

except Exception as e:
    print(str(e))

cv2.resize() 是它应该捕获错误的地方,因为它无法调整损坏的"代码块的大小.图像.

cv2.resize() was where it was supposed to catch the error since it couldn't resize a "broken" image.

这篇关于我在 OpenCV 中的函数“resize"中遇到了这个错误(-215:Assertion failed)!ssize.empty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆