前言

OpenCV 3.1之前的版本,在contrib目录下有提供遍历文件的函数,用起来比较方便。但是在最新的OpenCV 3.1版本给去除掉了。为了以后使用方便,这里将OpenCV 2.4.9中相关的函数给提取了出来,适合在Windows 64bits上使用。

不过,笔者在opencv2.4.10版本中并没有找到相关的文件。

实现代码

directory.hpp:

#ifndef FBC_CV_DIRECTORY_HPP_
#define FBC_CV_DIRECTORY_HPP_ // reference: include/opencv2/contrib/contrib.hpp (2.4.9) #ifndef __cplusplus
#error directory.hpp header must be compiled as C++
#endif #include <vector>
#include "core/fbcdef.hpp" namespace fbc { class FBC_EXPORTS Directory {
public:
std::vector<std::string> GetListFiles(const std::string& path, const std::string & exten = "*", bool addPath = true);
std::vector<std::string> GetListFilesR(const std::string& path, const std::string & exten = "*", bool addPath = true);
std::vector<std::string> GetListFolders(const std::string& path, const std::string & exten = "*", bool addPath = true);
}; } #endif // FBC_CV_DIRECTORY_HPP_

directory.cpp:

#include <windows.h>
#include "directory.hpp" // reference: contrib/src/inputoutput.cpp (2.4.9) namespace fbc{ std::vector<std::string> Directory::GetListFiles(const std::string& path, const std::string & exten, bool addPath)
{
std::vector<std::string> list;
list.clear();
std::string path_f = path + "/" + exten;
WIN32_FIND_DATAA FindFileData;
HANDLE hFind; hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
return list;
} else {
do {
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL ||
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE ||
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_HIDDEN ||
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_SYSTEM ||
FindFileData.dwFileAttributes == FILE_ATTRIBUTE_READONLY) {
char* fname;
fname = FindFileData.cFileName; if (addPath) {
list.push_back(path + "/" + std::string(fname));
} else {
list.push_back(std::string(fname));
}
}
} while (FindNextFileA(hFind, &FindFileData)); FindClose(hFind);
} return list;
} std::vector<std::string> Directory::GetListFilesR(const std::string& path, const std::string & exten, bool addPath)
{
std::vector<std::string> list = Directory::GetListFiles(path, exten, addPath);
std::vector<std::string> dirs = Directory::GetListFolders(path, exten, addPath); std::vector<std::string>::const_iterator it;
for (it = dirs.begin(); it != dirs.end(); ++it) {
std::vector<std::string> cl = Directory::GetListFiles(*it, exten, addPath);
list.insert(list.end(), cl.begin(), cl.end());
} return list;
} std::vector<std::string> Directory::GetListFolders(const std::string& path, const std::string & exten, bool addPath)
{
std::vector<std::string> list;
std::string path_f = path + "/" + exten;
list.clear(); WIN32_FIND_DATAA FindFileData;
HANDLE hFind; hFind = FindFirstFileA((LPCSTR)path_f.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
return list;
} else {
do {
if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
strcmp(FindFileData.cFileName, ".") != &&
strcmp(FindFileData.cFileName, "..") != ) {
char* fname;
fname = FindFileData.cFileName; if (addPath) {
list.push_back(path + "/" + std::string(fname));
} else {
list.push_back(std::string(fname));
}
}
} while (FindNextFileA(hFind, &FindFileData)); FindClose(hFind);
} return list;
}
}

测试代码test_directory.cpp:

#include <string>
#include <vector>
#include <iostream>
#include "directory.hpp"
#include "test_directory.hpp" int test_directory_GetListFiles()
{
fbc::Directory dir; std::string path = "E:/GitCode/OpenCV_Test/test_images";
std::string exten = "*.jpg"; //"*";
bool addPath = false; //true; // 遍历指定文件夹下的所有文件,不包括指定文件夹内的文件夹
std::vector<std::string> filenames = dir.GetListFiles(path, exten, addPath); std::cout << "file names: " << std::endl;
for (int i = ; i < filenames.size(); i++)
std::cout <<" "<< filenames[i] << std::endl; return ;
} int test_directory_GetListFilesR()
{
fbc::Directory dir; std::string path = "E:/GitCode/OpenCV_Test/test_images";
std::string exten = "*";
bool addPath = true; //false // 遍历指定文件夹下的所有文件,包括指定文件夹内的文件夹
std::vector<std::string> allfilenames = dir.GetListFilesR(path, exten, addPath); std::cout << "all file names: " << std::endl;
for (int i = ; i < allfilenames.size(); i++)
std::cout <<" "<< allfilenames[i] << std::endl; return ;
} int test_directory_GetListFolders()
{
fbc::Directory dir; std::string path = "E:/GitCode/OpenCV_Test/test_images";
std::string exten = "*d*"; //"*"
bool addPath = false; //true // 遍历指定文件夹下的所有文件夹,不包括指定文件夹下的文件
std::vector<std::string> foldernames = dir.GetListFolders(path, exten, addPath); std::cout << "folder names: " << std::endl;
for (int i = ; i < foldernames.size(); i++)
std::cout << " "<< foldernames[i] << std::endl; return ;
}

