C++ 使用不同的文件名读入图像集,无需硬编码

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

本文关键字:图像 编码 文件名 C++      更新时间:2023-10-16

有没有办法从文件中读取一组图像,这些图像彼此具有不同的名称,即根本没有连续性?

因此,如果您在同一文件夹中有 4 张文件名为:

  1. 头.jpg
  2. 肩膀.png
  3. 膝盖.tiff
  4. 脚趾.bmp

无需直接对文件名进行硬编码,因此您可以更改肩膀.png比如手臂.gif,有没有办法将它们加载进来?

我目前有OpenCV和Boost可用

对于其他任何想知道的人:

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