我使用冲浪描述符进行图像匹配。我计划将给定的图像与图像数据库进行匹配。
import cv2
import numpy as np
surf = cv2.xfeatures2d.SURF_create(400)
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=True)
#I am planning to add more descriptors
bf.add(des1)
bf.train()
#This is my test descriptor
bf.match(des2)
bf.match
的问题是,我得到了以下错误:
OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in batchDistance, file /build/opencv/src/opencv-3.1.0/modules/core/src/stat.cpp, line 3749
Traceback (most recent call last):
File "image_match4.py", line 16, in <module>
bf.match(des2)
cv2.error: /build/opencv/src/opencv-3.1.0/modules/core/src/stat.cpp:3749: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function batchDistance
该错误类似于这 post。我想知道如何解决这个问题。对于具有BFMatcher距离的NORM_HAMMING
,我也使用了ORB描述符。错误再次出现。任何帮助都将不胜感激。
我所使用的两幅图像是:
box.png
box_in_scene.png
我在linux中使用Python3.5.2和OpenCV 3.1.x。
发布于 2016-10-09 17:02:52
用于搜索两个图像的描述符,使用:
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
,以确定哪个测试映像与哪个查询映像是否更匹配。您可以检查树并查看如何使用它来搜索多维向量(Surf给出64维向量)。
注释:- BruteForceMatcher,顾名思义,没有内部搜索优化数据结构,因此有空列方法。
用于多图像搜索的代码示例
import cv2
import numpy as np
surf = cv2.xfeatures2d.SURF_create(400)
# Read Images
train = 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 BFMatcher
clusters = 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
有关DMatch输出的bf.match,请参见文档。
请参阅这里的完整示例:Opencv3.0博士。
其他信息
操作系统:麦克。
Python: 2.7.10。
Opencv: 3.0.0-dev如果记得正确,使用brew安装。
发布于 2018-04-10 07:45:41
我发现我也犯了同样的错误。花了一段时间才弄清楚--我的一些图像有些没有特征,因此没有找到关键点,detectAndCompute
返回了None
作为描述符。在传递到None
之前,可能值得检查BFMatcher.add()
元素的描述符列表。
发布于 2018-05-16 09:59:33
我也犯了同样的错误。但在我的例子中,这是因为我在cv2.BFMatcher_create
中使用了SIFT和cv2.BFMatcher_create
度量。将度量更改为cv2.NORM_L1
解决了这个问题。
引用BFMatcher的文档:
normType
-NORM_L1
,NORM_L2
,NORM_HAMMING
,NORM_HAMMING2
之一。L1
和L2
规范是SIFT和SURF描述符的较好选择,NORM_HAMMING
应与ORB一起使用,choices和NORM_HAMMING2
用于ORB时WTA_K==3
或4
(参见ORB::ORB
构造函数描述)。
https://stackoverflow.com/questions/39940766
复制相似问题