在openCV中使用BoW时出错

时间:2015-04-15 14:13:17

标签: c++ opencv computer-vision

我试图在openCV中使用BOW构建一个分类器应用程序(基于:http://www.morethantechnical.com/2011/08/25/a-simple-object-classifier-with-bag-of-words-using-opencv-2-3-w-code/)。 在加载所有训练图像并提取所有描述符之后,我试图形成一个"词汇表"但是当我使用Cluster功能时,响应时间极其 LONG 。事实上,我可以让它运行几个小时。有关为什么会发生这种情况的任何想法? 以下是代码示例:

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
            //cout << category << '\n';

            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;
}

这显然非常通用和简单,但我似乎无法使用此功能,也许我使用的程序错了?。

问候。

0 个答案:

没有答案