【问题标题】:Read all the image files within a directory using imread使用 imread 读取目录中的所有图像文件
【发布时间】:2014-08-26 14:57:14
【问题描述】:

我编写了以下代码,使用 imread 读取目录中的所有图像文件。但是代码不起作用并给出错误。

#include<iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/core/core.hpp>
#include<dirent.h>
#include<string.h>
using namespace std;
using namespace cv;
int main(){
    string dirName = "/home/Dataset/newImage";
    DIR *dir;
    dir = opendir(dirName.c_str());
    string imgName;
    struct dirent *ent;
    if (dir != NULL) {
        while ((ent = readdir (dir)) != NULL) {
             imgName= ent->d_name;
            Mat img = imread(imgName);
            cvtColor(img,img,CV_BGR2GRAY);
        }
        closedir (dir);
    } else {
        cout<<"not present"<<endl;
    }
}

错误:

    OOpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/buildd/opencv-2.3.1/modules/imgproc/src/color.cpp, line 2834
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.3.1/modules/imgproc/src/color.cpp:2834: error: (-215) scn == 3 || scn == 4 in function cvtColor

Aborted (core dumped)

我实际上忘记在前面的代码中添加行“imgName = ent->d_name”。对此感到抱歉。我已经更新了代码

【问题讨论】:

  • 您从未在此代码中将imgName 设置为任何内容。结果是一个无效的文件名 (none),这会导致抛出 cv::Exception,而不是由您的代码处理,因此进程终止。

标签: c++ opencv


【解决方案1】:

只需添加此行并确保您的imread 成功。

Mat img = imread(imgName);
if(img.empty()){
std::cout<<"Cannot load "<<imgName<<endl;
return -1;
}

在上面的代码中,您遇到了这样的错误,因为您试图将空 Mat 转换为灰度,这会引发这样的异常,因此请始终确保 Mat 在 imread 之后不为空。

【讨论】:

  • 我已经添加了代码,但我得到“无法加载”,即使我在其中添加了 imgName。
【解决方案2】:

这是失败的,因为 imread 只获取文件名,而不是完整路径。看到这个SO question

    while ((ent = readdir (dir)) != NULL) {
         imgName= ent->d_name;
        Mat img = imread(imgName);
        cvtColor(img,img,CV_BGR2GRAY);
    }

应该是这样的

    while ((ent = readdir (dir)) != NULL) {
        string imgPath(dirName + ent->d_name);
        Mat img = imread(imgPath);
        cvtColor(img,img,CV_BGR2GRAY);
    }

我对 dirent 不熟悉,因为我更喜欢 boost::filesystem 来处理这类事情。顺便说一句,我敢打赌,一些“printf 调试”在这里会有所帮助,以查看导致失败的 'imread' 的参数。

编辑:

看起来 OpenCV 的 imread 有一些已知问题,具体取决于您的程序是如何构建的。您的系统是 Windows 还是其他系统?

查看这些链接了解更多信息:

imread not working in Opencv

OpenCV imread(filename) fails in debug mode when using release libraries

要解决此问题,也许您可​​以尝试使用 C 接口,尤其是 cvLoadImage

【讨论】:

  • 仍然无法加载图像。在 imgPath 上使用 printf 时,我得到了正确的输出 - /home/Dataset/NewImage/17003.jpg。但 img.empty() 被证明是真的。
【解决方案3】:

我用你的代码做了这个......也许它可以帮助你(对不起,这是我的第一个答案,我不知道如何使代码看起来不错)

 #include<iostream>
 #include "opencv2/imgproc/imgproc.hpp"
 #include "opencv2/highgui/highgui.hpp"
 #include <opencv2/core/core.hpp>
 #include<dirent.h>
 #include<string.h>

 using namespace std;
 using namespace cv;

 int main()
 {
     string dirName = "/home/diego/Pictures/";
     DIR *dir;
     dir = opendir(dirName.c_str());
     string imgName;
     struct dirent *ent;
     if (dir != NULL) {
     while ((ent = readdir (dir)) != NULL) {
           imgName= ent->d_name;
           //I found some . and .. files here so I reject them.
           if(imgName.compare(".")!= 0 && imgName.compare("..")!= 0)
           {
             string aux;
             aux.append(dirName);
             aux.append(imgName);
             cout << aux << endl;
             Mat image= imread(aux);
             imshow(aux,image);
             waitKey(0);
           }
      }
      closedir (dir);
  } else {
      cout<<"not present"<<endl;
     }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-25
    • 2013-12-02
    • 2019-01-04
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    相关资源
    最近更新 更多