c++读取具有不同文件名的图像集而无需硬编码

标签 c++ image opencv boost image-loading

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

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

  1. head.jpg
  2. shoulders.png
  3. 膝盖.tiff
  4. 脚趾.bmp

如果不直接对文件名进行硬编码,那么您可以将 shoulders.png 更改为 arms.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;
    }
}

关于c++读取具有不同文件名的图像集而无需硬编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30918237/

相关文章:

c++ - 编译pimpl成语代码的问题

c++ - 为什么 tuple_size 是特征而不是成员

c++ - printf 黑客的格式字符串

javascript - 页面重新加载和图像缓存

css - 外部 CSS 图像无法加载

image - MATLAB:对高斯噪声使用 imnoise 和 randn 有什么区别

c++ - 为什么这个函数返回的值与我在内联时返回的值不同?

ios - 没有调用 cvGet2D 的匹配函数

c++ - 使用 OSVR 将 Opencv Mat 转换为 opengl

c++ - cvLogPolar 导致 opencv 错误 "Bad flag (parameter or structure field)"