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 ...
随机推荐
- TypeScript基础学习
什么是TypeScript? TypeScript是一种由微软开发的自由的和开源的编程语言,它是JavaScript的一个超集,扩展了JavaScript的语法. TypeScript支持任意浏览器, ...
- js获取url 参数
window.getRequest = function (url) { var theRequest = new Object(); var indexOf = url.indexOf(" ...
- 解决在nginx+php环境下$_SERVER['PHP_SELF']获取不到值的问题
Tp3.2. __APP__获取值不正确.$_SERVER['PHP_SELF']为空导致. 原来是php.ini的问题. sudo vim /usr/local/php/etc/php.ini 重启 ...
- 03_ExeZZ.cpp
1.静态加载 DLL : #pragma comment(lib, "DllZZ.lib") __declspec(dllimport) void __stdcall AA(); ...
- mount: unknown filesystem type 'LVM2_member'解决方案【转】
一台服务器,普通/dev/sda1/2(硬盘一) 同步数据到 lvm_member(硬盘二) rsync两硬盘数据同步: From: http://hi.baidu.com/williwill/ite ...
- POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)
题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...
- Oracle12c中性能优化&功能增强新特性之全局索引DROP和TRUNCATE 分区的异步维护
Oracle 12c中,通过延迟相关索引的维护可以优化某些DROP和TRUNCATE分区命令的性能,同时,保持全局索引为有效. 1. 设置 下面的例子演示带全局索引的表创建和加载数据的过程. -- ...
- 多线程私有数据pthread_key_create
参照:http://blog.csdn.net/xiaohuangcat/article/details/18267561 在多线程的环境下,进程内的所有线程共享进程的数据空间.因此全局变量为所有线程 ...
- HDU 2492 树状数组
DES:按照位置编号给你选手的rank值.每场比赛要有一个裁判,位置和rank在两个选手之间.两场比赛裁判不同 或有一个选手不同则可以说 两场比赛不同.问你一共可以有多少场比赛. 思路是遍历每个人当裁 ...
- linux physical and virtual addressing modes
example 1: 特理地址和虚拟地址一致 Physical addressing mode requires no page tables and the CPU does not attempt ...