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

思路来自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. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python OpenCV是一个广泛使用的计算机视觉和图像处理库。glob模块是Python中用于获取匹配的文件路径列表的模块。如果需要遍历文件夹下的所有图片,可以使用glob模块。 首先,需要导入glob模块和cv2模块。 ```python import glob import cv2 ``` 接下来,需要使用glob模块来搜索指定路径下所有符合特定模式的文件,并将其存储在一个列表中。 ```python img_path = './images/*.jpg' # 以.jpg结尾的所有图片 img_files = glob.glob(img_path) ``` 在这个例子中,路径"./images/"是需要遍历文件夹路径。使用*代表匹配该路径下所有文件名,最后由".jpg"结尾的所有文件遍历所有图片的列表,加载并处理每一张图片 ```python for img_file in img_files: img = cv2.imread(img_file) # Process image here # ... ``` 这个循环处理每个文件的方式是通过读取文件名列表并使用cv2.imread()函数打开每个文件。在读取每个图像后可以对其进行进一步处理,如应用图像滤波器,改变大小或裁剪等等。 最后,应在完成处理后释放内存。可以使用cv2.destroyAllWindows()关闭窗口同时释放内存,或使用cv2.waitKey()来关闭窗口,等待用户按下任何键,然后释放内存。 ```python cv2.destroyAllWindows() ``` 总之,使用Python OpenCV和glob模块遍历文件夹下的所有图片非常简单。首先使用glob模块搜索符合特定模式的文件,然后使用cv2.imread()函数逐个加载并处理每个文件。完成后关闭窗口并释放内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值