可以使用cv::glob()函数;
函数原型如下:
void cv::glob(cv::String pattern, std::vector<cv::String>& result, bool recursive = false)
函数参数如下:

  • pattern:图像路径名称;
  • result:路径下所有图片的路径名称集合;
  • recursive:是否包含子文件夹下的图片;

函数封装及测试代码如下:

bool ReadImage(std::string str_imgs_folder, std::vector<cv::Mat>& vec_mat_imgs)
{
    std::vector<std::string> vec_imgs_name;
    cv::glob(str_imgs_folder, vec_imgs_name, false);
    size_t i_imgs_num = vec_imgs_name.size();
    if (i_imgs_num < 1) { return false; }
    vec_mat_imgs.resize(i_imgs_num);
    for (size_t i = 0; i < i_imgs_num; ++i) {
        cv::Mat image = cv::imread(vec_imgs_name[i], cv::IMREAD_GRAYSCALE);
        if (image.empty()) { return false; }
        vec_mat_imgs[i] = image;
    }
    return true;
}

int main()
{
    std::string str_imgs_folder = "\\folder\\*.bmp";
    bool b_ret = false;
    std::vector<cv::Mat> vec_mat_imgs;
    b_ret = ReadImage(str_imgs_folder, vec_mat_imgs);
    std::system("pause");
    return 0;
}
GitHub 加速计划 / opencv31 / opencv
238
21
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
fba658b9 Improve precision of RotatedRect::points 16 小时前
97df136d Properly preserve KAZE/AKAZE license as mandated by BSD-3-Clause #28441 Close https://github.com/opencv/opencv/issues/28440 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 18 小时前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