根据创建的字典比较图片之间的相似度

#include “DBoW3/DBoW3.h”
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include < iostream>
#include < vector>
#include < string>

using namespace cv;
using namespace std;

/***************************************************

  • 本节演示了如何根据前面训练的字典计算相似性评分

  • ************************************************/
    int main(int argc, char **argv) {
    // read the images and database
    cout << “reading database” << endl;
    DBoW3::Vocabulary vocab("./vocabulary.yml.gz");加载字典vocab
    // DBoW3::Vocabulary vocab("./vocab_larger.yml.gz"); // use large vocab if you want:
    if (vocab.empty()) {
    cerr << “Vocabulary does not exist.” << endl;
    return 1;
    }
    cout << "reading images… " << endl;
    vector< Mat> images;
    for (int i = 0; i < 10; i++) {
    string path = “/data/” + to_string(i + 1) + “.png”;
    images.push_back(imread(path));
    }

    // NOTE: in this case we are comparing images with a vocabulary generated by themselves, this may lead to overfit.
    // detect ORB features
    cout << "detecting ORB features … " << endl;
    Ptr< Feature2D> detector = ORB::create();
    vector< Mat> descriptors;
    for (Mat &image:images) {
    vector< KeyPoint> keypoints;
    Mat descriptor;
    detector->detectAndCompute(image, Mat(), keypoints, descriptor);加载关键点和描述子
    descriptors.push_back(descriptor);
    }

    // we can compare the images directly or we can compare one image to a database
    // images :比较data下面10副图片之间的相似性,每个图片用图片上的Mat描述子向量来表示
    cout << "comparing images with images " << endl;
    for (int i = 0; i < images.size(); i++) {
    DBoW3::BowVector v1;
    vocab.transform(descriptors[i], v1);把描述子转换成BoW向量,来进行相似度比较
    for (int j = i; j < images.size(); j++) {
    DBoW3::BowVector v2;
    vocab.transform(descriptors[j], v2);
    double score = vocab.score(v1, v2);调用score函数来获得相似度评分
    cout << "image " << i << " vs image " << j << " : " << score << endl;
    }
    cout << endl;
    }

    // or with database
    cout << "comparing images with database " << endl;
    DBoW3::Database db(vocab, false, 0);数据库的索引库初始化为vocab
    for (int i = 0; i < descriptors.size(); i++)
    db.add(descriptors[i]);在数据库的搜索库中额外添加这10个图片的描述子
    cout << "database info: " << db << endl;
    for (int i = 0; i < descriptors.size(); i++) {
    DBoW3::QueryResults ret;
    db.query(descriptors[i], ret, 4); // max result=4,把每个搜索库中的描述子在db数据库进行逐个匹配,将最可能的4个结果放入ret
    cout << "searching for image " << i << " returns " << ret << endl << endl;
    }
    cout << “done.” << endl;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值