#include <iostream>
#include <vector>
#include <time.h>
#include <opencv2/opencv.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;
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++){
stringstream stream;
stream << j;
string name = final_path + stream.str() +
"_" + fileNames_tu[i];
double Rand =
double((rand() %
10000) -
5000) /
1000000;
double Rands =
double((rand() %
99) +
9900) /
10000;
Point2f srcTri[
3];
Point2f dstTri[
3];
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());
imwrite(name, warp_dst);
}
}
waitKey(
0);
return 0;
}
读取文件夹内一定图像,仿射变换后并保存,使文件夹内图像保证2000张以上。
转载请注明原文地址: https://ju.6miu.com/read-670186.html