OpenCV代码提取:遍历指定目录下指定文件的实现
前言
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代码提取:遍历指定目录下指定文件的实现的更多相关文章
- delphi遍历指定目录下指定类型文件的函数
遍历指定目录下指定类型文件的函数// ================================================================// 遍历某个文件夹下某种文件,/ ...
- Java基础知识强化之IO流笔记49:IO流练习之 复制指定目录下指定后缀名的文件并修改名称的案例
1. 复制指定目录下指定后缀名的文件并修改名称的案例 需求:复制指定目录下的指定文件,并修改后缀名. • 指定的文件是:.java文件. • 指定的后缀名是:.jad • 指 ...
- iOS案例:读取指定目录下的文件列表
// // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...
- socket实现两台FTP服务器指定目录下的文件转移(不依赖第三方jar包)
通过socket实现两台FTP服务器指定目录下的文件转移,其中包含了基础了ftp文件列表显示.上传和下载.这里仅供学习用,需掌握的点有socket.ftp命令.文件流读取转换等 完整代码如下: Ftp ...
- linux --> 删除指定目录下所有文件
删除指定目录下所有文件 代码样例: ///////////////////////////////////////////////////// //Name: DeleteFile //Purpose ...
- C# 获取指定目录下所有文件信息
/// <summary> /// 返回指定目录下所有文件信息 /// </summary> /// <param name="strDirectory&quo ...
- python实现指定目录下批量文件的单词计数:并发版本
在 文章 <python实现指定目录下批量文件的单词计数:串行版本>中, 总体思路是: A. 一次性获取指定目录下的所有符合条件的文件 -> B. 一次性获取所有文件的所有文件行 - ...
- [转]C# 获取指定目录下所有文件信息、移动目录、拷贝目录
原文:http://blog.csdn.net/vchao13/article/details/6200255 1.获取指定目录下所有文件信息 /// <summary> /// 返回指定 ...
- PHP 获取指定目录下所有文件(包含子目录)
PHP 获取指定目录下所有文件(包含子目录) //glob — 寻找与模式匹配的文件路径 $filter_dir = array('CVS', 'templates_c', 'log', 'img', ...
- PHP 批量获取指定目录下的文件列表(递归,穿透所有子目录)
//调用 $dir = '/Users/xxx/www'; $exceptFolders = array('view','test'); $exceptFiles = array('BaseContr ...
随机推荐
- Java 面向对象之接口、多态
01接口的概念 A:接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来完成 ...
- win10 操作系统 修改桌面图标
桌面右击出现菜单后,点击个性化: 点击左边菜单的主题,点击桌面图标设置,在新窗口中选择需要显示的图标接口 details please check following screenshot
- 使用 apply 函数族
之前,我们讨论过可以使用 for 循环,在一个向量或列表上进行迭代,重复执行某个表达式.但是在实践中,for 循环往往是最后的选择,因为每次迭代都是相互独立的,所以我们可以使用更简洁更方便的读写方式来 ...
- C#中使用Log4记录日志
具体步骤如下: 从网上下载log4net对应.net版本的dll 在C#项目中引用该dll 创建log4net对应的配置文件 在程序中使用 log4net的配置文件如下: <?xml versi ...
- Eclipse 设置代码风格
自动调整代码风格 快捷键Ctrl + Shift + F 或者 右键 source -> format 设置代码风格 window -> preference -> java -&g ...
- [Android教程] Cordova开发App入门(一)创建android项目
前言 Apache Cordova是一个开源的移动开发框架.允许使用标准的web技术-HTML5,CSS3和JavaScript做跨平台开发. 应用在每个平台的具体执行被封装了起来,并依靠符合标准的A ...
- codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)
题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元 ...
- Ant Man CodeForces - 704B (图论,贪心)
大意: 给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的距离计算公式 |xi - xj| + ci + bj seconds if j< i |xi - xj| ...
- HDU-3480 Division (四边形不等式优化DP)
题目大意:将n个数分成m组,将每组的最大值与最小值的平方差加起来,求最小和. 题目分析:先对数排序.定义状态dp(i,j)表示前 j 个数分成 i 组得到的最小和,则状态转移方程为dp(i,j)=mi ...
- iOS UI-IOS开发中Xcode的一些使用技巧
一.快捷键的使用 经常用到的快捷键如下: 新建 shift + cmd + n 新建项目 cmd + n 新建文件 视图 option + cmd + 回车 打开助理编 ...