OpenCV 之 特征檢測


    特征,也稱 興趣點 或 關鍵點,如下所示:除藍框區域平坦,無特征外,黑框內的“邊緣”,紅框內的“角點”,都可稱為“特征”

      

    以角點為例,它具有 旋轉不變性當圖像旋轉時,代表角點響應函數 R 的特征橢圓,其形狀保持不變

      

    然而,角點不具有 尺度不變性,左圖中被檢測為角點的特征,當放大到右圖的尺度空間時,會被檢測為 邊緣 或 曲線

       

   下面介紹具有尺度不變性的特征(檢測算法):SIFT、SURF、ORB、BRISK、KAZE 和 AKAZE 等

 

1  特征檢測

1.1  SIFT

    SIFT 全稱 Scale Invariant Feature Transform,是特征檢測中里程碑式的算法,也是目前最有效的特征檢測,該算法申請了專利,直到 2020年3月才過保護期

    OpenCV 從 4.4.0 起,已經將 SIFT 移到了主模塊 feature2d 中,SIFT 繼承自 Feature2D 類,而 Feature2D 繼承自 Algorithm 類,SIFT 的 create() 函數定義如下:

class SIFT : public Feature2D
{
public:
    static Ptr<SIFT> create(
        int nfeatures = 0,               // The number of best features to retain
        int nOctaveLayers = 3,           // The number of layers in each octave. 3 is the value used in D.Lowe paper
        double contrastThreshold = 0.04, // The contrast threshold used to filter out weak features in low-contrast regions
        double edgeThreshold = 10,       // The threshold used to filter out edge-like features
        double sigma = 1.6 );            // The sigma of the Gaussian applied to the input image at the octave 0  

    Algorithm 類中有兩個虛函數:detect() 檢測特征,compute() 計算描述符  

class Feature2D : public virtual Algorithm
{
public:
    /* Detects keypoints in an image (first variant) or image set(second variant). */
    virtual void detect(InputArray image, std::vector<KeyPoint>& keypoints, InputArray mask=noArray() );

    /* Computes the descriptors for a set of keypoints detected in an image (first variant) or image set (second variant). */
    virtual void compute(InputArray image, std::vector<KeyPoint>& keypoints, OutputArray descriptors );   

1.2  SURF

    SIFT 算法雖好,但計算速度不夠快,於是 SIFT 的近似版 SURF (Speeded Up Robust Features) 應運而生, SURF 的運行時間約為 SIFT 的 1/3

    SURF 屬於 xfeature2d 模塊,也繼承自 Feature2D 類,其 create() 函數定義如下:

namespace xfeatures2d
{
class SURF : public Feature2D
{
public:
    static Ptr<SURF> create(
        double hessianThreshold = 100, // Threshold for hessian keypoint detector used in SURF
        int nOctaves = 4,              // Number of pyramid octaves the keypoint detector will use
        int nOctaveLayers = 3,         // Number of octave layers within each octave
        bool extended = false,         // Extended descriptor flag (true, 128-element descriptors; false, 64-element descriptors)
        bool upright = false);         // Up-right or rotated features flag (true,do not compute orientation of features; false, compute orientation)  

    其中,hessianThreshold 為海森閾值,只有大於該閾值的特征才會被保留,因此,海森閾值越大,檢測到的特征越少。海森閾值取決於圖像對比度,一般取 300~500 時的檢測效果較好    

1.3  CenSurE

    CenSurE (Center Surround Extremas),是在 SURF 基礎上做的一種改進,基於 CenSurE 特征檢測 和 M-SURF 特征描述符,號稱比 SURF 更快,可用於實時處理領域

    OpenCV 並沒有完全實現 CenSurE 算法,而是借鑒衍生出了 StarDetector,其 create() 函數定義如下:

    static Ptr<StarDetector> create(
        int maxSize = 45,                   //
        int responseThreshold = 30,         //
        int lineThresholdProjected = 10,    //
        int lineThresholdBinarized = 8,     //
        int suppressNonmaxSize = 5          //
    );  

 

2  實時特征檢測

    SURF 的運行速度比 SIFT 快 3 倍,但在一些實時處理系統 (視覺里程計) 或低功耗設備中,SURF 還是不夠快,於是,便有了下面的兩種算法

2.1  ORB

    OpenCV Labs 實現了一種更快的算法 ORB - Oriented FAST and Rotated BRIEF,它是在 FAST 角點檢測 和 BRIEF 特征描述符的基礎上修改實現的

    視覺 SLAM (Simultaneous Localization and Mapping 同步定位與建圖) 領域中,著名的開源項目 ORB-SLAM,其特征提取就是基於 ORB 算法

