Frank Zickert Frank Zickert - 1 month ago 19x
Python Question

cv2 3.0.0 cv2.FlannBasedMatcher: flann.knnMatch is throwing cv2 error

I want to use a flann-based matcher in Python as described in

Since I am using OpenCV 3.0.0, I had to adjust the code regarding the initialization of the SIFT detector. The rest is taken without changes

def calculateMatch(self):
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
(kp1, desc1) = sift.detectAndCompute(self.image1,None)
(kp2, desc2) = sift.detectAndCompute(self.image2,None)

# FLANN parameters

index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(desc1,desc2,k=2)

When I run the code, I get the following message:

cv2.error: cv2.cpp:161: error: (-215) The data should normally be NULL! in function allocate

in the line:

matches = flann.knnMatch(desc1,desc2,k=2)

One more comment:
If I use brute force matcher, everything works fine:

bf = cv2.BFMatcher()
matches = bf.knnMatch(desc1, desc2, k=2)


I got same problem on my computer. so, I maked a new virtual-machine with Ubuntu 14.04 and tested.

I don't know why.... but, I got this problem solved with OpenCV 3.0.0-rc1.

If you are using OpenCV3.0.0, then try another release of OpenCV3

For installation , I was referring to the following page. On this page, Opencv's version is alpha. To read as in the example below.

wget -O