C ++ / OpenCV:如何使用BOWImgDescriptorExtractor来确定哪些聚类与词汇表中的哪些图像相关?

法唑

我的目标是将图像作为查询并在图像库中找到最匹配的图像。我正在openCV 3.0.0中使用SURF功能,并使用“语言袋”方法来找到匹配项。我需要一种方法来查找查询图像在库中是否有匹配项。如果是这样,我想知道最接近的图像索引。

这是我的代码,用于读取所有图像(图像库中总共300个)并提取和聚类特征:

Mat training_descriptors(1, extractor->descriptorSize(), extractor->descriptorType());
//read in all images and set to binary
char filepath[1000];
for (int i = 1; i < trainingSetSize; i++){
    cout << "in for loop, iteration: " << i << endl;
    _snprintf_s(filepath, 100, "C:/Users/Randal/Desktop/TestCase1Training/%d.bmp", i);
    Mat temp = imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);
    Mat tempBW;
    adaptiveThreshold(temp, tempBW, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 2);
    detector->detect(tempBW, keypoints1);
    extractor->compute(tempBW, keypoints1, descriptors1);
    training_descriptors.push_back(descriptors1);
    cout << "descriptors added" << endl;

}
cout << "Total descriptors: " << training_descriptors.rows << endl;
trainer.add(training_descriptors);

Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
BOWImgDescriptorExtractor BOW(extractor, matcher);
Mat library = trainer.cluster();
BOW.setVocabulary(library);

我编写了以下代码,以查找匹配项。问题在于BOW.compute仅返回图像和图像库中都存在的簇(单词)的索引。imgQ是查询图像。

Mat output;
Mat imgQBW;
adaptiveThreshold(imgQ, imgQBW, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 2);
imshow("query image", imgQBW);
detector->detect(imgQBW, keypoints2);
extractor->compute(imgQBW, keypoints2, descriptors2);

BOW.compute(imgQBW, keypoints1, output);
cout << output.row(0) << endl;

我需要知道BoW中的哪些群集对应于哪些图像。我现在的输出-output.row(0)-只是一个包含库中所有簇索引的数组。我是否误解了此输出?有没有办法确定哪个图像具有最匹配的群集?

安德烈·托德(Andrei Toader)

我也基于此代码做了类似的事情:

https://github.com/royshil/FoodcamClassifier/blob/master/training_common.cpp

但是以上部分是在聚类完成之后。您要做的是使用ML(我使用SVM)和群集中心进行训练,群集中心是您所拥有的视觉单词。此外,您需要找到所有与聚类点最接近的点,并使用直方图对其进行训练。接下来,您将有一个需要训练的频率直方图(关键点包)。

Ptr<ifstream> ifs(new ifstream("training.txt"));
int total_samples_in_file = 0;
vector<string> classes_names;
vector<string> lines; 

//read from the file - ifs and put into a vector
for(int i=0;i<lines.size();i++) {

    vector<KeyPoint> keypoints;
    Mat response_hist;
    Mat img;
    string filepath;

    string line(lines[i]);
    istringstream iss(line);

    iss >> filepath;

    string class_to_train; 
    iss >> class_to_train; 
    class_ml = "class_" + class_to_train;
    if(class_ml.size() == 0) continue;

    img = imread(filepath);

    detector->detect(img,keypoints);
    bowide.compute(img, keypoints, response_hist);

    cout << "."; cout.flush();
    //here create the logic for the class to train(class_0, e.g) and the data you need to train.
}

您可以在此git项目中找到更多信息:
https : //github.com/royshil/FoodcamClassifier
这里的文档:http :
//www.morethantechnical.com/2011/08/25/a-simple-object-classifier-with-bag使用opencv-2-3-w代码的单词/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我应该使用哪些类条目来引发C扩展中的非默认异常?

如何使用C#确定本地网络上的哪些IP地址是静态/动态的?

如何使用vlfeat和opencv为C ++中的图像确定PHOW功能?

如何遍历Python中的词汇表?

如何确定Java程序使用哪些类?

目标C如何确定在堆与堆栈上分配哪些C数据结构?

如何在C ++中检查向量中的哪些对象

使用逻辑AND(&&)时,如何以编程方式查看C ++中不满足哪些条件?

C ++链接器如何知道要使用哪些.cpp文件

我如何知道在 C++ 中使用哪些函数?

SIFT聚类将筛选特征(128维矢量)转换为词汇表

如何判断哪些参数是必需的,哪些不是?(Visual C ++)

如何确定从外部 didSelectRowAtIndexPath (Objective-C) 在 UITableView 中选择了哪些行

确定在C中使用Makefile时将运行哪些命令?

有没有一种简单的方法来判断哪些 C++ 类是从共享对象库中抽象出来的?

有没有一种简单的方法来判断哪些 C++ 类是从共享对象库中抽象出来的?

我可以使用哪些库来突出 C# 中流行的语言的语法?

哪些代码在 C 中执行得更快?

使用su -c时,哪些文件来源?

如何创建一个对象来存储从词汇表中的单词到其索引的映射?

向量C ++中存储的聚类点

R中的模糊C均值聚类

STream类的哪些子类支持在C#中查找(不在文档ffs中)

如何确定Word文档中的哪些图像异常大?

使用OpenCV在C ++中图像的SNR

使用opencv在c ++中修改图像

Lua C API如何确定将函数称为类成员还是仅从表中调用函数?

操作系统提供哪些服务来执行C程序

C ++如何知道模板类在编译时支持哪些方法?