本文介绍了OpenCV 2.4.1 - 在Python中计算SURF描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新我的代码使用 cv2.SURF()而不是 cv2.FeatureDetector_create(SURF) cv2.DescriptorExtractor_create(SURF)。但是,我在检测到关键点后无法获取描述符。调用 SURF.detect

的正确方法是什么?我尝试按照OpenCV文档,但我是一个小混乱。这是它在文档中说的。

  Python:cv2.SURF.detect(img,mask)→keypoints¶
Python:cv2.SURF.detect (img,mask [,descriptors [,useProvidedKeypoints]])→关键点,描述符

第二次调用 SURF.detect

解决方案

我不知道我是否正确地理解你的问题。但是如果你正在寻找一个匹配的SURF关键点的示例,下面是一个非常简单和基本的类似于模板匹配:

  import cv2 
import numpy as np

#加载图片
img = cv2.imread('messi4.jpg')

#将它们转换为灰度
imgg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#SURF提取
surf = cv2.SURF()
kp,descritors = surf.detect(imgg,None,useProvidedKeypoints = False)

#为kNN设置样本和响应
samples = np.array(descritors)
responses = np.arange len(kp),dtype = np.float32)

#kNN training
knn = cv2.KNearest()
knn.train(samples,responses)

#现在加载模板图像并搜索类似的关键点
template = cv2.imread('template.jpg')
templateg = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY)
键,desc = surf.detect(templateg,None,useProvidedKeypoints = False)

为h,des in枚举(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 matching keypoints in red color
color =(0,0,255)
else:#draw undazched in blue color
print dist
color =(255,0,0)

#在原始图像上绘制匹配的关键点
x,y = kp [res] .pt
center = (int(x),int(y))
cv2.circle(img,center,2,color,-1)

#在模板图片上绘制匹配的关键点
x ,y = keys [h] .pt
center =(int(x),int(y))
cv2.circle(template,center,2,color,-1)
b $ b cv2.imshow('img',img)
cv2.imshow('tm',template)
cv2.waitKey(0)
cv2.destroyAllWindows

以下是我得到的结果(使用颜色在原始图片上复制粘贴的模板图片):



>



>



可以看到,有一些小错误。但对于一个创业公司来说,希望一切顺利。


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?

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

How do I pass the keypoints in when making the second call to SURF.detect?

解决方案

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描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:59
查看更多