OpenCV BoW assert error while computing histograms for SVM training

asked 2016-04-04 01:26:55 -0600

NathanSn gravatar image

updated 2016-04-04 01:34:23 -0600

I am trying to do classification of images with combining SIFT features, Bag of Visual Words and SVM. Now I am on training part. I need to get BoW histograms for each training image to be able to train SVM. For this I am using BOWImgDescriptorExtractor from OpenCV. I am using OpenCV version 3.1.0. The problem is that it computes histogram for some images, but for some of images it gives me this error:

OpenCV Error: Assertion failed (queryIdx == (int)i) in compute, 
file /Users/opencv-3.1.0/modules/features2d/src/bagofwords.cpp, line 200

libc++abi.dylib: terminating with uncaught exception of type 
cv::Exception: /Users/opencv-3.1.0/modules/feature/src/bagofwords.cpp:200: error: (-215) queryIdx == (int)i in function compute

Training images are all of the same size, all have same number of channels. For creating dictionary I use another image set than for training SVM.

Here's part of code:

Ptr<FeatureDetector> detector(cv::xfeatures2d::SIFT::create());
Ptr<DescriptorMatcher> matcher(new BFMatcher(NORM_L2, true));
BOWImgDescriptorExtractor bow_descr(det, matcher);
Mat features_svm;
for (int i = 0; i < num_svm_data; ++i) {
    Mat hist;
    std::vector<KeyPoint> keypoints;
    detector->detect(data_svm[i], keypoints);
    bow_descr.compute(data_svm[i], keypoints, hist);

data_svm is a vector<Mat> type. It is my training set images which I will use in SVM. I have draw keypoints, print hist for images they looks good. I tried changing from SIFT to SURF it works for some images for which SIFT does not, but it also does not work for all. If I train dictionary on all images I also receive the same problem.

When I change size of the BoW dictionary form K = 200 to K = 1000 number of images which raise error decreases.

What the problem can be?

edit retag flag offensive close merge delete


If I do not use cross check option in BFMatcher then everything works fine for any value of K. Can someone explain what means this cross check option? I thought that for each keypoint it gives exactly one codeword from dictionary.

NathanSn gravatar imageNathanSn ( 2016-04-04 03:06:41 -0600 )edit