OpenCV学习笔记-SURF

一、原理
参考两位大神的博客:

原理解释: SURF原理解释
数学公式: SURF数学公式

二、函数及代码
因为版本改动的原因,所以一些书上的代码都报错了,这个是能运行的代码。

首先我们先初始化SURF函数,并可以直接指定hessianthreshold的值,通过surf.getHessianThreshold()可以查看 HessianThreshold的值。我们也可以通过surf.setHessianThreshold(50000)更改 HessianThreshold的值。

SURF函数默认的描述符的维度大小是64维,我们通过 surf.setExtended()可以查看,如果是False就是64维,反之是128维,通过函数surf.setExtended(True)设置。

我们也可以使用U-SURF,它不会检测关键点的方向,运算速度会快很多。默认为Flase,通过surf.getUpright()查看,通过surf.setUpright(True)设置。

具体代码
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt


img = cv.imread('img/butterfly.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

#可以在初始化的时候直接给hessian threshold
surf = cv.xfeatures2d_SURF.create(400)
print(surf.getHessianThreshold())

#给hessianthreshold赋值
surf.setHessianThreshold(50000)

print(surf.getExtended())

#设置extended为True,返回128维描述符
surf.setExtended(True)

kp, des = surf.detectAndCompute(gray, None)
print(des.shape)

print(surf.getUpright())
#设置upright为Ture,就变成了U-SURF,它不会检测关键点的方向,速度会提升很多
surf.setUpright(True)
print(surf.getUpright())
kp_img = cv.drawKeypoints(gray, kp, None, (255, 0, 0), 4)
cv.imshow('kp image', kp_img)

cv.namedWindow('img',cv.WINDOW_AUTOSIZE)
cv.imshow('img',img)
cv.waitKey(0)
cv.destroyAllWindows()
效果:



 
  
 
  
 
 

猜你喜欢

转载自blog.csdn.net/qq_36387683/article/details/80559890