当我们使用opencv处理图片的时候,每次只能处理一张图像,如果我们想对许多图像进行批同样的处理,这时使用c++的文件批处理操作将会使我们的工作量变得很小。我们可以通过一个getFiles()函数来实现我们需要的功能,下面给出具体函数定义:
void getFiles(string path, vector<string>& files) {
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo; //大家可以去查看一下_finddata结构组成
//以及_findfirst和_findnext的用法,了解后妈妈就再也不用担心我以后不会编了
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*.jpg").c_str(), &fileinfo)) != -1) {
do {
//如果是目录,迭代之
//如果不是,加入列表
if ((fileinfo.attrib & _A_SUBDIR)) {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
else {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile);
}
}
有了这个函数,就可以很轻松地对图像进行批处理了,是不是很方便呢