六、仿射变换

      空间变换中的仿射变换对应着五种变换,平移,缩放,旋转,翻转,错切。而这五种变化由原图像转变到变换图像的过程,可以用仿射变换矩阵进行描述。而这个变换过程可以用一个2*3的矩阵与原图进行相乘得到。

opencv通过两个函数的组合使用来实现仿射变换:

一、获得仿射映射矩阵。这也有两种方法:

1、三点法:
getAffineTransform     
Mat img=getAffineTransform(const Point2f* src, const Point2f* dst)

 参数介绍:参数一:const Point2f* src:原图的三个固定顶点

 参数二:const Point2f* dst:目标图像的三个固定顶点
 //返回值:Mat型变换矩阵,可直接用于warpAffine()函数
 //注意,顶点数组长度超过3个,则会自动以前3个为变换顶点;数组可用Point2f[]或Point2f*表示

2、指定比例和角度

getRotationMatrix2D     
Mat img=getRotationMatrix2D (CvPoint2D32f  center,double angle,double scale)
  参数一:CvPoint2D32f  center,表示源图像旋转中心
  参数二:double angle,旋转角度,正值表示逆时针旋转
  参数三:double scale,缩放系数

二、进行仿射变换

void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())

 参数一:InputArray src:输入变换前图像
 参数二:OutputArray dst:输出变换后图像,需要初始化一个空矩阵用来保存结果,不用设定矩阵尺寸
参数三:InputArray M:变换矩阵,用另一个函数getAffineTransform()计算
参数四 :Size dsize:设置输出图像大小
参数五:int flags = INTER_LINEAR:设置插值方式,默认方式为线性插值(另一种WARP_FILL_OUTLIERS)(还有好几种插值方式,在下面)
参数六:int borderMode=BORDER_CONSTANT:边界像素模式,默认值BORDER_CONSTANT
参数七:const Scalar& borderValue=Scalar(),在恒定边界情况下取的值,默认值为Scalar(),即0。

eg

#include<iostream>
#include<opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char* argv) {
	Mat src, dst;
	src = imread("D:/RM/OpenCv的学习/仿射变换/仿射变化/22.jpg");
	if (src.empty() ){
		cout<<"can not load ...."<<endl;
			return -1;
	}
	namedWindow("original image", WINDOW_AUTOSIZE);
	imshow("original image", src);
	Mat dst_warp, dst_warpRotateScale, dst_warpTransformation, dst_warpFlip;
	Point2f srcPoints[3];
	Point2f dstPoints[3];
	//第一种仿射变换调用——三点法
	srcPoints[0] = Point2f(0, 0);
	srcPoints[1] = Point2f(0, src.rows);
	srcPoints[2] = Point2f(src.cols, 0);
	
	dstPoints[0] = Point2f(0, src.rows * 0.3);
	dstPoints[1] = Point2f(src.cols * 0.25, src.rows * 0.75);
	dstPoints[2] = Point2f(src.cols * 0.75, src.rows * 0.25);
	Mat M1 = getAffineTransform(srcPoints, dstPoints); 
	warpAffine(src, dst_warp, M1, src.size());

	//第二种仿射变换的仿射,直接指定角度和比例
	Point2f center(src.cols / 2, src.rows / 2);
	double angle = 45;
	double scale = 0.5;

	Mat M2 = getRotationMatrix2D(center, angle, scale);
	warpAffine(src, dst_warpRotateScale, M2, Size(src.cols, src.rows), INTER_LINEAR);
	//平移
	Point2f srcPoints1[3];
	Point2f dstPoints1[3];

	srcPoints1[0] = Point2f(0, 0);
	srcPoints1[1] = Point2f(0, src.rows);
	srcPoints1[2] = Point2f(src.cols, 0);

	dstPoints1[0] = Point2f(src.cols / 3.0);
	dstPoints1[1] = Point2f(src.cols / 3, src.rows);
	dstPoints1[2] = Point2f(src.cols + src.cols / 3.0);
	
	Mat M3 = getAffineTransform(srcPoints1, dstPoints1);
	warpAffine(src, dst_warpTransformation, M3, Size(src.cols + src.cols / 3, src.rows));
	//翻转、镜像
	Point2f srcPoints2[3];
	Point2f dstPoints2[3];

	srcPoints2[0] = Point2i(0, 0);
	srcPoints2[1] = Point2i(0, src.rows);
	srcPoints2[2] = Point2i(src.cols, 0);
	dstPoints2[0] = Point2i(src.cols, 0);
	dstPoints2[1] = Point2i(src.cols, src.rows);
	dstPoints2[2] = Point2i(0, 0);
	Mat M4 = getAffineTransform(srcPoints2, dstPoints2);
	warpAffine(src, dst_warpFlip, M4, Size(src.cols, src.rows));
	//flip(src, dst_warpFlip, 1);//  flipCode:= 0 图像向下翻转
	//> 0 图像向右翻转
	//< 0 图像同时向下向右翻转
	imshow("affine transformation1(三点法)", dst_warp);
	imshow("affine transfoemation2(指定比例和角度)", dst_warpRotateScale);
	imshow("affine transfoemation3(仿射变换平移)", dst_warpTransformation);
	imshow("affine transformation4(仿射变换镜像)", dst_warpFlip);
	waitKey(0);
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值