BFMatcher match in OpenCV throwing error
<p>I am using SURF descriptors for image matching. I am planning to match a given image to a database of images.</p>

<pre><code>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)
</code></pre>

<p>The issue is with <code>bf.match</code> is that I am getting the following error:</p>

<pre><code>OpenCV Error: Assertion failed (type == src2.type() &amp;&amp; src1.cols == src2.cols &amp;&amp; (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 &lt;module&gt;
    bf.match(des2)
cv2.error: /build/opencv/src/opencv-3.1.0/modules/core/src/stat.cpp:3749: error: (-215) type == src2.type() &amp;&amp; src1.cols == src2.cols &amp;&amp; (type == CV_32F || type == CV_8U) in function batchDistance
</code></pre>

<p>The error is similar to <a href="http://stackoverflow.com/questions/24224846/opencv-bfmatcher-match-always-return-error">this</a> 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 <code>NORM_HAMMING</code> distance. The error resurfaces.
Any help will be appreciated.</p>

<p>The two images that I have used for this are:</p>

<p><a href="http://i.stack.imgur.com/Er7vx.png" rel="nofollow"><img src="http://i.stack.imgur.com/Er7vx.png" alt="box.png"></a></p>

<p>box.png </p>

<p><a href="http://i.stack.imgur.com/gAz96.png" rel="nofollow"><img src="http://i.stack.imgur.com/gAz96.png" alt="box_in_scene.png"></a></p>

<p>box_in_scene.png</p>

<p>I am using Python 3.5.2 and OpenCV 3.1.x in linux.</p>

<p><strong>To search between descriptors of two images</strong> use:</p>

<pre><code>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)
</code></pre>

<p><strong>To search among multiple images</strong></p>

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

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

<p><strong>Code Sample for Multiple Image search</strong></p>

<pre><code>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
</code></pre>

<p>For DMatch output of bf.match, see <a href="http://docs.opencv.org/2.4/modules/features2d/doc/common_interfaces_of_descriptor_matchers.html#dmatch" rel="nofollow">docs</a>.</p>

<p>See full example for this here: <a href="http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html" rel="nofollow">Opencv3.0 docs</a>.</p>

<p><strong>Other Info</strong></p>

<p>OS: Mac.<br>
Python: 2.7.10.<br>
Opencv: 3.0.0-dev [If remember correctly, installed using brew].  </p>

python opencv surf orb