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
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
相关文章推荐
- OpenCV学习笔记---- 挨个读取图像文件夹下的所有文件,进行样本训练
- 可视化webpart基础开发——TreeView控件读取文档库中的所有文件夹和文件(递归方法读取)
- Android入门学习笔记(一)|基础知识|文件数据存储读取|解析XML
- 基于OpenCV依次读取文件夹下的所有图像文件
- 读取指定文件夹中所有文件名以及文件路径,并读到matlab
- 可视化webpart基础开发——TreeView控件读取文档库中的所有文件夹和文件(递归方法读取)
- ubuntu C++ 读取文件夹下所有文件的文件名
- Linux C 读取文件夹下所有文件(包括子文件夹)的文件名
- 读取文件夹所有文件并保存文件名至指定文件
- c语言学习之基础知识点介绍(九):预处理指令和多文件开发
- java 读取某个文件夹下的所有文件夹和文件, 返回所有文件名 (转)
- C#控制台基础 directory获取一个文件夹下所有文件的名字(不包含文件夹)
- 可视化webpart基础开发——TreeView控件读取文档库中的所有文件夹和文件(递归方法读取)
- 如何读取文件夹下的所有文件名到TXT文件中
- 可视化webpart基础开发——TreeView控件读取文档库中的所有文件夹和文件(递归方法读取)
- 【Java学习笔记】基础知识学习17【文件文本读取写入】
- 读取文件夹中文件所有文件名
- 基于OpenCV依次读取文件夹下的所有图像文件
- Java实现读取文件夹下(包括子目录)所有文件的文件名
- java 读取某个文件夹下的所有文件夹和文件, 返回所有文件名