因opencv directory已经不能使用,手写opencv contrib实现Directory类
C++读取一个文件夹下的所有文件并不方便,可以利用opencv
opencv中有一个工具类Directory:它可以遍历对应文件夹下的所有文件,帮助读取文件夹下的所有文件名。但是在opencv高版本中,contrib已经被摘出来了,导致directory不能使用。
给出方法:
自己写一个contrib.h文件,放同目录下,然后#include"contrib.h"
contrib.h内容如下:
#ifndef __OPENCV_CONTRIB_HPP__
#define __OPENCV_CONTRIB_HPP__
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
class CV_EXPORTS Directory
{
public:
static std::vector<std::string> GetListFiles(const std::string& path,const std::string &exten="*",bool addPath=true);
static std::vector<std::string> GetListFilesR(const std::string& path,const std::string &exten="*",bool addPath=true );
static std::vector<std::string> GetListFolders(const std::string& path, const std::string &exten = "*", bool addPath = true);
};
#endif
在cpp文件下写using namespace cv;
即可直接用Directory,例如:
Directory dir;
vector<string> fileNames = dir.GetListFiles(dirPath,"*.ply",false);
参考链接:
链接: CSDN下载.