zoukankan      html  css  js  c++  java
  • 遍历文件夹,修改图片尺寸

    #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);
    }
    
    Trouble is a Friend
  • 相关阅读:
    AcWing 826. 单链表
    AcWing 803. 区间合并
    codeforces Codeforces Round #597 (Div. 2) D. Shichikuji and Power Grid
    球球大作战.exe
    RGB MIXER三原色混色器的制作
    125. 验证回文串
    110. 平衡二叉树
    112. 路径总和
    111. 二叉树的最小深度
    100. 相同的树
  • 原文地址:https://www.cnblogs.com/sunniflyer/p/4059583.html
Copyright © 2011-2022 走看看