【OpenCV】批量处理图片尺寸-64*64

时间:2021-06-27 14:52:12

使用的话修改对应的路径,和图片生成的位置。


#include <cv.h>
#include <highgui.h>
#include<io.h>
#include <string.h>
using namespace std;
using namespace cv;
char * filePath = "E:\\一";

void getFiles(string path, vector<string>& files)
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").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);
}
}

int main()
{
vector<string> files;
getFiles(filePath, files);
char str[30];
int size = files.size();
IplImage *image, *imageresize=0;

for (int i = 0; i < size; i++)
{
char filename[100] = "";
char dir[100] = "E:/";
sprintf(filename, "%d.jpg", i);
strcat(dir, filename);
image = 0;
image = cvLoadImage(files[i].c_str(), 1);

imageresize = cvCreateImage(cvSize(64, 64), IPL_DEPTH_8U, 3);
cvResize(image, imageresize, CV_INTER_LINEAR);

cout << "处理完毕" << dir << endl;

cvSaveImage(dir, imageresize);

cout << "处理完毕" << files[i].c_str() << endl;

cvReleaseImage(&image);
cvReleaseImage(&imageresize);

}
cvWaitKey(0);
}