0

I'm trying to create Bag-Of-Words so I can train the SVM later. I'm new with OpenCV so I used a code that I found on the Internet. The problem is that I have the following error:

OpenCV Error: Assertion failed (!_descriptors.empty()) in add, file /build/buildd/opencv-2.4.8+dfsg1/modules/features2d/src/bagofwords.cpp, line 57
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.4.8+dfsg1/modules/features2d/src/bagofwords.cpp:57: error: (-215) !_descriptors.empty() in function add

Aborted (core dumped)

And here is the code:

#include <stdio.h>
#include <stdlib.h>

#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
#include <string>

#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>


using namespace cv;
using namespace std;

int main(int argc, char** argv)
{

    Ptr<FeatureDetector> features = FeatureDetector::create("FAST");
    Ptr<DescriptorExtractor> descriptors = DescriptorExtractor::create("FAST");
    Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");

    //defining terms for bowkmeans trainer
    TermCriteria tc(TermCriteria::MAX_ITER + TermCriteria::EPS, 10, 0.001);
    int dictionarySize = 1000;
    int retries = 1;
    int flags = KMEANS_PP_CENTERS;

    BOWKMeansTrainer bow_trainer(dictionarySize, tc, retries, flags);
    BOWImgDescriptorExtractor bowDE(descriptors, matcher);

    //training data now
    Mat mat_features1, mat_features2;

    Mat img = imread("../positive_images/2014-12-07 19-55-07 804ea51d (1).JPG", 0);
    Mat img2 = imread("../positive_images/Pictures23.bmp_0000_0342_0104_0564_0543.png", 0);

    vector<KeyPoint> keypoints, keypoints2;

    features->detect(img, keypoints);
    features->detect(img2,keypoints2);

    descriptors->compute(img, keypoints, mat_features1);
    descriptors->compute(img2, keypoints2, mat_features2);

    bow_trainer.add(mat_features1);
    bow_trainer.add(mat_features2);

    Mat dictionary = bow_trainer.cluster();
    bowDE.setVocabulary(dictionary);

    return 0;
}

And here is how the code is compiled:

g++ BOW_creator.cpp -o BOW_creator `pkg-config --cflags --libs opencv`

Do you know what the problem could be? Can you tell me how to fix it? Let me know if you need me to provide more information.

dephinera
  • 3,703
  • 11
  • 41
  • 75

0 Answers0