0
votes

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);
bow_descr.setVocabulary(dict);
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);
    features_svm.push_back(hist);
}

data_svm is a vector<Mat> type. It is my training set images which I will use in SVM.

What the problem can be?

1
Print the values of data[i], keypoints and hist before you call the compute method. Anything odd about them? - Dan Mašek
@DanM, I was doing this. Actually, I am showing keypoints on image with drawKeypoints() function, hist for images for which program gives me result looks good, keypoints for all images looks good, and data[i] which is one image also looks good. - York's
@DanM, hist before calling compute method is empty. - York's
Ya, that one's supposed to be the output, so that's OK. Look at the source: sources\modules\features2d\src\bagofwords.cpp starting with line 143. First it computes descriptors using SIFT, then finds matches with BFMatcher. Processing of those matches then causes an error.Maybe there keypoints are not good enough for this picture? Try calculating those yourself and inspecting the results (or break inside the opencv code). Maybe try tweaking the parameters of the detector or matcher, or try different algorithms? - Dan Mašek
@DanM, what do you mean by "keypoints are not good enough for this picture"? I have looked at source code already, but I do not understand meaning of matches[i].queryIdx, matches[i].trainIdx. Can you explain them to me? - York's

1 Answers

0
votes

I ran into the same issue. I was able to fix it by changing the cross check option of the Brute Force matcher to False. I think its because the cross check option keeps only a select few matches and removes the rest and messes up the indexes in the process