opencv python SURF

Introduction to SURF (Speeded-Up Robust Features)

理论

在上一章中,我们看到了SIFT的关键点检测和描述,但它相对较慢,人们需要更加快速的版本,所以2006年引入了一种名为SURF的新算法, 顾名思义,它是SIFT的加速版本.

作为尺度不变特征变换(SIFT)算法的加速版,SURF算法在适中的条件下完成两幅图像中物体的匹配基本实现了实时处理,其快速的基础实际上只有一个——积分图像haar求导.

SURF算法原理:

  1. 构建Hessian矩阵构造高斯金字塔尺度空间
  2. 利用非极大值抑制初步确定特征点
  3. 精确定位极值点
  4. 选取特征点的主方向
  5. 构造surf特征点描述算子

OpenCV中的SURF

import numpy as np
import cv2

img = cv2.imread('img.jpg')

# Create SURF object. You can specify params here or later.
# Here I set Hessian Threshold to 400
surf = cv2.xfeatures2d.SURF_create(400)
# Find keypoints and descriptors directly
kp, des = surf.detectAndCompute(img,None)
print(len(kp))

output:3477

# Check present Hessian threshold
print( surf.getHessianThreshold() )

output: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.setHessianThreshold(50000)
#Again compute keypoints and check its number.
kp, des = surf.detectAndCompute(img,None)
print( len(kp) )

output:2

img2 = cv2.drawKeypoints(img,kp,None,(255,0,0),4)
plt.imshow(img2),plt.show()

opencv python SURF

# Check upright flag, if it False, set it to True
print( surf.getUpright() )

output:False

surf.setUpright(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()

opencv python SURF

比之前更快了

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

output: 64

# That means flag, "extended" is False.
surf.getExtended()

output: False

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

output:
128
(2, 128)

相关推荐