OpenCV-Python教程:37.SURF(加速穩(wěn)健特征)

理論

在前一節(jié),我們看到了SIFT來做關(guān)鍵點(diǎn)檢測和描述,但是它相對(duì)來說比較慢,人們需要更快的版本。在2006, 三個(gè)人,Bay,H.,Tuytelaars,T.和Van Gool, L, 發(fā)表了另一篇論文“SURF: Speeded Up Robust Features”介紹了一個(gè)新的算法叫SURF,名字中可以知道,他是加速版的SIFT。

SURF用盒子過濾器來近似LoG,下面的圖演示了這種近似。一個(gè)很大的好處是用盒子過濾器卷積可以很容易的計(jì)算,還可以在不同尺度并行計(jì)算。SURF在尺度和位置上依賴Hessian矩陣的決定。


OpenCV里的SURF

OpenCV提供了SURF函數(shù),你用一些可選條件,比如64/128-維的描述來初始化一個(gè)SURF對(duì)象,然后做SIFT,我們可以使用SURF.detect(), SURF.compute()來找關(guān)鍵點(diǎn)和描述。

首先我們會(huì)看到一個(gè)簡單的demo

>>> img = cv2.imread('fly.png',0)

# Create SURF object. You can specify params here or later.
# Here I set Hessian Threshold to 400
>>> surf = cv2.SURF(400)

# Find keypoints and descriptors directly
>>> kp, des = surf.detectAndCompute(img,None)

>>> len(kp)
699

1199個(gè)關(guān)鍵點(diǎn)太多,我們減少到50好畫他們,當(dāng)匹配時(shí)我們需要所有的特征,所以我們提高Hessian閾值。

# Check present Hessian threshold
>>> print surf.hessianThreshold
400.0

# We set it to some 50000. Remember, it is just for representing in picture.
# In actual cases, it is better to have a value 300-500
>>> surf.hessianThreshold = 50000

# Again compute keypoints and check its number.
>>> kp, des = surf.detectAndCompute(img,None)

>>> print len(kp)
47

這比50少了,我們畫出它來

>>>img2=cv2.drawKeypoints(img,kp,None,(255,0,0),4)

>>>plt.imshow(img2),plt.show()

現(xiàn)在使用U-SURF.

# Check upright flag, if it False, set it to True
>>> print surf.upright
False

>>> surf.upright = True

# Recompute the feature points and draw it
>>> kp = surf.detect(img,None)
>>> img2 = cv2.drawKeypoints(img,kp,None,(255,0,0),4)

>>> plt.imshow(img2),plt.show()

看下面的結(jié)果,所有的方向都一樣了,這比前面的更快,如果你遇到的是不太關(guān)心方向的,這就更好。

最后我們檢查描述的大小,如果是64維的話就把它變成128。

# Find size of descriptor
>>> print surf.descriptorSize()
64

# That means flag, "extended" is False.
>>> surf.extended
False

# So we make it to True to get 128-dim descriptors.
>>> surf.extended = True
>>> kp, des = surf.detectAndCompute(img,None)
>>> print surf.descriptorSize()
128
>>> print des.shape
(47, 128)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容