3

Is there any way of reading in a set of images from file that all have varying names to each other, i.e. no continuity at all?

So if you had 4 images in the same folder with the file names of:

  1. head.jpg
  2. shoulders.png
  3. knees.tiff
  4. toes.bmp

Without hard coding the file names directly, so you could change shoulders.png to say arms.gif, is there a way of loading them in?

I currently have OpenCV and Boost available

MLMLTL
  • 1,519
  • 5
  • 21
  • 35
  • if you change **shoulders.png** to **arms.gif**, do you still need to know what body part it represents? or do you just need to load every image file located in a given directory? – m.s. Jun 18 '15 at 14:33
  • 2
    with boost (or dirent.h from internet) you can get a list of all filenames in a specific folder. You can loop over all those filenames, then try to load the filename with opencv (imread) and test whether a image could be loaded (mat.empty() may not be true after loading). if an image could be loaded it is an image, if it wasnt successfuly just ignore that file. – Micka Jun 18 '15 at 14:34
  • Cheers sehe, but if I knew how I wouldn't have asked. The file names in the question are arbitrary. Thanks @Micka, I'll look into that. – MLMLTL Jun 18 '15 at 14:38
  • So your question is actually : how to get the list of files in a directory ? – undu Jun 18 '15 at 15:10
  • I guess technically yes – MLMLTL Jun 18 '15 at 15:14

1 Answers1

2

For anyone else wondering:

#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
std::vector<cv::Mat> imageVec;
fs::path p ("."); 
fs::directory_iterator end_itr; 
// cycle through the directory
for (fs::directory_iterator itr(p); itr != end_itr; ++itr){
    // If it's not a directory, list it. If you want to list directories too, just remove this check.
    if (fs::is_regular_file(itr->path())) {
        if (fs::is_regular_file(itr->path())){
            cv::Mat img;
            img = cv::imread(itr->path().string());
            if(img.data) imagesVecc.push_back(img);
        }
        // assign current file name to current_file and echo it out to the console.
        std::string current_file = itr->path().string();
        std::cout << current_file << std::endl;
    }
}
MLMLTL
  • 1,519
  • 5
  • 21
  • 35
  • 1
    Well done. I had the frame done, but no experience with Gil/OpenCV :) (If you had made it clearer that you didn't need help there, you'd have had the answer sooner) – sehe Jun 18 '15 at 15:47