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°,即:


發佈了58 篇原創文章 · 獲贊 20 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章