前言

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. Qt5.3.2openglVS2010_QSqlField_字段类型

    1.本来想通过 QSqlField::typeID() 来找字段类型,但是没找到... 然而看到了 SQL_INTEGER.SQL_SMALLINT等的使用(在“static QVariant::Ty ...

  2. 批量删除Redis数据库中的Key

    批量删除KeyRedis 中有删除单个 Key 的指令 DEL,但好像没有批量删除 Key 的指令,不过我们可以借助 Linux 的 xargs 指令来完成这个动作 redis-cli keys &q ...

  3. python数据持久存储-pickle模块

    pickle模块实现了基本的数据序列和反序列化.pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,通过pickle模块的反序列化操作,能够从文件中创建上一次程序保存的对象. 接 ...

  4. idea运行main方法报错,提示Shorten command line for xxx

    在Intell IDEA运行main函数的时候遇到了如下错误: Error running' xxxxxx': Command line is too long. Shorten command li ...

  5. Codeforces 895C - Square Subsets

    895C - Square Subsets 思路:状压dp. 每个数最大到70,1到70有19个质数,给这19个质数标号,与状态中的每一位对应. 状压:一个数含有这个质因子奇数个,那么他状态的这一位是 ...

  6. Java JDK5新特性-静态导入

    2017-10-31 00:10:50 静态导入格式:import static 包名 ...类名.方法名: 也就说可以直接导入到方法名. 注意: 方法必须是静态的 如果有多个同名的静态方法,容易不知 ...

  7. DownLoadImage

    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA&quo ...

  8. Enter键实现按钮相同功能

    1.在所在的按钮(Enter键功能)的容器上加上onkeydown="saveForKeyDown()",通常加载body上 <!-- 添加窗口--> <div ...

  9. hdu2845_最大不连续子段和

    ---恢复内容开始--- Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. Oracle11g温习-第七章:redo日志

      2013年4月27日 星期六 10:33 1.redo (重做) log 的功能:        用于数据恢复   2.redo log 特征: [特征]: 1)   记录数据块的变化(DML.D ...