前言

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_容器_知识点记录

    1.删除: 1.1.erase 1.2.remove / removeAt 2. 3. 4. 5.

  2. Android JNI(一)——NDK与JNI基础

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

  3. Spring AMQP 源码分析 03 - MessageConverter

    ### 准备 ## 目标 了解 Spring AMQP 消息转化实现   ## 相关资源 Quick Tour for the impatient:<http://docs.spring.io/ ...

  4. Codeforces 913C - Party Lemonade

    913C - Party Lemonade 思路:对于第i个话费cost[i],取min(cost[i],2*cost[i-1]),从前往后更新,这样就可以保证第n个的话费的性价比最高,那么从最高位开 ...

  5. 使用MyBatis Generator自动生成实体、mapper和dao层

    原文链接 通过MyBatis Generator可以自动生成实体.mapper和dao层,记录一下怎么用的. 主要步骤: 关于mybatis从数据库反向生成实体.DAO.mapper: 参考文章:ht ...

  6. C#退出模式

    1.this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit();  强制所有消息中 ...

  7. secs/gem协议

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

  8. Pavel and barbecue CodeForces - 756A (排列,水题)

    大意: 给定排列p, 0/1序列b, 有n个烤串, 每秒钟第i串会移动到$p_i$, 若$p_i$为1则翻面, 可以修改b和p, 求最少修改次数使得每串在每个位置正反都被烤过. 显然只需要将置换群合并 ...

  9. python-day4笔记

    1.文件后缀名对python运行没关系 2.Python解释器执行python程序的过程:python3 C:\test.py 1)启动python解释器(内存中) 2)将C:\test.py内容从硬 ...

  10. CF808D STL

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