OpenCV 3.1之前的版本,在contrib目录下有提供遍历文件的函数,用起来比较方便。但是在最新的OpenCV 3.1版本给去除掉了。为了以后使用方便,这里将OpenCV 2.4.9中相关的函数给提取了出来,适合在Windows 64bits上使用。
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, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0) { 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 = 0; i < filenames.size(); i++) std::cout <<" "<< filenames[i] << std::endl; return 0; } 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 = 0; i < allfilenames.size(); i++) std::cout <<" "<< allfilenames[i] << std::endl; return 0; } 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 = 0; i < foldernames.size(); i++) std::cout << " "<< foldernames[i] << std::endl; return 0; }