C ++ reading in image with different filenames without hardcoding

Is there a way to read in a set of images from a file, all of which have different names to each other, i.e. no continuity at all?

So if you had 4 images in one folder with filenames:

  • head.jpg
  • shoulders.png
  • knees.tiff
  • toes.bmp

Without hardcoding the filenames directly, so you can change Shoulders.png to say arm.gif is there a way to load them?

I currently have OpenCV and Boost

+3


source to share


1 answer


For someone else interested in:



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

      

+2


source







All Articles