opencv自带的目录文件遍历

COPY FROM:http://blog.csdn.net/watkinsong/article/details/9227439

在做图像处理的时候,可能进行一个文件夹的所有文件的遍历。

有一种比较笨的方式是使用c  的文件夹遍历方式,但是代码太难理解,而且如果在windows中使用还需要使用wchar_t宽字符。

opencv本身就有目录遍历的类库,非常方便,我以前还一直傻傻的使用c的方式进行遍历。


示例代码:非常简单的操作

需要特别注意的是:代码19行,vector<string> fileNames = dir.GetListFiles(dir_path, "*.jpg"false); 中的vector<string>其实是vector<cv::String>。而且这个函数在使用时需要contrib头文件和相关lib

[cpp]  view plain copy
  1. // OpencvDirTraverse.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "iostream"  
  6. #include "stdio.h"  
  7.   
  8. #include "opencv\cv.h"  
  9. #include "opencv\highgui.h"  
  10. #include <opencv2\opencv.hpp>  
  11.   
  12. using namespace std;  
  13. using namespace cv;  
  14.   
  15. int _tmain(int argc, _TCHAR* argv[])  
  16. {  
  17.     string dir_path = "D:/images/all_0407/";  
  18.     Directory dir;  
  19.     vector<string> fileNames = dir.GetListFiles(dir_path, "*.jpg"false);  
  20.   
  21.     for(int i=0; i < fileNames.size(); i++)  
  22.     {  
  23.         string fileName = fileNames[i];  
  24.         string fileFullName = dir_path + fileName;  
  25.         cout<<"file name:"<<fileName<<endl;  
  26.         cout<<"file paht:"<<fileFullName<<endl;  
  27.     }  
  28.   
  29.     system("pause");  
  30.     return 0;  
  31. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值