c++ 读取文件夹下所有图像

本文介绍了一种方法,用于读取指定文件夹内的所有图像文件,并将其存储为图像矩阵和文件名列表。此过程包括检查目录是否存在、遍历文件夹中的每个文件、排除特殊目录并按字母顺序排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取文件夹下所有图像

void ReadImage(const std::string &image_path, std::vector<cv::Mat> &img_rgb_vec,
               std::vector<std::string> &image_name_vec) {

    std::vector<std::string> image_path_vec;
    /// 读取文件夹下所有图像路径
    DIR *dir;
    if ((dir = opendir(image_path.c_str())) == nullptr) {
        throw std::runtime_error("directory " + image_path + " does not exist");
    }
    dirent *dp;
    for (dp = readdir(dir); dp != nullptr; dp = readdir(dir)) {
        const std::string img_file_name = dp->d_name;
        if (img_file_name == "." || img_file_name == "..") {
            continue;
        }
        image_path_vec.push_back(image_path + "/" + img_file_name);
        image_name_vec.push_back(img_file_name);
    }
    closedir(dir);
    std::sort(image_path_vec.begin(), image_path_vec.end());
    std::sort(image_name_vec.begin(), image_name_vec.end());
 }

### C++读取文件夹图像的方法 在C++中,可以结合`std::filesystem`库或使用第三方库(如OpenCV)来读取文件夹中的图像。以下是两种常见的方法实现: #### 方法一:使用`std::filesystem`和OpenCV 此方法需要支持C++17标准,并使用`std::filesystem`来遍历文件夹中的所有文件[^2]。然后通过OpenCV的`imread`函数加载图像。 ```cpp #include <iostream> #include <filesystem> #include <opencv2/opencv.hpp> namespace fs = std::filesystem; int main() { std::string folder_path = "C:\\path\\to\\your\\images\\"; // 文件夹路径 for (const auto& entry : fs::directory_iterator(folder_path)) { if (entry.is_regular_file()) { // 确保是普通文件 std::string file_path = entry.path().string(); cv::Mat image = cv::imread(file_path); // 使用OpenCV读取图像 if (!image.empty()) { std::cout << "成功读取: " << file_path << std::endl; // 在此处添加对图像的处理逻辑 } else { std::cerr << "无法读取图像: " << file_path << std::endl; } } } return 0; } ``` 上述代码使用了`std::filesystem::directory_iterator`来遍历指定文件夹下的所有文件,并逐一用OpenCV读取图像[^2]。 --- #### 方法二:使用`_findfirst`和`_findnext`函数 这种方法适用于不支持C++17标准的情况,利用C语言风格的文件操作函数来获取文件列表[^3]。 ```cpp #include <iostream> #include <vector> #include <opencv2/opencv.hpp> #include <io.h> // _findfirst, _findnext bool get_filelist_from_dir(const std::string& path, std::vector<std::string>& files) { long hFile = 0; struct _finddata_t fileinfo; files.clear(); if ((hFile = _findfirst(path.c_str(), &fileinfo)) != -1) { do { if (!(fileinfo.attrib & _A_SUBDIR)) { // 排除子目录 files.push_back(fileinfo.name); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); return true; } return false; } int main() { std::string folder_path = "C:\\path\\to\\your\\images\\"; std::string search_path = folder_path + "*.jpg"; // 指定文件类型 std::vector<std::string> file_list; if (!get_filelist_from_dir(search_path, file_list)) { std::cerr << "无法打开文件夹: " << folder_path << std::endl; return -1; } for (const auto& file_name : file_list) { std::string file_path = folder_path + file_name; cv::Mat image = cv::imread(file_path); // 使用OpenCV读取图像 if (!image.empty()) { std::cout << "成功读取: " << file_path << std::endl; // 在此处添加对图像的处理逻辑 } else { std::cerr << "无法读取图像: " << file_path << std::endl; } } return 0; } ``` 该方法通过`_findfirst`和`_findnext`函数获取指定文件夹下的所有`.jpg`文件,并逐一用OpenCV加载图像[^3]。 --- ### 注意事项 1. **路径分隔符**:Windows系统下使用双反斜杠`\\`作为路径分隔符。 2. **文件类型**:可以根据需求修改文件扩展名过滤规则,例如改为`*.png`或其他格式。 3. **兼容性**:如果项目需要兼容旧版C++标准,建议使用方法二。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值