#include <iostream>
#include <vector>
#include <time.h>
#include <opencv2/opencv.hpp>
//#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/contrib/contrib.hpp>
//#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
const int width = 64;
const int height = 64;
int main()
{
Mat src;
string test_path = "C:\\test\\xuetielong\\";
string tu_path = "C:\\tu\\xuetielong\\";
string final_path = "C:\\tu\\z_final\\xuetielong\\";
Directory dir;
//遍历文件夹内.bmp文件
vector<string> fileNames = dir.GetListFiles(test_path, "*.bmp", false);
for (int i = 0; i < fileNames.size(); i++)
{
//灰度读取图像,改变图像大小,保存
src = imread(test_path + fileNames[i], 0);
Mat dst(width, height, src.type());
resize(src, dst, dst.size(), 0, 0, INTER_LINEAR);
imwrite(tu_path + fileNames[i], dst);
}
vector<string> fileNames_tu = dir.GetListFiles(tu_path, "*.bmp", false);
for (int i = 0; i < fileNames_tu.size(); i++){
src = imread(tu_path + fileNames_tu[i], 1);
srand((unsigned)time(NULL));
for (int j = 1; j < 99; j++){
//int转string,获取仿射变换保存文件名
stringstream stream;
stream << j;
string name = final_path + stream.str() + "_" + fileNames_tu[i];
double Rand = double((rand() % 10000) - 5000) / 1000000;//产生随机数-0.005000到0.005000
double Rands = double((rand() % 99) + 9900) / 10000;//产生随机数0.9900到0.9999
Point2f srcTri[3];
Point2f dstTri[3];
//Mat rot_mat(2, 3, CV_32FC1);
Mat warp_mat(2, 3, CV_32FC1);
Mat warp_dst, warp_rotate_dst;
srcTri[0] = Point2f(0, 0);
srcTri[1] = Point2f(src.cols - 1, 0);
srcTri[2] = Point2f(0, src.rows - 1);
dstTri[0] = Point2f(src.cols*Rand, src.rows*Rand);
dstTri[1] = Point2f(src.cols*Rands, src.rows*Rand);
dstTri[2] = Point2f(src.cols*Rand, src.rows*Rands);
warp_mat = getAffineTransform(srcTri, dstTri);
warpAffine(src, warp_dst, warp_mat, src.size());
//旋转,缩放因子再次变换
//Point center = Point(warp_dst.cols / 2, warp_dst.rows / 2);
//double angle = 1.0;
//double scale = 0.998;
//rot_mat = getRotationMatrix2D(center, angle, scale);
//warpAffine(warp_dst, warp_rotate_dst, rot_mat, warp_dst.size());
imwrite(name, warp_dst);
}
}
waitKey(0);
return 0;
}
读取文件夹内一定图像,仿射变换后并保存,使文件夹内图像保证2000张以上。