您的位置:首页 > 其它

fastAtan2函数解密

2015-04-20 20:44 615 查看
转载地址:http://www.csdn123.com/html/mycsdn20140110/18/1810e12f67a1c70147b5d668e8aeb935.html

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

高中数学中各种正弦函数,余弦函数总是把人搞得头大,但是具体应用时你会发现,其实你只需要搞清楚一个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°,即:



----

 atan2该函数的值域为

,可以通过对负数结果加

的方法,将函数的结果映射到

范围内。

atan2值域示意图,上下都是以x轴正方向为基点

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: