#include <stdio.h> #include<io.h> //下面查找文件的功能需要用到 #include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; void resizePic(char *destFile, char *srcFile); int main(int argc, char *argv[]) { struct _finddata_t fa; long fHandle; char destDir[] = "E:/newPicLib/"; char srcDir[] = "E:/oldPicLib/"; int i = 0; if ((fHandle = _findfirst("E:/oldPicLib/*.bmp", &fa)) == -1L)//这里可以改成需要的目录 { printf("当前目录下没有bmp文件 "); return 0; } else do { char tempDest[256] = {0}; char tempSrc[256] = {0}; strcat_s(tempDest, destDir); strcat_s(tempDest, fa.name); strcat_s(tempSrc, srcDir); strcat_s(tempSrc, fa.name); //cout << "dest file is:" << tempDest << " src file is :" << tempSrc << endl; printf("处理文件:%s... ...", fa.name); resizePic(tempDest, tempSrc); cout << "ok.." << ++i << endl; } while (_findnext(fHandle, &fa) == 0); _findclose(fHandle); return 0; } void resizePic(char *destFile, char *srcFile) { IplImage* img = cvLoadImage(srcFile, 0); IplImage* destImg = cvCreateImage(cvSize(360, 288), 8, 1); memset(destImg->imageData, 0 ,destImg->imageSize); int width = img->width; int height = img->height; int shiftH = (288 - height) / 2; int shiftW = (width - 360) / 2; cvSetImageROI(img, cvRect(shiftW, 0, 360, height)); cvSetImageROI(destImg, cvRect(0, shiftH, 360, height)); cvCopy(img, destImg); cvResetImageROI(img); cvResetImageROI(destImg); cvSaveImage(destFile, destImg); //cvShowImage("oral", img); cvShowImage("dest", destImg); cvWaitKey(); cvReleaseImage(&img); cvReleaseImage(&destImg); }