特徵,也稱 興趣點 或 關鍵點,如下:藍框內區域平坦,無特徵;黑框內有“邊緣”,紅框內有“角點”,後二者都可視為“特徵”

角點作為一種特徵,它具有旋轉不變性,如下:當影象旋轉時,代表角點響應函式 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/core.hpp"
#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 的個人主頁