opencv4 学习 14 仿射变换

1、旋转矩阵

getRotationMatrix2D( 
Point2f center, 旋转中心
double angle, 旋转角度,大于0逆时针方向;小于0顺时针方向
double scale 缩放因子
);

 2、计算仿射变换矩阵

getAffineTransform( const Point2f src[], const Point2f dst[] );
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char* argv[])
{
    Mat src = imread("lena.jpg", IMREAD_COLOR);

    Point2f srcTri[3];
    srcTri[0] = Point2f(0.f, 0.f);
    srcTri[1] = Point2f(src.cols - 1.f, 0.f);
    srcTri[2] = Point2f(0.f, src.rows - 1.f);

    Point2f dstTri[3];
    dstTri[0] = Point2f(0.f, src.rows*0.33f);
    dstTri[1] = Point2f(src.cols*0.85f, src.rows*0.25f);
    dstTri[2] = Point2f(src.cols*0.15f, src.rows*0.7f);

    Mat warp_mat = getAffineTransform(srcTri, dstTri);
    Mat warp_dst = Mat::zeros(src.rows, src.cols, src.type());
    warpAffine(src, warp_dst, warp_mat, warp_dst.size());

    Point center =  Point(warp_dst.cols/2, warp_dst.rows/2);
    double angle = -50.0;
    double scale = 0.6;

    Mat rot_mat = getRotationMatrix2D(center, angle, scale);
    Mat warp_rotate_dst;
    warpAffine(warp_dst, warp_rotate_dst, rot_mat, warp_dst.size());

    imshow("Source image", src);
    imshow("Warp", warp_dst);
    imshow("Warp + Rotate", warp_rotate_dst);

    waitKey(0);
    return EXIT_SUCCESS;
}

 

参考:

https://docs.opencv.org/3.4/d4/d61/tutorial_warp_affine.html

 
posted @ 2020-08-13 13:55  blackx  阅读(96)  评论(0编辑  收藏  举报