OpenCV學習:fastAtan2函數解密

來源:互聯網
上載者:User

高中數學中各種正弦函數,餘弦函數總是把人搞得頭大,但是具體應用時你會發現,其實你只需要搞清楚一個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°,即:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.