一. SURF基本原理
SURF是SIFT的加速版,它善于处理具有模糊和旋转的图像,但是不善于处理视角变化和光照变化。在SIFT中使用DoG对LoG进行近似,而在SURF中使用盒子滤波器对LoG进行近似,这样就可以使用积分图像了(计算图像中某个窗口内所有像素和时,计算量的大小与窗口大小无关)。总之,SURF最大的特点在于采用了Haar特征以及积分图像的概念,大大加快了程序的运行效率。
二. 构建Hessian矩阵
SIFT算子构建一幅图像金字塔,在每一层进行高斯滤波并求取图像差(DoG)进行特征点的提取,而SURF算子用Hessian矩阵进行特征点的提取。
三. 构建尺度空间
四. 精确定位特征点
为了在图像不同尺寸中定位特征点,使用3×3×3邻域非最大值抑制,即每个像素点与其三维邻域中的26个点进行大小
比较。局部极大值精确定位采用3维线性插值法得到亚像素级的特征点,同时也去掉小于一定阈值的点。
五. 特征点主方向确定
六. 特征点描述子生成
七. OpenCV中的SURF算子
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('fly.png',0)
# here set Hessian Threshold to 400
surf = cv2.SURF(400)
# Find keypoints and descriptors directly
kp, des = surf.detectAndCompute(img,None)
#print len(kp) #699
# check present Hessian threshold
# print surf.hessianThreshold #400.0
# here set it to some 50000
surf.hessianThreshold = 50000
# again compute keypoints and check its number.
kp, des = surf.detectAndCompute(img,None)
#print len(kp) #47
img2 = cv2.drawKeypoints(img,kp,None,(255,0,0),4)
plt.imshow(img2)
plt.show()
# 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()
# find size of descriptor
print surf.descriptorSize() #64
# that means flag, "extended" is False.
print surf.extended #False
# so we make it to True to get 128-dim descriptors.
surf.extended = True
kp, des = surf.detectAndCompute(img,None)
#print print surf.descriptorSize() #128
#print print des.shape #(47, 128)
结果输出,如下所示:
cv2.SURF函数原型cv2.SURF([hessianThreshold[, nOctaves[, nOctaveLayers[, extended[, upright]]]]]) →
<SURF object>,如下所示:
(1)hessianThreshold:Threshold for hessian keypoint detector used in SURF.
(2)nOctaves:Number of pyramid octaves the keypoint detector will use.
(3)nOctaveLayers:Number of octave layers within each octave.
(4)extended:Extended descriptor flag (true use 128-element descriptors; false use 64-element descriptors).
(5)upright:Up-right or rotated features flag (true don't compute orientation; false compute orientation).