用opencv中的warpAffine获取仿射变换图片保存

#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张以上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值