Ask Your Question

Revision history [back]

BoW in openCV: cluster() takes far too long.

I'm trying to build a Classifier App (based on: http://www.morethantechnical.com/2011/08/25/a-simple-object-classifier-with-bag-of-words-using-opencv-2-3-w-code/) using BOW in openCV. After Loading all the training images and extracting all the descriptors I'm trying to form a "vocabulary" but when I use the function Cluster, response time is extremly LONG. Infact I can leave it to run for several hours. Any ideas on why this is happening? Here is a sample of code:

Classifier::Classifier(int clusters, SImage* img){

_clusters = clusters;   
_featureDetector = new SurfFeatureDetector(400);
_descriptorExtractor = (
    new OpponentColorDescriptorExtractor(
        Ptr<DescriptorExtractor>(new SurfDescriptorExtractor())
        )
    );
_descriptorMatcher = DescriptorMatcher::create("BruteForce-Hamming");

_bowDescriptorExtractor = new BOWImgDescriptorExtractor(_descriptorExtractor,_descriptorMatcher);
_bowtrainer = new BOWKMeansTrainer(100);


// building vocabulary and orginaizing training set
_vocab = build_vocab();

_bowDescriptorExtractor->setVocabulary(_vocab);

cout << "Initialized" << endl;

}


Mat Classifier::build_vocab() {

Mat training_descriptors(1, _descriptorExtractor->descriptorSize(), _descriptorExtractor->descriptorType());

for (images in training folder...) {

            // getting training Mat

            Mat im, desc;
            im = imread(filename);

            cout << "extract descriptors.." << endl;

            vector<KeyPoint> kp;

            _featureDetector->detect(im, kp);

            _descriptorExtractor->compute(im, kp, desc);

            training_descriptors.push_back(desc);
        }
    }

    cout << "Total decriptors: " << training_descriptors.rows << endl;

    _bowtrainer->add(training_descriptors);
    cout << "cluster BOW features" << endl;

    Mat vocabulary = _bowtrainer->cluster(); // **<---- Here is where the program halts.**


    return vocabulary;
}

This is obviously very generic and simple, still i cannot seem to use this function, maybe I'm using the procedure wrong?.

Regards