方案一:将文件夹中图像遍历到vector中
#include <vector> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> std::vector<cv::Mat> ReadImage(cv::String pattern) { std::vector<cv::String> fn; cv::glob(pattern, fn, false); std::vector<cv::Mat> images; int count = fn.size(); //number of png files in images folder for (int i = 0; i < count; i++) { images.emplace_back(cv::imread(fn[i])); } return images; }
int main() { cv::String pattern="/home/guopei/桌面/test_imgs/*.jpg"; std::vector<cv::Mat> img_list = ReadImage(pattern); return 0; }
方案二
#include <iostream> #include "opencv2/core.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" #include <opencv2/core/utils/filesystem.hpp> int main() { std::string img_dir = "../imgs"; if(cv::utils::fs::exists(img_dir)) { std::cout << "该文件夹存在" << std::endl; } // 获取当前文件夹下指定格式的文件 std::vector<cv::String> img_lists; cv::utils::fs::glob(img_dir, "*.jpg", img_lists); for(auto name:img_lists) { std::cout << name << std::endl; } return 0; }
标签:std,vector,img,int,c++,OpenCV,文件夹,include,cv From: https://www.cnblogs.com/lzy0709/p/17789601.html