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

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

#include "stdafx.h"
#include 
#include 
#include 
#include "tinydir.h"
#include 
#include 
#include 
#include 

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

void get_file_names(string dir_name, vector & 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 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 fdetector;
    fdetector = ORB::create(750, 1.2f, 8, 31, 0, 2, ORB::HARRIS_SCORE, 31, 20);
    vector img_names;
    vector 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  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/)
DBoW2加载训练好的二进制格式ORB特征词典及测试_第1张图片
测试结果
DBoW2加载训练好的二进制格式ORB特征词典及测试_第2张图片

你可能感兴趣的:(DBoW2,二进制ORB词典)