OpenCV 2.4.1 - 在 Python 中计算 SURF 描述符 [英] OpenCV 2.4.1 - computing SURF descriptors in Python
问题描述
我正在尝试更新我的代码以使用 cv2.SURF()
而不是 cv2.FeatureDetector_create("SURF")
和 cv2.DescriptorExtractor_create(冲浪")
.但是,在检测到关键点后,我无法获取描述符.调用 SURF.detect
的正确方法是什么?
I'm trying to update my code to use cv2.SURF()
as opposed to cv2.FeatureDetector_create("SURF")
and cv2.DescriptorExtractor_create("SURF")
. However I'm having trouble getting the descriptors after detecting the keypoints. What's the correct way to call SURF.detect
?
我尝试遵循 OpenCV 文档,但我有点困惑.这是它在文档中所说的.
I tried following the OpenCV documentation, but I'm a little confused. This is what it says in the documentation.
Python: cv2.SURF.detect(img, mask) → keypoints¶
Python: cv2.SURF.detect(img, mask[, descriptors[, useProvidedKeypoints]]) → keypoints, descriptors
如何在第二次调用 SURF.detect
时传入关键点?
How do I pass the keypoints in when making the second call to SURF.detect
?
推荐答案
我不确定我是否正确理解了您的问题.但是如果你正在寻找一个匹配 SURF 关键点的样本,下面是一个非常简单和基本的,类似于模板匹配:
I am not sure whether i understand your questions correctly. But if you are looking for a sample of matching SURF keypoints, a very simple and basic one is below, which is similar to template matching:
import cv2
import numpy as np
# Load the images
img =cv2.imread('messi4.jpg')
# Convert them to grayscale
imgg =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# SURF extraction
surf = cv2.SURF()
kp, descritors = surf.detect(imgg,None,useProvidedKeypoints = False)
# Setting up samples and responses for kNN
samples = np.array(descritors)
responses = np.arange(len(kp),dtype = np.float32)
# kNN training
knn = cv2.KNearest()
knn.train(samples,responses)
# Now loading a template image and searching for similar keypoints
template = cv2.imread('template.jpg')
templateg= cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)
keys,desc = surf.detect(templateg,None,useProvidedKeypoints = False)
for h,des in enumerate(desc):
des = np.array(des,np.float32).reshape((1,128))
retval, results, neigh_resp, dists = knn.find_nearest(des,1)
res,dist = int(results[0][0]),dists[0][0]
if dist<0.1: # draw matched keypoints in red color
color = (0,0,255)
else: # draw unmatched in blue color
print dist
color = (255,0,0)
#Draw matched key points on original image
x,y = kp[res].pt
center = (int(x),int(y))
cv2.circle(img,center,2,color,-1)
#Draw matched key points on template image
x,y = keys[h].pt
center = (int(x),int(y))
cv2.circle(template,center,2,color,-1)
cv2.imshow('img',img)
cv2.imshow('tm',template)
cv2.waitKey(0)
cv2.destroyAllWindows()
以下是我得到的结果(使用油漆将粘贴的模板图像复制到原始图像上):
Below are the results I got (copy pasted template image on original image using paint):
如您所见,存在一些小错误.但对于初创公司,希望一切顺利.
As you can see, there are some small mistakes. But for a startup, hope it is OK.
这篇关于OpenCV 2.4.1 - 在 Python 中计算 SURF 描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!