OpenCV从仿射矩阵得到旋转量平移量缩放量

一、前言

我们都知道图像的仿射变换是将原图像通过仿射变换矩阵进行运算,得到仿射变换后的图像,而仿射变换矩阵中包含了图像旋转、平移以及缩放等信息,某些场合下,我们需要通过两组点的对应关系中得到旋转量、平移量以及缩放量。仿射变换矩阵中的值并不能直接都得到它们,需要进一步计算得出,本文主要基于opencv来实现从仿射矩阵得到旋转量平移量缩放量的值,本文主要上实现代码,有些小知识就不补充细说了。

二、自己封装的接口

typedef struct
{
	double Rot;  //旋转角度
	double Tx;   //X方向平移量
	double Ty;   //Y方向平移量
	double Sx;   //X方向涨缩值
	double Sy;   //Y方向涨缩值
	double Sh;   //剪切量
}AffineParam;


void getAffineParam2D(const Point2f srcTri[POINT_NUM], const Point2f dstTri[POINT_NUM], AffineParam& result, Mat& M, int& status)
{
	try{
		//仿射变换矩阵(三组点)
		Mat SrcM, DestM, SrcInvertM;
		SrcM = (Mat_<double>(3, 3) << srcTri[0].x, srcTri[1].x, srcTri[2].x, srcTri[0].y, srcTri[1].y, srcTri[2].y, 1, 1, 1);
		DestM = (Mat_<double>(3, 3) << dstTri[0].x, dstTri[1].x, dstTri[2].x, dstTri[0].y, dstTri[1].y, dstTri[2].y, 1, 1, 1);
		cv::invert(SrcM, SrcInvertM);
		M = DestM*SrcInvertM;

		cout << M << endl;

		//平移量
		result.Tx = M.at<double>(0, 2);
		result.Ty = M.at<double>(1, 2);

		//旋转角度,弧度
		double Alpha = M.at<double>(0, 1) / M.at<double>(0, 0);
		//rot 正值为逆时针方向
		result.rot = atan(Alpha );

		//求出剪切值
		double Bata = M.at<double>(1, 0) / M.at<double>(1, 1);
		result.Sh = (Bata  * cos(result.Rot) + sin(result.Rot)) / (cos(result.Rot) - Bata *sin(result.Rot));

		//缩放量
		result.Sx = M.at<double>(0, 0) / cos(result.rot);
		double Theta = M.at<double>(1, 0) + M.at<double>(1, 1);
		result.Sy = Theta  / ((cos(result.Rot) + sin(result.Rot)) * result.Sh + cos(result.Rot) - sin(result.Rot));
	}
	catch (exception e)
	{
		status = 0;
	}
	status = 1;
}

三、测试代码

void test()
{
	Point2f srcTri[3];
	Point2f dstTri[3];

	/// 设置3组点,求出变换矩阵
	srcTri[0] = Point2f(1023, 1048);
	srcTri[1] = Point2f(11812, 1613);
	srcTri[2] = Point2f(11108, 9826);


	//目标的三个点
	dstTri[0] = Point2f(796, 1877);
	dstTri[1] = Point2f(12786, 1305);
	dstTri[2] = Point2f(12919, 10463);

	AffineParam affine_result;
	Mat transformM;
	int status = 0;
	getAffineParam2D(srcTri, dstTri, affine_result, transformM, status);
	
	cout << "Tx:" << affine_result.Tx << endl
		<< "Ty:" << affine_result.Ty << endl
		<< "Rot:" << affine_result.Rot << endl
		<< "Sx:" << affine_result.Sx << endl
		<< "Sy:" << affine_result.Sy << endl
		<< "Sh:" << affine_result.Sh << endl;
	system("pause");
}

int _tmain(int argc, _TCHAR* argv[])
{
        test();
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值