對文件進行批量處理(含模式選擇)

#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()
{	
	cout << "switch mode:";
	char mode;
	cin >> mode;
	cout << mode << endl;
	Directory dir;
	string path2 = "F:/test/20090810_x";
	cout << path2;
	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 valpath = "F:\\yangben\\second\\trainall\\val" + a + foldernames[i];
		string writeval = "F:/yangben/second/trainall/val" + b + foldernames[i];
		string trainpath = "F:\\yangben\\second\\trainall\\train" + a + foldernames[i];
		string writetrain = "F:/yangben/second/trainall/train" + b + foldernames[i];
		string querypath = "F:\\yangben\\second\\test\\query" + a + foldernames[i];
		string writequery = "F:/yangben/second/test/query" + b + foldernames[i];
		string gallerypath = "F:\\yangben\\second\\test\\gallery" + a + foldernames[i];
		string writegallery = "F:/yangben/second/test/gallery" + b + foldernames[i];
		switch (mode)
		{
		case 'x':
			_mkdir(valpath.c_str());
			_mkdir(trainpath.c_str());
			break;
		case 'c':
			_mkdir(querypath.c_str());
			_mkdir(gallerypath.c_str());
			break;
		}
		
		/*
		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_f = foldernames[i] + '_' + 'c' + sn + "_f" + '_' + imgname;
			switch (mode)
			{
			case 'x':imwrite(writetrain + b + transname_f, dst_affine);
				break;
			case 'c': imwrite(writegallery + b + transname_f, dst_affine);
				break;
			}
			cout << transname_f << endl;
			//cout << "f" << endl;
			//imwrite(imgwritepath + b + transname_f, dst_affine);
			//每張圖片進行旋轉角度
			for (int angle = -30; angle <= 30; 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;
				
				switch (mode)
				{
				case 'x':
					if ((angle==0) && (n==1))
					imwrite(writeval + b + transname,dstimg);
					else
					imwrite(writetrain+ b + transname, dstimg);
					break;
				case 'c':
					if ((angle==0) && (n<10))
					imwrite(writequery + b + transname, dstimg);
					else 
					imwrite(writegallery + b + transname, dstimg);
					break;
				}
				cout << transname << endl;
				//string Img_Name = "F:\\test\\result\\Desktop\\save\\" + to_string(k) + ".bmp";
			}
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章