opencv 批量读取图片\缩放\保存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/water_93/article/details/82023621

先把文件夹下的所有图片选中,重命名变成image(i)的连续图片名字,然后可以对i进行循环遍历操作。

定义2个string字符串分别得到原图片所在文件夹和处理后的图片所在文件夹,注意在获得文件夹名的时候,把i转化成字符串ss进行连接。

#include "opencv2/opencv.hpp"  
#include "opencv2/imgproc/imgproc.hpp" 
#include <stdio.h>

using namespace std;  
using namespace cv;  
#define ImageNum 200

int main()  
{  
    Mat srcImage,dstImage;  
    string ImgName,savedImgName;  
    int i=1;  
    int resize_width,resize_height;
    resize_width=324;
    resize_height=216;

    while(i<=ImageNum)    
    {   
        ImgName="image";  
        //int i转换成string str  
        stringstream ss;  
        string str;  
        ss<<i;  
        ss>>str;  

        ImgName=ImgName+" ("+str+")";  //图像文件格式:ImgName(i),括号和引号都在英文状态下输入,并且左边引号和括号之间空1格
        ImgName = "D:\\tupian\\" + ImgName+".jpg";  
        cout<<"处理:"<<ImgName<<endl;  
        srcImage= imread(ImgName);//读取图片  
        if(srcImage.data ==0)  
        { printf("[error] 没有图片\n");}  

        //缩放图片
        resize(srcImage, dstImage, Size(resize_width,resize_height), 0, 0,INTER_LINEAR);
        //保存图片到新文件夹
        savedImgName = "D:\\tupian1\\" + str + ".jpg";
        imwrite(savedImgName,dstImage);
        i++;  
   }  

    waitKey(0);  
    system("pause");  
    return 4;  
}  

猜你喜欢

转载自blog.csdn.net/water_93/article/details/82023621