Ask Your Question
0

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

asked 2015-04-15 10:14:19 -0600

I'm trying to build a Classifier App (based on: http://www.morethantechnical.com/2011...) 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

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-05-19 13:30:44 -0600

WhatsAGorski gravatar image

How big is your training set? K-Means is an O(n^2) algorithm, so it can take a long time on large datasets.

For reference, I trained a similar vocabulary as you with 3138432 SIFT descriptors in it and it took around 48 hours.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-04-15 10:12:56 -0600

Seen: 361 times

Last updated: Apr 15 '15