    OpenCV 中 ORB 的 create() 函數定義如下:

static Ptr<ORB> create ( 
    int nfeatures = 500,           // The maximum number of features to retain
    float scaleFactor = 1.2f,      // Pyramid decimation ratio, greater than 1
    int nlevels = 8,               // The number of pyramid levels
    int edgeThreshold = 31,        // This is size of the border where the features are not detected
    int firstLevel = 0,            // The level of pyramid to put source image to
    int WTA_K = 2,                 // The number of points that produce each element of the oriented BRIEF descriptor
    ORB::ScoreType scoreType = ORB::HARRIS_SCORE, // The default HARRIS_SCORE means that Harris algorithm is used to rank features
    int patchSize = 31,            // size of the patch used by the oriented BRIEF descriptor
    int fastThreshold = 20         // the fast threshold
 );   

2.2  BRISK

    BRISK 號稱比 SURF 的運行速度快一個數量級,它基於 AGAST 角點檢測 和 BRIEF 特征描述符,其中 AGAST 是比 FAST 更快的一種角點檢測算法

    BRISK 的 create() 函數如下:

 /* The BRISK constructor */
    static Ptr<BRISK> create(
        int thresh = 30,            // AGAST detection threshold score 
        int octaves = 3,            // octaves detection octaves. Use 0 to do single scale
        float patternScale = 1.0f   // apply this scale to the pattern used for sampling the neighbourhood of a keypoint
    );

    /* The BRISK constructor for a custom pattern, detection thresholdand octaves */
    static Ptr<BRISK> create(
        int   thresh,                           // AGAST detection threshold score
        int   octaves,                          // detection octaves. Use 0 to do single scale.
        const std::vector<float> &radiusList,   // defines the radii(in pixels) where the samples around a keypoint are taken (for keypoint scale 1).
        const std::vector<int> &numberList,     // defines the number of sampling points on the sampling circle.Must be the same size as radiusList..
        float dMax = 5.85f,                     // threshold for the short pairings used for descriptor formation (in pixels for keypoint scale 1)
        float dMin = 8.2f,                      // threshold for the long pairings used for orientation determination (in pixels for keypoint scale 1)
        const std::vector<int>&indexChange = std::vector<int>()  // index remapping of the bits
    );  

2.3  BRIEF 描述符

    上述 ORB 和 BRISK 中,都提到了 BRIEF 特征描述符,BRIEF 全稱 Binary Robust Independent Elementary Feature),是用二進制串向量來描述特征的一種方式

    SIFT 中的一個特征,對應着一個由128個浮點數組成的向量,占 512 個字節;而 SURF 的一個特征,對應着一個由 64個浮點數組成的向量,占 256 個字節

    當有成千上萬個特征時, 特征描述符會占用大量的內存,並且會增加匹配的時間,在一些資源受限的場合,尤其是嵌入式系統中,SIFT 和 SURF 並非最優選擇

    而 BRIEF 特征描述符,采用的是二進制串,可將所占字節縮減為 64 或 32 甚至 16,相比 SIFT 和 SURF,大大減少了對內存的占用,非常適合於實時處理系統

    OpenCV 中 BRIEF 描述符的定義如下:

// Class for computing BRIEF descriptors described in @cite calon2010 .
class BriefDescriptorExtractor : public Feature2D
{
public:
    static Ptr<BriefDescriptorExtractor> create(
        int bytes = 32,                  // legth of the descriptor in bytes, valid values are: 16, 32 (default) or 64 .
        bool use_orientation = false);   // sample patterns using keypoints orientation, disabled by default.
};   

 

3  非線性尺度空間

    SIFT 和 SURF 是在線性尺度空間內的分析,在構建高斯尺度空間的過程中,高斯濾波會將圖像中的邊界細節信息等,連同噪聲一起模糊化掉,因此,會造成一定程度上特征定位精度的損失

    為了克服高斯濾波的缺點,2012年,西班牙人 Pablo F. Alcantarilla 利用非線性擴散濾波代替高斯濾波,通過加性算子分裂法 (Additive Operator Splitting) 構建了非線性尺度空間,提出了 KAZE 算法

    KAZE 是為了紀念“尺度空間分析之父” Iijima 而取得名字,在日語中是 “風” 的意思,發音“卡哉”;AKAZE 是 Accelerated KAZE,顧名思義是 KAZE 的加速版本

              

3.1  KAZE

     KAZE 的 create() 函數如下:

    /* The KAZE constructor */
    static Ptr<KAZE> create (
        bool extended = false,          // Set to enable extraction of extended (128-byte) descriptor
        bool upright = false,           // Set to enable use of upright descriptors (non rotation-invariant)
        float threshold = 0.001f,       // Detector response threshold to accept point
        int nOctaves = 4,               // Maximum octave evolution of the image
        int nOctaveLayers = 4,          // Default number of sublevels per scale level
        KAZE::DiffusivityType diffusivity = KAZE::DIFF_PM_G2  // Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or DIFF_CHARBONNIER
    );  

3.2  AKAZE

     AKAZE 的 create() 函數如下:

