Error using Flann matcher for BOWImgDescriptorExtractor in opencv-python

asked 2016-01-05 01:33:51 -0600

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
edit retag flag offensive close merge delete

Comments

i can reproduce it (ocv3.1, py2.7), and i think, it's a bug (not an error on your side).

berak gravatar imageberak ( 2016-01-05 02:09:15 -0600 )edit

@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.

Amit Raj gravatar imageAmit Raj ( 2016-01-05 02:28:25 -0600 )edit

again, i do not think, you're doing something wrong (or that a rebuild would fix it)

berak gravatar imageberak ( 2016-01-05 02:46:29 -0600 )edit

here's a simplified version, same error:

>>> sift = cv2.xfeatures2d.SIFT_create()
>>> 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)
>>> voc=np.array([[1,2,3,4],[5,6,7,8]], np.float32)
>>> voc
array([[ 1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.]], dtype=float32)
>>> bowDE.setVocabulary(voc)
OpenCV Error: Assertion failed (The data should normally be NULL!) in NumpyAlloc
ator::allocate, file ..\..\..\modules\python\src2\cv2.cpp, line 163
berak gravatar imageberak ( 2016-01-05 02:48:12 -0600 )edit

@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

Amit Raj gravatar imageAmit Raj ( 2016-01-05 02:54:09 -0600 )edit

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 gravatar imageberak ( 2016-01-05 02:57:10 -0600 )edit

@berak thanks for your help :)

Amit Raj gravatar imageAmit Raj ( 2016-01-05 03:03:38 -0600 )edit