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
i can reproduce it (ocv3.1, py2.7), and i think, it's a bug (not an error on your side).
@berak I have built opencv by cloning from the git repository and am also using py2.7. Could you tell me how you set up your ocv.
again, i do not think, you're doing something wrong (or that a rebuild would fix it)
here's a simplified version, same error:
@berak is there something else that i can do to get my bag of words descriptor..is the alternative brute force matcher as good as the flann
getting the bag of words is not the problem here, but setting it later to the BOWImgDescriptorExtractor, right ?
imho, the best way is to make an official issue and have someone fix it (or try on your own)
@berak thanks for your help :)
https://github.com/Itseez/opencv/issu...