    /* The AKAZE constructor */
    static Ptr<AKAZE> create(
        AKAZE::DescriptorType descriptor_type = AKAZE::DESCRIPTOR_MLDB, // Type of the extracted descriptor: DESCRIPTOR_KAZE, DESCRIPTOR_KAZE_UPRIGHT, DESCRIPTOR_MLDB or DESCRIPTOR_MLDB_UPRIGHT.
        int descriptor_size = 0,                // Size of the descriptor in bits. 0 -> Full size
        int descriptor_channels = 3,            // Number of channels in the descriptor (1, 2, 3)
        float threshold = 0.001f,               // Detector response threshold to accept point
        int nOctaves = 4,                       // Maximum octave evolution of the image
        int nOctaveLayers = 4,                  // Default number of sublevels per scale level
        KAZE::DiffusivityType diffusivity = KAZE::DIFF_PM_G2  // Diffusivity type. DIFF_PM_G1, DIFF_PM_G2, DIFF_WEICKERT or DIFF_CHARBONNIER
    );  

 3.3  AKAZE vs ORB

    OpenCV Tutorials 中,有 ORB 和 AKAZE 的對比,從所選取的數據集來看,AKAZE 檢測到的特征匹配效果優於 ORB

      

 

4  代碼例程

    2004年 D. Lowe 提出 SIFT 算法后,在提高運算速度的方向上,先是誕生了比 SIFT 快3倍的 SURF,而后在 SURF 的基礎上,實現了可用於實時處理領域的 CenSurE

    BRIEF 特征描述符,利用二進制串描述符,減少了對內存的占用,提高了匹配的速度,特別適合資源受限的場合,如嵌入式系統

    在 BRIEF 的基礎上,ORB 結合 FAST 角點檢測 和 BRIEF 描述符,BRISK 結合 AGAST 角點檢測 和 BRIEF 描述符,真正實現了實時特征檢測

    KAZE 和 AKAZE 針對高斯濾波的缺點,另辟蹊徑,直接從 線性尺度空間 跳轉到 非線性尺度空間,變換尺度空間后,重新定義了特征檢測

    以上七種特征檢測的算法,代碼例程如下:

#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"

using namespace cv;

int main()
{
    // read
    Mat img = imread("messi.jpg");
    if (img.empty())
        return -1;

    // create and detect
    Ptr<SIFT> detector = SIFT::create();
    // Ptr<xfeatures2d::SURF> detector = xfeatures2d::SURF::create(400);
    // Ptr<xfeatures2d::StarDetector> detector = xfeatures2d::StarDetector::create(20, 20);
    // Ptr<ORB> detector = ORB::create(2000);
    // Ptr<BRISK> detector = BRISK::create();
    // Ptr<KAZE> detector = KAZE::create();
    // Ptr<AKAZE> detector = AKAZE::create();
    std::vector<KeyPoint>  keypoints;
    detector->detect(img, keypoints);

    // draw and show
    Mat img_keypoints;
    drawKeypoints(img, keypoints, img_keypoints);
    imshow("SIFT", img_keypoints);

    waitKey();
}  

    各算法的特征檢測效果如下:

              

                

 

后記

    一開始醞釀本篇博客時,目標是將 OpenCV 中所有的特征檢測算法,都閱讀一遍原始論文,並弄懂 OpenCV 的代碼實現,但隨着閱讀的深入,發現這幾乎是不可能完成的任務。

    第一,自己非學術科研人員,沒有太多時間和精力投入;第二,數學知識的薄弱,尤其是閱讀到 KAZE 算法,涉及非線性擴散濾波和加性算子分裂,深感數學的博大精深和自身能力的瓶頸。

    “吾生也有涯,而知也無涯”,想到牛人如 David Lowe,一生最有名的也只是發明了 SIFT 算法,我等凡夫俗子更難以遑論,莫名間竟生出一些悲涼,繼續寫下去的動力消失殆盡  ......

    好在這幾天想通了,重新認清自己的水平和定位,調整當初太過宏大的目標,改目標為 “介紹 OpenCV 中的特征檢測算法和使用例程”,於是,便有了本篇文章 ^_^

   

參考資料

  SIFT 算法作者 David Lowe 的主頁

  OpenCV-Python Tutorials / Feature Detection and Description / Introduction to SIFT (Scale-Invariant Feature Transform) 

  OpenCV-Python Tutorials / Feature Detection and Description / Introduction to SURF (Speeded-Up Robust Features)

  Censure: Center surround extremas for realtime feature detection and matching. In Computer Vision–ECCV 2008

  OpenCV-Python Tutorials / Feature Detection and Description / BRIEF (Binary Robust Independent Elementary Features)

  OpenCV-Python Tutorials / Feature Detection and Description / ORB (Oriented FAST and Rotated BRIEF)

  OpenCV Tutorials / 2D Features framework (feature2d module) / AKAZE and ORB planar tracking

  KAZE 和 AKAZE 作者 Pablo F. Alcantarilla 的個人主頁 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM