参考文章1https://www.it610.com/article/1294501697242079232.htm
参考文章2 opencv 两点之间角度 与倾斜校正应用_无左无右的博客-优快云博客
目的:图像坐标下:计算以P0,PA两个点组成的向量P0PA,与基准轴Y-(坐标系下:Y负【也就是朝上】)构成的夹角,此夹角范围是0-360°,。
升级点:
(1) 根据自己的需求,将起始角度0设置为 y负轴,正时针旋转角度为(0-360°)
(2) 按照y负的顺时针方向重新排布区域,第一象限(0-90),第二:(90-180)
************************************************************************
*函数名: get_point_angle2
* 沿着Y负向为起始0角度,顺时针旋转的角度
*函数作用: 已知2个坐标点,求从 0------->x 顺时针需旋转多少角度到该位置
* (0°)
* / \
* / |
* / |
* (4) | (1)
*(270°)----------------p0-------------------- x(90°)
* |
* (3) | (2)
* |
* |
* v
* (180°)
*
*函数参数:
*CvPoint2D32f pointO - 起点
*CvPoint2D32f pointA - 终点
*
*函数返回值:
*double 向量OA,从 0------->Y 顺时针需旋转多少角度到该位置
**************************************************************************/
double get_point_angle2(CvPoint pointO, CvPoint pointA)
{
double angle = 0;
CvPoint point;
double temp;
point = cvPoint((pointA.x - pointO.x), (pointA.y - pointO.y));
if ((0 == point.x) && (0 == point.y))
{
return 0;
}
if (0 == point.x)
{
if (point.y > 0)
{
angle =180;
}
if (point.y <0)
{
angle = 0;
}
return angle;
}
if (0 == point.y)
{
if (point.x > 0)
{
angle = 90;
}
if (point.x <0)
{
angle = 270;
}
return angle;
}
temp = fabsf(float(point.y) / float(point.x));
temp = atan(temp);
temp = temp * 180 / CV_PI;
if ((0 < point.x) && (0 > point.y)) //图像坐标系下 p1在p0的右上角;
{
angle = 90 - temp;
}
if ((0<point.x) && (0<point.y)) //图像坐标系下 p1在p0的右下角;
{
angle =90+ temp;
}
if ((0>point.x) && (0<point.y)) //图像坐标系下 p1在p0的左下角;
{
angle =270-temp ;
}
if ((0>point.x) && (0>point.y)) //图像坐标系下 p1在p0的左上角;
{
angle = 360 - temp;
return angle;
}
/*printf("sceneDrawing :: getAngle error!");*/
return angle;
}
2测试:
int main()
{
Point2f p_next(37.1651, 27.809);
Point2f p1(53.557514, 28.343039);
Point2f p2(34.750237, 45.588959);
Point2f p3(50.847076, 45.933853);
float M_potential_Angle_3P1 = get_point_angle2(p_next, p1);
float M_potential_Angle_3P2 = get_point_angle2(p_next, p2);
float M_potential_Angle_3P3 = get_point_angle2(p_next, p3);
cout << "M_potential_Angle_3P1=" << M_potential_Angle_3P1 << endl;
cout << "M_potential_Angle_3P2=" << M_potential_Angle_3P2 << endl;
cout << "M_potential_Angle_3P3=" << M_potential_Angle_3P3 << endl;
float M_potential_Angle_3P4 = CalculateAngle(p_next,p1);
cout << "M_potential_Angle_3P4=" << M_potential_Angle_3P4 << endl;
}