DBoW2加载训练好的二进制格式ORB特征词典及测试

为了利用ORB-SLAM2中提供的二进制格式的ORB词典,根据开源,稍作修改,这里给出自己编译后的lib文件及include文件,对于自己新建的项目,链接到上面的两个文件即可。
库文件及词典下载地址:
https://github.com/nxyzgf/sgg-dbow2
测试代码

#include "stdafx.h"
#include <BowVector.h>
#include <TemplatedVocabulary.h>
#include <FORB.h>
#include "tinydir.h"
#include <time.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>

using namespace std;
using namespace cv;
using namespace DBoW2;

void get_file_names(string dir_name, vector<string> & names)
{
    names.clear();
    tinydir_dir dir;
    tinydir_open(&dir, dir_name.c_str());
    while (dir.has_next)
    {
       tinydir_file file;
       tinydir_readfile(&dir, &file);
       if (!file.is_dir)
       {
           names.push_back(file.path);
       }
       tinydir_next(&dir);
    }
    tinydir_close(&dir);
}

int main()
{
    typedef TemplatedVocabulary<DBoW2::FORB::TDescriptor, DBoW2::FORB> ORBVocabulary;
    ORBVocabulary* mpVocabulary;
    mpVocabulary = new ORBVocabulary();
    //加载预训练词典
    bool bVocLoad = mpVocabulary->loadFromBinaryFile("C:\\Users\\sggzg\\Desktop\\DBoW2\\ORBvoc.bin");
    if (!bVocLoad)
    {
        cerr << "Wrong path to vocabulary. " << endl;
        cerr << "Falied to open at:ORBvoc.bin " << endl;
        exit(-1);
    }
    cout << "Vocabulary loaded!" << endl << endl;

    //使用词典
    Ptr<Feature2D> fdetector;
    fdetector = ORB::create(750, 1.2f, 8, 31, 0, 2, ORB::HARRIS_SCORE, 31, 20);
    vector<string> img_names;
    vector<Mat> features;
    BowVector v1, v2;
    get_file_names("C:\\Users\\sggzg\\Desktop\\tupian", img_names);

    for (int i = 0; i < img_names.size(); i++)
    {
        Mat image = cv::imread(img_names[i], 0);
        vector<KeyPoint>  keypoints;
        Mat descriptors;
        fdetector->detectAndCompute(image, Mat(), keypoints, descriptors);
        features.push_back(descriptors);
    }
    clock_t start, finish;
    double totaltime;
    start = clock();

    for (int i = 0; i < features.size(); i++)
    {
        mpVocabulary->transform(features[0], v1);
        mpVocabulary->transform(features[i], v2);
        cout << "Image " << "1" << " vs Image " << i + 1 << ": " << mpVocabulary->score(v1, v2) << endl;
    }

    finish = clock();
    totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
    cout << "\n此程序的运行时间为" << totaltime << "秒!" << endl;
    system("pause");
    return 0;
}

测试
测试图像(来源:https://demuc.de/colmap/datasets/)
在这里插入图片描述
测试结果
在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值