参考

1.大牛源链接

OpenCV代码提取:遍历指定目录下指定文件的实现的更多相关文章

  1. delphi遍历指定目录下指定类型文件的函数

    遍历指定目录下指定类型文件的函数// ================================================================// 遍历某个文件夹下某种文件,/ ...

  2. Java基础知识强化之IO流笔记49:IO流练习之 复制指定目录下指定后缀名的文件并修改名称的案例

    1. 复制指定目录下指定后缀名的文件并修改名称的案例     需求:复制指定目录下的指定文件,并修改后缀名.  • 指定的文件是:.java文件.     • 指定的后缀名是:.jad     • 指 ...

  3. iOS案例:读取指定目录下的文件列表

    // // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...

  4. socket实现两台FTP服务器指定目录下的文件转移(不依赖第三方jar包)

    通过socket实现两台FTP服务器指定目录下的文件转移,其中包含了基础了ftp文件列表显示.上传和下载.这里仅供学习用,需掌握的点有socket.ftp命令.文件流读取转换等 完整代码如下: Ftp ...

  5. linux --> 删除指定目录下所有文件

    删除指定目录下所有文件 代码样例: ///////////////////////////////////////////////////// //Name: DeleteFile //Purpose ...

  6. C# 获取指定目录下所有文件信息

    /// <summary> /// 返回指定目录下所有文件信息 /// </summary> /// <param name="strDirectory&quo ...

  7. python实现指定目录下批量文件的单词计数:并发版本

    在 文章 <python实现指定目录下批量文件的单词计数:串行版本>中, 总体思路是: A. 一次性获取指定目录下的所有符合条件的文件 -> B. 一次性获取所有文件的所有文件行 - ...

  8. [转]C# 获取指定目录下所有文件信息、移动目录、拷贝目录

    原文:http://blog.csdn.net/vchao13/article/details/6200255 1.获取指定目录下所有文件信息 /// <summary> /// 返回指定 ...

  9. PHP 获取指定目录下所有文件(包含子目录)

    PHP 获取指定目录下所有文件(包含子目录) //glob — 寻找与模式匹配的文件路径 $filter_dir = array('CVS', 'templates_c', 'log', 'img', ...

  10. PHP 批量获取指定目录下的文件列表(递归,穿透所有子目录)

    //调用 $dir = '/Users/xxx/www'; $exceptFolders = array('view','test'); $exceptFiles = array('BaseContr ...

随机推荐

  1. testNG 学习笔记 Day2 配置testNG自带的监听器

    IntelliJ IDEA配置testNG自带的监听器的时候,操作如下菜单栏中 run ----> 下拉菜单中的 Edit Configurations ----> 新矿口中TeatNG下 ...

  2. MongoDB(课时4 数据增加)

    3.4 数据操作(重点) 只要是数据库就绝对离不开最核心的功能:CRUD(增加Create.读取查询Retrieve.更新Update.删除Delete),除了增加之外,其他都很麻烦,最麻烦的是修改. ...

  3. secs/gem协议

    介绍SECS/GEM SEMI SECS/GEM标准概述 SECS/GEM是由国际半导体设备与材料协会(SEMI)制定的连接性标准.此连接性标准用于在设备和工厂的资讯和控制系统间建立通讯. SECS是 ...

  4. WPF几种高级绑定

    (1)Binding  + RelativeSource + AncestorType 模式  , 根据关联源所指定的类型,可动态绑定指定类型的Path属性(Path可以省略)(PS:动态指父级在运行 ...

  5. 自适应界面开发总结——WPF客户端开发

    1.由于界面大小是变化的,所以必须有一个稳定不变的参考界面(即在一个标准的界面尺寸下进行WPF界面开发,比如:发票查验V3.0的美工设计尺寸——1024*740):   PS:在WPF的用户控件Xam ...

  6. 在 Confluence 6 中连接一个 LDAP 目录

    希望将 Confluence 连接到一个 LDAP 目录: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 在左侧的面板中单击 用户目录(User Di ...

  7. CF808D STL

    D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. dp练习(5)——最长严格上升子序列

    1576 最长严格上升子序列  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给一个数组a1, a2 ... ...

  9. Eclipse重命名项目名后如何彻底修改工程名

    背景:在Eclipse中当我们修改了一个Web项目名称后,在再次运行该项目,发现使用新的名称无法正常的浏览,而用旧的名称去可以 解决方案: 1:修改该项目目录下:.project文件 <?xml ...

  10. 上传xslx文件设置accept的MIME 类型

    .dotx:application/vnd.openxmlformats-officedocument.wordprocessingml.template.docx:application/vnd.o ...