【批量读取文件夹中图片路径】使用c++ 中dirent.h 或者使用opencv3中的cv::glob

源码上一层文件夹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

参考文献

  1. C++使用opencv遍历文件夹

  2.  利用Directory类实现文件夹遍历(只适用于2.x版本的opencv,含源码分析)

  3. opencv:从文件中批量读取图片

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曾小蛙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值