码迷,mamicode.com
首页 > 其他好文 > 详细

遍历文件夹,修改图片尺寸

时间:2014-10-29 16:47:13      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   os   ar   sp   文件   div   on   

#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文件\n");
		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);
}

 

遍历文件夹,修改图片尺寸

标签:des   blog   io   os   ar   sp   文件   div   on   

原文地址:http://www.cnblogs.com/sunniflyer/p/4059583.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!