Ask Your Question

Revision history [back]

Error using Flann matcher for BOWImgDescriptorExtractor in opencv-python

I am using opencv3 and python to implement the bag of words model for images. Here is my code.

import numpy as np
import cv2
import glob

img_cls = '/home/user/class_path'
imgs = glob.glob(img_cls + '*jpg')

sift = cv2.xfeatures2d.SIFT_create()

clusterCount = 200
termcrit = cv2.TermCriteria_EPS + cv2.TermCriteria_MAX_ITER, 10, 0.001
flags = cv2.KMEANS_RANDOM_CENTERS
attempts = 10

bowTrainer = cv2.BOWKMeansTrainer(clusterCount, termcrit, attempts, flags)

for i in imgs:
    img = cv2.imread(i, 0)
    kp  = sift.detect(img)
    kp, des = sift.compute(img, kp)
    bowTrainer.add(des)

voc = bowTrainer.cluster()

FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
matcher = cv2.FlannBasedMatcher(index_params, search_params)

bowDE = cv2.BOWImgDescriptorExtractor(sift, matcher)
bowDE.setVocabulary(voc)

img = cv2.imread('/home/user/test_img.jpg', 0)
kp = sift.detect(img)
bdes = bowDE.compute(img, kp)

After running the program, I get the following error. Could anyone point out what I might be doing wrong.

OpenCV Error: Assertion failed (The data should normally be NULL!) in allocate, file /home/user/opencv/modules/python/src2/cv2.cpp, line 163
Traceback (most recent call last):
   File "ImgBow.py", line 31, in <module>
     bowDE.setVocabulary(voc)
cv2.error: /home/user/opencv/modules/python/src2/cv2.cpp:163: error: (-215) The data should normally be NULL! in function allocate