Ask Your Question
0

I read image file name using 'fs::directory_iterator' . How can i read this file by using imread in opencv c++

asked 2018-08-20 02:16:15 -0600

Samjith888 gravatar image

updated 2018-08-20 02:29:30 -0600

I wrote the following code to read the files from a directory and ' fs::path(p.path()).filename()' this function returns the filename..How can i use the imread function after that?

    string path = "C:/Users/Sam/Images2/";
   for (const auto & p : fs::directory_iterator(path))
   {
       cout << fs::path(p.path()).filename() << " " << " Image " << endl;
      Mat img = imread((fs::path(p.path()).filename()), IMREAD_GRAYSCALE);
}
edit retag flag offensive close merge delete

Comments

take a look at train_HOG.cpp i think you will find many answers for your recent questions

sturkmen gravatar imagesturkmen ( 2018-08-20 02:38:25 -0600 )edit

@sturkmen : i have done those steps. I can read the file name by using '(fs::path(p.path()).filename())' , but can't pass this function into imread() and i would like to proceed with 'namespace fs = std::experimental::filesystem' because it is a new method added in visual studio 2017 '

Samjith888 gravatar imageSamjith888 ( 2018-08-20 02:44:45 -0600 )edit
1

imho, if you use this, you have to concat the filename to the path again, to read something valid.

berak gravatar imageberak ( 2018-08-20 04:04:18 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-08-20 04:01:14 -0600

berak gravatar image

updated 2018-08-20 04:05:31 -0600

maybe you should use opencv's glob() instead:

vector<String> fn;
glob("C:/Users/Sam/Images2/", fn);
for (auto f:fn) { // f contains the complete path to the file
    Mat img = imread(f);
}
edit flag offensive delete link more

Comments

@berak : your code works. but we can also use 'namespace fs = std::experimental::filesystem;' this concept

Samjith888 gravatar imageSamjith888 ( 2018-08-20 04:41:56 -0600 )edit

sure you can, it's just more difficult.

berak gravatar imageberak ( 2018-08-20 05:02:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-08-20 02:16:15 -0600

Seen: 66,301 times

Last updated: Aug 20 '18