c++ read in image set with different file names without hardcoding

1.3k views Asked by At

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

1

There are 1 answers

1
MLMLTL On BEST ANSWER

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