本文共 931 字,大约阅读时间需要 3 分钟。
在做图像处理相关的任务时,经常遇到要遍历文件夹下所有图像的问题,实现图像的批处理,所以特地查找了遍历文件夹的方法,现总结如下:
实现思路是根据给定的文件夹路径,将该文件夹下的图像都存储近一个vector中,功能实现整合为一个函数,如下:
void getFiles(string path, vector& files){ long hFile = 0; struct _finddata_t fileinfo; string p; if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { do { if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) getFiles(p.assign(path).append("\\").append(fileinfo.name), files); } else { files.push_back(p.assign(path).append("\\").append(fileinfo.name)); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); }}
调用的大致步骤为:
char * filePath = "D:\\data\\train_image";vectorfiles;getFiles(filePath, files);int number = files.size();for (int i = 0; i < number; i++){ //利用循环遍历文件夹里的每一张图像 Mat SrcImage = imread(files[i].c_str()); ……//你的功能代码 }
根据自己文件夹的路径,对应修改filePath。
转载地址:http://xxssi.baihongyu.com/