1

I have written following code, to read all the image files within a directory using imread. But the code is not working and giving error.

#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;
    }
}

Error:

    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)

I actually forgot to add line "imgName = ent->d_name" in previous code. Sorry for that. I have updated the code

user3747190
  • 1,627
  • 3
  • 20
  • 28
  • 1
    You never set `imgName` to anything in this code. The result is an invalid file name (none), which results in a `cv::Exception` thrown, not handled by your code, so the process terminates. – WhozCraig Jul 05 '14 at 16:27

3 Answers3

4

This is failing because imread is only getting a file name, not a full path. See this SO question.

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

Should be something like

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

I am not familiar with dirent, as I prefer boost::filesystem for this kind of thing. Btw, I bet some "printf debugging" would be helpful here, to look at the arguments to 'imread' that lead to failure.

EDIT:

It looks like OpenCV's imread has some known issues, depending on how your program is built. Is your system Windows, or something else?

See these links for more information:

imread not working in Opencv

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

To troubleshoot this, perhaps you can try using the C interface, and in particular cvLoadImage.

Community
  • 1
  • 1
NicholasM
  • 4,557
  • 1
  • 20
  • 47
  • Still not able to load the image. On using printf on imgPath, I am getting correct output - /home/Dataset/NewImage/17003.jpg. But img.empty() is turning out to be true. – user3747190 Jul 06 '14 at 06:01
3

I did this with your code... maybe it can help you (Sorry, this is my first answer and I don't know how to make the code to look good)

 #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;
     }
 }
0

Just add this line and make sure your imread is successful.

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

In the above code you are getting such an error because, you are trying to convert empty Mat to gray scale, which will throw such an exception, so always make sure the Mat is not empty after imread.

Haris
  • 13,645
  • 12
  • 90
  • 121