对指定文件夹下的图片进行批量处理

版权声明: https://blog.csdn.net/weixin_40614261/article/details/80658584

对指定文件夹下的图片进行批量处理
#include <cstdio>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>

#include "opencv\cv.h"
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\contrib\contrib.hpp"

#include<direct.h>

#include "shlwapi.h"
#pragma comment(lib,"shlwapi.lib")

using namespace std;
using namespace cv;
//得到仿射变换
Mat AffineTrans(Mat src, Point2f* scrPoints, Point2f* dstPoints)
{
	Mat dst;
	Mat Trans = getAffineTransform(scrPoints, dstPoints);
	warpAffine(src, dst, Trans, Size(src.cols, src.rows), CV_INTER_CUBIC, BORDER_REPLICATE);//, BORDER_REPLICATE
	return dst;
}
void main()
{
	//遍历指定文件夹,得到指定文件夹下的文件夹
	Directory dir;
	string path2 = "F:/test/20090802";
	string exten2 = "*";//"Image*";//"*"  
	bool addPath2 = false;//false 
	vector<string> foldernames = dir.GetListFolders(path2, exten2, addPath2);
	for (int i = 0; i < foldernames.size(); i++)
	{
		cout << endl;
		string shoespath = path2 + '/' + foldernames[i];
		string imgpath = shoespath + '/' + "Extend";
		//创建保存文件夹,并定义保存路径
		string a = "\\";
		string b = "/";
		string makeshoesfolder = "F:\\test\\result" + a + foldernames[i];
		_mkdir(makeshoesfolder.c_str());
		string imgwritepath = "F:/test/result" + b + foldernames[i];

		//此处可对文件夹下的文件进行遍历,此处对文件夹内的图片进行遍历并处理
		string exten1 = "*.jpg";//"*"
		bool addPath1 = false;//true;
		vector<string> filenames = dir.GetListFiles(imgpath, exten1, addPath1);
		for (int n = 0; n< filenames.size(); n++)
		{
			string imgname = filenames[n];
			string srcpath = imgpath + '/' + imgname;
			//system("md F:\\test\\result\\%s");
			//CreateDirectory(res,NULL);
			string sn;
			stringstream ss;
			ss << n + 1;
			ss >> sn;
			//进行仿射变换
			Mat srcimg = imread(srcpath, 1);
			Mat dstimg;
			Mat rotMat;
			Point center = Point(srcimg.cols / 2, srcimg.rows / 2);
			Point2f AffinePoints0[3] = { Point2f(0, 0), Point2f(static_cast<float>(srcimg.cols / 2 - 1), static_cast<float>(srcimg.rows - 1)), Point2f(static_cast<float>(srcimg.cols - 1), 0) };
			Point2f AffinePoints1[3] = { Point2f(static_cast<float>(srcimg.cols - 1), 0), Point2f(static_cast<float>(srcimg.cols / 2 - 1), static_cast<float>(srcimg.rows - 1)), Point2f(0, 0) };
			Mat dst_affine = AffineTrans(srcimg, AffinePoints0, AffinePoints1);
			string transname = foldernames[i] + '_' + 'c' + sn + "_f" + '_' + imgname;
			//cout << "f" << endl;
			imwrite(imgwritepath + b + transname, dst_affine);
			//每张图片进行旋转角度
			for (int angle = -30; angle <= 30; angle = angle + 5)
			{
				rotMat = getRotationMatrix2D(center, angle, 1);
				warpAffine(srcimg, dstimg, rotMat, dstimg.size(), CV_INTER_CUBIC, BORDER_REPLICATE);
			
				string sangle;
				stringstream ss;
				ss << angle;
				ss >> sangle;

				string transname = foldernames[i] + '_' + 'c' + sn + '_' + sangle + '_' + imgname;
				cout << transname << endl;
				//string Img_Name = "F:\\test\\result\\Desktop\\save\\" + to_string(k) + ".bmp";
				imwrite(imgwritepath + b + transname, dstimg);
			}
		}
	}
}














目标是实现,对A文件夹里面的a,b,c,d文件夹里面图片进行旋转翻转(翻转用的仿射变换)完成后并还以a,b,c,d为文件夹进行保存。批量处理很好用,希望对你有帮助。

猜你喜欢

转载自blog.csdn.net/weixin_40614261/article/details/80658584