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