OpenCV3 Flann knnMatch-数据通常应为NULL!在功能分配

时间:2019-02-15 17:44:04

标签: python opencv

我正在关注this OpenCV tutorial on feature matching

尤其是我的代码如下:

# Example matching features between images

img1 = cv2.imread('img1.jpg',0)          # queryImage
img2 = cv2.imread('img2.jpg',0) # trainImage

# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# FLANN parameters
FLANN_INDEX_KDTREE = 0
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(des1,des2,k=2)

# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in range(len(matches))]

# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
    if m.distance < 0.7*n.distance:
        matchesMask[i]=[1,0]

draw_params = dict(matchColor = (0,255,0),
                   singlePointColor = (255,0,0),
                   matchesMask = matchesMask,
                   flags = 0)

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)

plt.imshow(img3,),plt.show()

执行此代码段时,出现此错误:

---------------------------------------------------------------------------

error                                     Traceback (most recent call last)

<ipython-input-25-51bee536a566> in <module>()
     17 flann = cv2.FlannBasedMatcher(index_params,search_params)
     18 
---> 19 matches = flann.knnMatch(des1,des2,k=2)
     20 
     21 # Need to draw only good matches, so create a mask

error: /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/python/src2/cv2.cpp:163: error: (-215) The data should normally be NULL! in function allocate

我不确定应该如何解释该错误或如何解决该错误。有帮助吗?

1 个答案:

答案 0 :(得分:0)

显然,这是由于OpenCV 3.1中的错误所致。我更新到3.4,它会自行修复。