要在两个图像的描述符之间进行搜索,请 使用:
img1 = cv2.imread('box.png',0)img2 = cv2.imread('box_in_scene.png',0)kp1,des1 = surf.detectAndCompute(img1,None)kp2,des2 = surf.detectAndCompute(img2,None)bf = cv2.BFMatcher(cv2.NORM_L1,crossCheck=False)matches = bf.match(des1,des2)在多张图像中搜索
该
add方法用于添加多个测试图像的描述符。一旦所有描述符都建立索引,就可以运行
train方法来构建基础的数据结构(示例:KdTree,在FlannbasedMatcher的情况下将用于搜索)。然后,您可以运行
match以查找哪个测试图像与哪个查询图像更匹配。您可以检查K-d_tree并查看如何将其用于搜索多维矢量(Surf提供64维矢量)。
注意:- 顾名思义,BruteForceMatcher没有内部搜索优化数据结构,因此具有空训练方法。
用于多图像搜索的代码示例
import cv2import numpy as npsurf = cv2.xfeatures2d.SURF_create(400)# Read Imagestrain = cv2.imread('box.png',0)test = cv2.imread('box_in_scene.png',0)# Find Descriptors kp1,trainDes1 = surf.detectAndCompute(train, None)kp2,testDes2 = surf.detectAndCompute(test, None)# Create BFMatcher and add cluster of training images. One for now.bf = cv2.BFMatcher(cv2.NORM_L1,crossCheck=False) # crossCheck not supported by BFMatcherclusters = np.array([trainDes1])bf.add(clusters)# Train: Does nothing for BruteForceMatcher though.bf.train()matches = bf.match(testDes2)matches = sorted(matches, key = lambda x:x.distance)# Since, we have index of only one training image, # all matches will have imgIdx set to 0.for i in range(len(matches)): print matches[i].imgIdx有关bf.match的DMatch输出,请参阅docs。
在此处查看完整示例:Opencv3.0
docs。
其他资讯
操作系统:Mac。
的Python:2.7.10。
Opencv:3.0.0-dev [如果没记错,请使用brew安装。



