OpenCV学习:fastAtan2函数解密

来源:互联网 发布:华为抢购软件神器 编辑:程序博客网 时间:2024/06/02 07:12

OpenCV学习:fastAtan2函数解密


高中数学中各种正弦函数,余弦函数总是把人搞得头大,但是具体应用时你会发现,其实你只需要搞清楚一个2π空间内函数分布即可。下面分析OpenCV中fastAtan2函数是怎么处理的方向问题。

fastAtan2函数在OpenCV中用户非常广,比如在SIFT描述子求取过程中需要计算特征点的方向,此时OpenCV的源码中就是使用的fastAtan2函数,fastAtan2函数原型如下:

float fastAtan2(float y,float x)
x—向量的x坐标
y—向量的y坐标
输入一个2维向量,计算这个向量的方向,以度为单位(范围是0度---360度),精度是0.3度。


函数声明路径:/opencv-2.4.5/modules/core/include/opencv2/core/core.hpp

函数定义路径:/opencv-2.4.5/modules/core/src/mathfuncs.cpp

----------------------------------------------------------------------------------------------------------------------------------------------------------------

下图是OpenCV中fastAtan2函数的求解过程:



----------------------------------------------------------------------------------------------------------------------------------------------------------------

源码以及分析如下:

static const float atan2_p1 = 0.9997878412794807f*(float)(180/CV_PI);static const float atan2_p3 = -0.3258083974640975f*(float)(180/CV_PI);static const float atan2_p5 = 0.1555786518463281f*(float)(180/CV_PI);static const float atan2_p7 = -0.04432655554792128f*(float)(180/CV_PI);float fastAtan2( float y, float x ){    float ax = std::abs(x), ay = std::abs(y);//首先不分象限,求得一个锐角角度    float a, c, c2;    if( ax >= ay )    {        c = ay/(ax + (float)DBL_EPSILON);        c2 = c*c;        a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;    }    else    {        c = ax/(ay + (float)DBL_EPSILON);        c2 = c*c;        a = 90.f - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;    }    if( x < 0 )//锐角求出后,根据x和y的正负性确定向量的方向,即角度。        a = 180.f - a;    if( y < 0 )        a = 360.f - a;    return a;}

综上所述,fastAtan2函数得出的角度是以X轴正方向为0°方向,然后角度确定按照逆时针方向,以360°为终点,角度范围0°- 360°,即:

0 0