源码上一层文件夹test中存储的图片
使用两种不同方法读取的结果
总的代码
#include <opencv2/opencv.hpp>
// #include <contrib.hpp>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h>
using namespace std;
using namespace cv;
void readImgs_opencv3x();
void readImgs_dirent();
int main()
{
readImgs_dirent();
cout<<endl;
readImgs_opencv3x();
return 0;
}
void readImgs_dirent()
{
cout<<"readImgs_dirent...."<<endl;
DIR *dir;
struct dirent *entry;
char img_path[100]="../test/";
if ((dir=opendir(img_path))==NULL)
printf("Error opening \n");
else
{
int nImg=1;
while ((entry=readdir(dir))!=NULL)
{
char img_fullname[100]="";
if(strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0)
continue;
strcat(img_fullname,img_path);
strcat(img_fullname,entry ->d_name);
cout<<"------------processing "<< nImg <<" image: "<< img_fullname<<endl;
// imread(entry ->d_name);
nImg++;
}
}
}
void readImgs_opencv3x()
{
cout<<"readImgs_opencv3x...."<<endl;
string src_path="../test/";
vector <cv::String> fileNames;
string pattern1="";
string pattern2="*.txt";
string pattern3="*.jpg";
glob(src_path+pattern1,fileNames,false);
cout<<"pattern1=null"<<endl;
for (auto file :fileNames)
{
cout<<file<<endl;
}
// glob(src_path+pattern2,fileNames,false);
// cout<<"pattern2=txt"<<endl;
// for (auto file :fileNames)
// {
// cout<<file<<endl;
// }
// cout<<"pattern3=jpg"<<endl;
// glob(src_path+pattern3,fileNames,false);
// for (auto file :fileNames)
// {
// cout<<file<<endl;
// }
}
#if 0
void readImgs_opencv2x()
{
string inPath= "../test/";
Directory dir;
vector <string> fileNames = dir.GetListFiles(dirPath,"*,",true);
for (auto folder : fileNames)
cout<<folder<<endl;
}
#endif
参考文献