BFMatcher匹配OpenCV中的抛出错误 [英] BFMatcher match in OpenCV throwing error

查看:453
本文介绍了BFMatcher匹配OpenCV中的抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SURF描述符进行图像匹配.我正计划将给定的图像与图像数据库匹配.

I am using SURF descriptors for image matching. I am planning to match a given image to a database of images.

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的问题是出现以下错误:

The issue is with bf.match is that I am getting the following error:

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

该错误类似于帖子.给出的说明不完整且不充分.我想知道如何解决此问题.我已经将ORB描述符与具有NORM_HAMMING距离的BFMatcher一起使用.错误再次浮出水面. 任何帮助将不胜感激.

The error is similar to this post. The explanation given is incomplete and inadequate.I want to know how to resolve this issue. I have used ORB descriptors as well with BFMatcher having NORM_HAMMING distance. The error resurfaces. Any help will be appreciated.

我为此使用的两个图像是:

The two images that I have used for this are:

box.png

box_in_scene.png

box_in_scene.png

我正在Linux中使用Python 3.5.2和OpenCV 3.1.x.

I am using Python 3.5.2 and OpenCV 3.1.x in linux.

推荐答案

要在两个图像的描述符之间进行搜索,请使用:

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维矢量.

The add method is used to add descriptor of multiple test images. Once, all descriptors are indexed, you run train method to build an underlying Data Structure(example: KdTree which will be used for searching in case of FlannBasedMatcher). You can then run match to find if which test image is a closer match to which query image. You can check K-d_tree and see how it can be used to search for multidimensional vectors(Surf gives 64-dimensional vector).

注意:-顾名思义,BruteForceMatcher没有内部搜索优化数据结构,因此具有空的训练方法.

Note:- BruteForceMatcher, as name implies, has no internal search optimizing data structure and thus has empty train method.

用于多张图片搜索的代码示例

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

有关bf.match的DMatch输出,请参见文档.

For DMatch output of bf.match, see docs.

在此处查看完整示例: Opencv3.0文档.

See full example for this here: Opencv3.0 docs.

其他信息

操作系统:Mac.
Python:2.7.10.
Opencv:3.0.0-dev [如果没记错,请使用brew安装.

OS: Mac.
Python: 2.7.10.
Opencv: 3.0.0-dev [If remember correctly, installed using brew].

这篇关于BFMatcher匹配OpenCV中的抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