opencv中的仿射变换(程序)

(一)仿射变换的步骤:
1.得到仿射变换的算子
2.进行仿射变换的操作。

仿射变换的操作:
1.平移操作:三个点的坐标进行加运算
2.旋转操作:中心点,旋转角度,缩放比例构成仿射算子
3.缩放:可以根据旋转操作来,旋转操作中的角度为0度,有缩放比例来进行缩放操作,也可以用三点来进行缩放的操作。
最重要的一点是只要是重映射能操作的仿射变换均能进行操作的。
仿射变换的函数:warpAffine(输入,输出,仿射算子,输出图像的大小)
1.getAffineTransform 获得简单的仿射算子
2.getRotationMatrix2D 获得旋转缩放的仿射算子
仿射变换的程序:
#include <opencv2\opencv.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>

using namespace cv;
using namespace std;

void showHelpText()
{
printf("\n\n\n\n 当前的程序是仿射变换\n\n");
printf("当前的opencv的版本为:"CV_VERSION);
}
int main()
{
showHelpText();
Point2f srcTriangle[3];
Point2f dstTriangle[3];//定义两组点,每组有三个点
Mat rotMat(2,3,CV_32FC1);
Mat warpMat(2,3,CV_32FC1);//定义两个23的矩阵
Mat srcImg,dstImg,dstImg_warpRotate;
srcImg=imread(“2.jpg”);
if(!srcImg.data){
printf(“载入图片失败”);
return false;
}
dstImg=Mat::zeros(srcImg.size(),srcImg.type());
srcTriangle[0]=Point2f(0,0);
srcTriangle[1]=Point2f(static_cast(srcImg.cols-1),0);
srcTriangle[2]=Point2f(0,static_cast(srcImg.rows-1));
dstTriangle[0]=Point2f(static_cast(srcImg.cols
0.0),static_cast(srcImg.rows0.33));
dstTriangle[1]=Point2f(static_cast(srcImg.cols
0.65),static_cast(srcImg.rows0.35));
dstTriangle[2]=Point2f(static_cast(srcImg.cols
0.15),static_cast(srcImg.rows*0.6));
//求得仿射变换
warpMat=getAffineTransform(srcTriangle,dstTriangle);//仿射因子
warpAffine(srcImg,dstImg,warpMat,dstImg.size());
//对图像缩放后在进行旋转,先仿射变换在进行旋转仿射变换
/Point center=Point(dstImg.cols/2,dstImg.rows/2);
double angle=30.0;
double scale=0.8;
//在仿射变换的基础上进行旋转变换
rotMat=getRotationMatrix2D(center,angle,scale); //旋转因子
warpAffine(dstImg,dstImg_warpRotate,rotMat,dstImg.size());
/
Point center=Point(srcImg.cols/2,srcImg.rows/2); //对原图进行旋转变换
double angle=0.0;
double scale=0.5;
//在仿射变换的基础上进行旋转变换
rotMat=getRotationMatrix2D(center,angle,scale); //旋转因子
warpAffine(srcImg,dstImg_warpRotate,rotMat,srcImg.size()); imshow(“原始的图像”,srcImg);
imshow(“仿射变换的图像”,dstImg);
imshow(“仿射变换之后进行旋转的图像”,dstImg_warpRotate);
waitKey(0);
return 1;
}

猜你喜欢

转载自blog.csdn.net/nbxuwentao/article/details/86300676