opencv读取图像"JPEG文件的过早结尾" [英] opencv read image “Premature end of JPEG file”

查看:122
本文介绍了opencv读取图像"JPEG文件的过早结尾"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV从文件夹中读取图像.出现很多这样的消息:

I was using OpenCV to read the images from a folder. A lot of messages like this show up:

Corrupt JPEG data: premature end of data segment
Premature end of JPEG file
Premature end of JPEG file
Premature end of JPEG file

如何捕获此异常并删除这些图像文件?

How to catch this exception and remove these image files?

推荐答案

由于您说您正在读取图像"(多张图像),因此您将在要从中读取文件的文件夹中循环浏览文件. 在这种情况下,如果使用以下方法检查图像是否有效:

Since you said you are reading 'images' (multiple images), you would be looping through files in the folder that you are reading them from. In that case, if you check if the image is valid or not by using the following :

Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

if(! image.data )                              // Check for invalid input
{
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
}

然后您可以继续删除损坏/损坏的文件.

you can then proceed to deleting files which are corrupt/bad.

这篇关于opencv读取图像"JPEG文件的过早结尾"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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