Imshow issue reading images from directory.

asked 2017-10-25 20:57:06 -0500

blackmamba591 gravatar image

Hi,

I am trying to read images from a directory. However, throws me this error:

opencv3-3.2.0/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow

My code is as follows:

string dirName = "/home/an/all_samples/";
        DIR *dir;
        dir = opendir(dirName.c_str());
        string imgName;
        struct dirent *en; // creating a pointer towards dirent struct members


    if(dir != NULL){ // checking directory if NULL
        while ((en = readdir (dir)) != NULL) {
            string imgPath(dirName + en->d_name);
            Mat img = imread(imgPath);
            //cvtColor(img,img,CV_BGR2GRAY);
            //imshow("Greyscale_Image", img );
            //
            //namedWindow( "Grey_Scale_test", CV_WINDOW_AUTOSIZE );
            //
            imshow("Greyscale_Image", img );
            waitKey(0);
        }
        closedir(dir);

    } else {
        std::cout << "=====There are no Samples=====" << std::endl;
    }

any help will be appreciated. Thanks.

edit retag flag offensive close merge delete

Comments

1

see glob

sturkmen gravatar imagesturkmen ( 2017-10-26 02:43:51 -0500 )edit

glob did not work for me. I finally used boost:: filesystem.

Now it works for me.

blackmamba591 gravatar imageblackmamba591 ( 2017-10-26 03:31:04 -0500 )edit
2

please make it a habit of checking the results of imread(), like: if (img.empty()) ...

berak gravatar imageberak ( 2017-10-26 11:20:28 -0500 )edit