我的目标是将图像作为查询并在图像库中找到最匹配的图像。我正在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)-只是一个包含库中所有簇索引的数组。我是否误解了此输出?有没有办法确定哪个图像具有最匹配的群集?
我也基于此代码做了类似的事情:
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] 删除。
我来说两句