對指定文件夾下的圖片進行批量處理


對指定文件夾下的圖片進行批量處理
#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爲文件夾進行保存。批量處理很好用,希望對你有幫助。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章