如何使用词袋模型进行训练和预测?

14

我有一份汽车各个角度的图像文件夹。我想使用词袋方法来训练系统识别这辆车。训练完成后,如果给出该汽车的图像,它应该能够识别它。

我一直在尝试学习OpenCV中的BOW函数,以使这项工作顺利进行,并已经达到了一个水平,现在不知道该怎么做,需要一些指导。

这是我用来制作词袋的代码:

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

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

    BOWImgDescriptorExtractor bowDE(descriptors, matcher);

    //training data now
    Mat features;
    Mat img = imread("c:\\1.jpg", 0);
    Mat img2 = imread("c:\\2.jpg", 0);
    vector<KeyPoint> keypoints, keypoints2;
    features->detect(img, keypoints);
    features->detect(img2,keypoints2);
    descriptor->compute(img, keypoints, features);
    Mat features2;
    descripto->compute(img2, keypoints2, features2);
    bowTrainer.add(features);
    bowTrainer.add(features2);

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

这一切都基于BOW文档

我认为在这个阶段我的系统已经被训练好了,下一步就是预测。

这就是我不知道该怎么做的地方。如果我使用SVM或者NormalBayesClassifier,它们都会用到"train"和"predict"这些术语。

我该如何进行预测和训练呢?非常感谢任何指导。我应该如何将分类器的训练与我的`bowDE`函数连接起来?


1
你是如何决定 TermCriteria 和 dictionarySize 的值的?tc、retries、flags 又是怎么确定的呢? - dephinera
1个回答

15

您的下一步是提取实际的单词袋描述符。您可以使用BOWImgDescriptorExtractor中的 compute 函数来完成此操作。类似以下内容:

 bowDE.compute(img, keypoints, bow_descriptor);

使用这个函数,您可以创建描述符,然后将它们收集到矩阵中,该矩阵用作分类器函数的输入。也许这篇教程可以为您提供一点帮助。

我想要提醒的另一件事是,对于分类通常需要至少两个类别。因此,您还需要一些不包含汽车的图像来训练分类器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接