您的位置:首页 > 运维架构

基于OpenCV批量处理文件夹中的图片的方法

2017-06-25 20:34 288 查看
在进行图像处理等问题是面临的一个问题是如何批量的处理图片,这些图片存在在一个文件夹中,如何对这个文件夹中的数据进行批处理是非常重要的,下面介绍几种常用的方法。


1. sprintf()函数法

这种方法最为简单,他是将路径的名字存放在一个数组中
//input为输入文件夹的路径,i为第几幅图像
//图像的命名格式都是1.jpg,2.jpg,...
sprintf(filename, "%s\\%d.jpg", input, i)
1
2
3
1
2
3

示例:
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;

void Resize(int m, int n) {
char filename[256];
char filename2[256];
for (int i = 1; i <= 10; i++) {
//input中有10个文件
sprintf(filename,"%s\\%d.jpg","input",i);
Mat img = imread(filename);
//  imshow("img", img);
Mat res;
resize(img, res, Size(m, n));
//输出
sprintf(filename2, "%s\\%d_resize.jpg","output",i);
imwrite(filename2, res);
}
}
int main() {
Resize(24, 24);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


2. windows中使用dir方法

在DOS的环境下将文件夹中的图像名称生成一个txt文档,这样就可以批量处理这个txt文档从而对图像进行批量处理。

命令形式为
dir /b > example.txt
即输出到example.txt文件中。
//DOS环境下
C:\WINDOWS\system32>cd C:\Users\AAA\Desktop\example
C:\Users\AAA\Desktop\example>dir /b > example.txt
1
2
3
1
2
3


3. c/c++中调用cmd的dir方法

这种方法要比上面的方法要好用的多,因为不必来回折腾,而且非常的方便。

代码如下: 

需要注意这一行语句,就是将字符串的最后一个\n去掉,可以单步调式去观察。 
output.back().resize(output.back().size() - 1);

#include<iostream>
#include<string>
#include<vector>
using namespace std;
void getDir(string filename, vector<string> &output){
FILE* pipe = NULL;
string pCmd = "dir /B " + filename;
char buf[256];
pipe = _popen(pCmd.c_str(), "rt");
if (pipe == NULL) {
cout << "file is not exist" << filename << endl;
exit(1);
}
while (!feof(pipe))
if (fgets(buf, 256, pipe) != NULL) {
output.push_back(string(buf));
output.back().resize(output.back().size() - 1);  //将\n去掉
}
_pclose(pipe);
}
int main() {
vector<string>output;   //output就是输出的路径集合
getDir("example", output);
for (auto c : output)
cout << c << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

输出的结果如下,输出了一系列的图片名称:
0_GaussianBlur.jpg
0_Perspective.jpg
0_Rotate108.jpg
0_Rotate144.jpg
0_Rotate180.jpg
0_Rotate216.jpg
0_Rotate252.jpg
0_Rotate288.jpg
0_Rotate324.jpg
0_Rotate36.jpg
0_Rotate360.jpg
0_Rotate72.jpg
example.txt
IMG_20151003_17250_GaussianBlur.jpg
IMG_20151003_17250_Perspective.jpg
IMG_20151003_17250_Rotate108.jpg
IMG_20151003_17250_Rotate144.jpg
IMG_20151003_17250_Rotate180.jpg
IMG_20151003_17250_Rotate216.jpg
IMG_20151003_17250_Rotate252.jpg
IMG_20151003_17250_Rotate288.jpg
IMG_20151003_17250_Rotate324.jpg
IMG_20151003_17250_Rotate36.jpg
IMG_20151003_17250_Rotate360.jpg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


4.OpenCV的类方法

OpenCV中有实现遍历文件夹下所有文件的类Directory,它里面包括3个成员函数:

(1)、GetListFiles:遍历指定文件夹下的所有文件,不包括指定文件夹内的文件夹; 

(2)、GetListFolders:遍历指定文件夹下的所有文件夹,不包括指定文件夹下的文件; 

(3)、GetListFilesR:遍历指定文件夹下的所有文件,包括指定文件夹内的文件夹。

若要使用Directory类,则需包含contrib.hpp头文件,此类的实现在contrib模块。 

注意:OpenCV3中没有这个模块,因为安全性移除了,可以安装,此种方法具体可见: 

http://blog.csdn.net/fengbingchun/article/details/42435901

OpenCV中有实现遍历文件夹下所有文件的类Directory,它里面包括3个成员函数:(1)、GetListFiles:遍历指定文件夹下的所有文件,不包括指定文件夹内的文件夹;(2)、GetListFolders:遍历指定文件夹下的所有文件夹,不包括指定文件夹下的文件;(3)、GetListFilesR:遍历指定文件夹下的所有文件,包括指定文件夹内的文件夹。

若要使用Directory类,则需包含contrib.hpp头文件,此类的实现在contrib模块。

下面为测试代码:

[cpp] view
plain copy

cv::Directory dir;  

  

string path1 = "E:/data/image";  

string exten1 = "*.bmp";//"*"  

bool addPath1 = false;//true;  

  

vector<string> filenames = dir.GetListFiles(path1, exten1, addPath1);  

  

cout<<"file names: "<<endl;  

for (int i = 0; i < filenames.size(); i++)  

    cout<<filenames[i]<<endl;  

  

string path2 = "E:/data/image";  

string exten2 = "*";//"Image*";//"*"  

bool addPath2 = true;//false  

  

vector<string> foldernames = dir.GetListFolders(path2, exten2, addPath2);  

  

cout<<"folder names: "<<endl;  

for (int i = 0; i < foldernames.size(); i++)  

    cout<<foldernames[i]<<endl;  

  

string path3 = "E:/data/image";  

string exten3 = "*";  

bool addPath3 = true;//false  

  

vector<string> allfilenames = dir.GetListFilesR(path3, exten3, addPath3);  

  

cout<<"all file names: "<<endl;  

for (int i = 0; i < allfilenames.size(); i++)  

    cout<<allfilenames[i]<<endl;  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: