初识Opencv4.X----图像仿射变换

//图像仿射变换
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
    
    
	//图像的仿射变换,需要2x3的旋转矩阵,总共6个参数,通过原图像和变换后的对应3个点就可以构造六个方程解出6个参数
	Mat img = imread("person1.jpeg");
	Point2f src[3]= {
    
     Point2f(0,0),Point2f(0,img.cols-1),Point2f(img.rows-1,img.cols-1) };
	Point2f dst[3] = {
    
     Point2f(img.rows*0.11,img.cols*0.2),Point2f(img.rows*0.15,img.cols*0.7),Point2f(img.rows*0.81,img.cols*0.85) };
	Mat rotation1, img_dst1;
	rotation1 = getAffineTransform(src, dst);
	warpAffine(img, img_dst1, rotation1, Size(img.cols,img.rows));
	namedWindow("仿射变换", WINDOW_NORMAL);
	imshow("仿射变换", img_dst1);
	//利用仿射进行旋转操作
	double angle = 30;//旋转30°
	Point2f center(img.rows / 2, img.cols / 2);//旋转中心
	Mat rotation2, img_dst2;
	rotation2 = getRotationMatrix2D(center, angle, 1);//最后一个参数是图像两个轴的缩放因子
	namedWindow("旋转变换", WINDOW_NORMAL);
	warpAffine(img, img_dst2, rotation2, Size(img.cols,img.rows));
	imshow("旋转变换", img_dst2);
	waitKey(0);
	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_46146657/article/details/120235572