//仿射变换,根据点求变换矩阵以及根据角度和缩放因子求变换矩阵
Mat mapx;
Mat mapy;
Mat src;
Mat dst;
int main()
{
//1.原图像。
src = imread("马车2.jpg");
resize(src, src, Size(), 0.1, 0.1);
imshow("效果图",src);
//2.确定两组点,每组点3个。
Point2f src_triangle[3];
src_triangle[0] = Point2f(0.,0.);
src_triangle[1] = Point2f(0.,static_cast<float>(src.rows-1));
src_triangle[2] = Point2f(static_cast<float>(src.cols-1),0.);
Point2f dst_triangle[3];
dst_triangle[0] = Point2f(0.12*src.cols,0.43*src.rows);
dst_triangle[1] = Point2f(0.33*src.cols,0.78*src.rows);
dst_triangle[2] = Point2f(0.65*src.cols,0.23*src.rows);
//3.从两组点来计算变换矩阵。//getAffine放射矩阵。
Mat fist_trans;
fist_trans.create(2,3,CV_32FC1);
fist_trans=getAffineTransform(src_triangle,dst_triangle);
//3.1计算转换后的图像
dst.create(src.rows,src.cols,src.type());
warpAffine(src,dst,fist_trans,src.size());
imshow(window,dst);
//4.再将变换后图像再次变换一个角度,和一个缩放。
float theta = 30.0;
float scale = 0.78;
//5.根据这个角度和缩放计算第二次变换的变换矩阵。getRotiation//仿射矩阵。
Mat second_mtrax;
second_mtrax.create(2,3,CV_32FC1);
second_mtrax=getRotationMatrix2D(Point2f(dst.cols/2.0,dst.rows/2.0),theta,scale);
//6.应用变换矩阵进行变换。warpaffine//仿射变换。
Mat dst2;
warpAffine(dst,dst2,second_mtrax,dst.size());
imshow("再次变换",dst2);
waitKey(0);
return 0;
}
来源:CSDN
作者:梅津太郎
链接:https://blog.csdn.net/gaocui883/article/details/103703361