opencv 遍历文件夹里面图像--实现

来源:互联网 发布:java 数据库导入redis 编辑:程序博客网 时间:2024/05/23 01:57

思路来自http://blog.csdn.NET/watkinsong/article/details/9227439,后来,我自己用VS2012+Opencv2.4.8实现了一下,只是没有显示图像。后来,稍微修改了一下,可以显示图像喽,这样就可以批处理指定目录文件夹里面的图像了。

1.head.h头文件

[cpp] view plain copy
 print?
  1. #ifndef _HEAD_H_  
  2. #define _HEAD_H_  
  3.   
  4. //OpencvDirTraverse.cpp : Defines the entry point for the console application.  
  5. #define _CRT_SECURE_NO_DEPRECATE  
  6.   
  7. #include <cstdio>  
  8. #include <vector>  
  9. #include <iostream>  
  10. #include <fstream>  
  11. #include <cstring>  
  12. #include <cstdlib>  
  13. #include <cmath>  
  14. #include <algorithm>  
  15.   
  16. #include "opencv\cv.h"  
  17. #include "opencv2\core\core.hpp"  
  18. #include "opencv2\highgui\highgui.hpp"  
  19. #include "opencv2\imgproc\imgproc.hpp"  
  20. #include "opencv2\contrib\contrib.hpp"  
  21.   
  22. using namespace std;  
  23. using namespace cv;  
  24.   
  25. #endif // !_HEAD_H_  


2.main.cpp源文件

[cpp] view plain copy
 print?
  1. #include "head.h"  
  2.   
  3. int main(int argc, char * argv[])  
  4. {  
  5.     string dir_path = "D:/Test/";  
  6.     Directory dir;  
  7.     vector<string> fileNames = dir.GetListFiles(dir_path, "*.jpg"false);  
  8.   
  9.     for(int i = 0; i < fileNames.size(); i++)  
  10.     {  
  11.         string fileName = fileNames[i];  
  12.         string fileFullName = dir_path + fileName;  
  13.         cout<<"file name:"<<fileName<<endl;  
  14.         cout<<"file paht:"<<fileFullName<<endl<<endl;  
  15.   
  16.         //Image processing  
  17.         Mat pScr;  
  18.         pScr=imread(fileFullName,1); //以文件名命名窗口   
  19.         imshow(fileName,pScr);  
  20.         waitKey(1000);  
  21.   
  22.   
  23.     }  
  24.   
  25.     //system("pause");  
  26.     return EXIT_SUCCESS;  
  27. }