计算二维平面内两个矢量的旋转角度(带方向)
OpenCV中,计算两个方向向量的旋转角度及旋转方向。基本思路:向量1旋转到x轴正方向,记录对应的旋转角度,同理,将方向向量2旋转到x轴正方向,记录旋转角度,两角度作差,计算出从向量1旋转到向量2的角度,角度值为正,则为顺时针旋转,反之为逆时针旋转。
示例代码
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main()
{
Point2f p1(90.011, 32.924), p2(126.827, 100), p3(90.011, 0.0001576);
Point2f p4(59.589, 67.076), p5(0.00011792, 67.076), p6(59.589, 0);
Vec2f v(30.422, -34.152), v1(36.816, 67.076), v2(0, -32.9238), v3(-59.58888), v4(0, -67.076);
float angle1 = atan2(v[1], v[0]) * 180 / CV_PI;
float angle2 = atan2(v1[1], v1[0]) * 180 / CV_PI;
float angle3 = atan2(v2[1], v2[0]) * 180 / CV_PI;
float angle4 = atan2(v3[1], v3[0]) * 180 / CV_PI;
float angle5 = atan2(v4[1], v4[0]) * 180 / CV_PI;
float beta1 = angle2 - angle1;
float beta2 = angle3 - angle1;
float beta3 = angle4 - angle1;
float beta4 = angle5 - angle1;
cout << "beta1: " << beta1 << endl;
cout << "beta2: " << beta2 << endl;
cout << "beta3: " << beta3 << endl;
cout << "beta4: " << beta4 << endl;
system("pause");
}