您的位置:首页 > 运维架构

opencv学习---Directory类基础知识介绍(读取文件夹下的所有文件的文件名)

2018-01-27 16:57 609 查看
只为总结,便于以后复习,便于其他同学参考!

opencv中有一个工具类Directory:它可以遍历对应文件夹下的所有文件,帮助读取文件夹下的所有文件名;

opencv2 中没有提供简单的文件名读取接口,需要重新编译文件;这个工具类在contrib.hpp下。

如果想用这个工具类的话,最好自己重新把contrib编译一下。(编译过程网上一堆教程)

Directory工具类中包含三个函数接口,分别是:

static std::vector< std::string > GetListFiles (const std::string &path, const std::string &exten="*", bool addPath=true)

static std::vector< std::string > GetListFilesR (const std::string &path, const std::string &exten="*", bool addPath=true)

static std::vector< std::string > GetListFolders (const std::string &path, const std::string &exten="*", bool addPath=true)

这三个函数都是static,函数只获取指定目录下的文件, 不理会目录。

(1) GetListFiles:遍历指定文件夹下的所有文件,不包括指定文件夹内的文件夹;

(2) GetListFolders:遍历指定文件夹下的所有文件夹,不包括指定文件夹下的文件;

(3) GetListFilesR:遍历指定文件夹下的所有文件,包括指定文件夹内的文件;这个R

    代表的recursive的意思,就是碰到文件夹还是往里钻。

path:string, 用于指定根目录

exten: string,这个是个正则表达式,匹配的返回,否则不返回。

addPath: bool,如果为true,返回的文件名会带path,如果为false,返回的仅是文件名;

/////测试代码

///这里测试代码用的是  fengbingchun 博主的

cv::Directory dir;  

  

string path1 = "E:/data/image";  

string exten1 = "*.bmp";//"*"  

bool addPath1 = false;//true;  

  

vector<string> filenames = dir.GetListFiles(path1, exten1, addPath1);  

  

cout<<"file names: "<<endl;  

for (int i = 0; i < filenames.size(); i++)  

    cout<<filenames[i]<<endl;  

  

string path2 = "E:/data/image";  

string exten2 = "*";//"Image*";//"*"  

bool addPath2 = true;//false  

  

vector<string> foldernames = dir.GetListFolders(path2, exten2, addPath2);  

  

cout<<"folder names: "<<endl;  

for (int i = 0; i < foldernames.size(); i++)  

    cout<<foldernames[i]<<endl;  

  

string path3 = "E:/data/image";  

string exten3 = "*";  

bool addPath3 = true;//false  

  

vector<string> allfilenames = dir.GetListFilesR(path3, exten3, addPath3);  

  

cout<<"all file names: "<&l
ada7
t;endl;  

for (int i = 0; i < allfilenames.size(); i++)  

    cout<<allfilenames[i]<<endl;  
参考博客: http://blog.csdn.net/u012436149/article/details/72179334 http://blog.csdn.net/fengbingchun/article/details/42435901
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