orbslam-BoW

为什么要研究BoW?
闭环检测:核心就是判断两张图片是否是同一个场景,也就是判断图像的相似性。
如何计算两张图的相似性?
类帧差方法
不行,这样做有很多问题
视角变化
没办法将两张图的像素进行一一匹配,直接帧差时几乎不能保证是同一个像素点的差。

词袋法
Bag of words 可以解决这个问题。是以图像特征集合作为visual words,只关心图像中有没有这些words,有多少次,更符合人类认知方式 ,对不同光照、视角变换、季节更替等非常鲁棒。
加速匹配
ORB-SLAM2代码中使用的 SearchByBoW(用于关键帧跟踪、重定位、闭环检测SIM3计算),以及局部地图里的SearchForTriangulation, 内部实现主要是利用了 BoW中的FeatureVector 来加速特征匹配。
使用FeatureVector 避免了所有特征点的两两匹配,只比较同一个节点下的特征点,极大加速了匹配效率,至于匹配精度,论文 《Bags of Binary Words for Fast Place Recognition in Image Sequences 》中提到在26292 张图片里的 false positive 为0,说明精度是有保证的。
实际应用中效果非常不错。
缺点:
需要提前加载离线训练好的词袋字典,增加了存储空间。但是带来的优势远大于劣势,而且也有不少改进方法比如用二进制存储等来压缩词 袋,减少存储空间,提升加载速度。
离线训练 vocabulary tree(也称为字典)
首先图像提取ORB 特征点,将描述子通过 k-means 进行聚类,根据设定的树的分支数和深度,从叶子节点开始聚类一直到根节点,最后得 到一个非常大的 vocabulary tree。
1、遍历所有的训练图像,对每幅图像提取ORB特征点。
2、设定vocabulary tree的分支数K和深度L。将特征点的每个描述子用 K-means聚类,变成 K个集合,作为vocabulary tree 的第1层级,然 后对每个集合重复该聚类操作,就得到了vocabulary tree的第2层级,继续迭代最后得到满足条件的vocabulary tree,它的规模通常比较 大,比如ORB-SLAM2使用的离线字典就有108万+ 个节点。
在线图像生成BoW向量
1、对新来的一帧图像进行ORB特征提取,得到一定数量(一般几百个)的特征点,描述子维度和vocabulary tree中的一致。
2、对于每个特征点的描述子,从离线创建好的vocabulary tree中开始找自己的位置,从根节点开始,用该描述子和每个节点的描述子计算 汉明距离,选择汉明距离最小的作为自己所在的节点,一直遍历到叶子节点。
整个过程是这样的,见下图。紫色的线表示 一个特征点从根节点到叶子节点的过程。

词袋模型的实现 :

#include<iostream>
#include<vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/features2d.hpp>
#include "DBoW3/DBoW3.h"//词袋支持头文件
using namespace std;
using namespace cv;
int main(int argc, char const *argv[])
{
    cout<< "reading images..."<<endl;
    vector<Mat> images;

    //read img
    for(int i=0;i<10 ;i++)
    {
         string  path="/home/robot/test_project/slambook/data/"+to_string(i+1) +".png";
         images.push_back(imread(path));

    }

    //detect ORB features
    cout <<"detect 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);

    }

    cout <<"creating vocabulary . . ." << endl;

    // create vocabulary (创建字典)
    DBoW3::Vocabulary vocab;//默认构造函数 k=10,d=5

    vocab.create( descriptors );
    cout<<"vocabulary info: "<<vocab<<endl;//字典信息
    vocab.save( "vocabulary.yml.gz" );//保存字典压缩包
    cout<<"done"<<endl;//输出done
    return 0;






    

}
#include "DBoW3/DBoW3.h"//词袋支持头文件
#include <opencv2/core/core.hpp>//opencv核心模块
#include <opencv2/highgui/highgui.hpp>//gui模块
#include <opencv2/features2d/features2d.hpp>//特征点头文件
#include <iostream>
#include <vector>
#include <string>

using namespace cv;
using namespace std;


int main(int argc, char **argv) {
    // if (argc != 2) {
    //     cout << "Usage: 需要字典" << endl;
    //     return 1;
    // }
    // string zidian_file = argv[1];
    DBoW3::Vocabulary vocab("/home/robot/test_project/BoW/slambook/example/vocabulary.yml.gz");

    // read the images and database(读取图像和数据库)
    cout << "reading database" << endl;//输出reading database(读取数据)
    //DBoW3::Vocabulary vocab("../src/vocabulary.yml.gz");//vocabulary.yml.gz路径
    //DBoW3::Vocabulary vocab("../src/vocab_larger.yml.gz");  // use large vocab if you want:
    if (vocab.empty()) {
        cerr << "Vocabulary does not exist." << endl;//输出Vocabulary does not exist
        return 1;
    }
    cout << "reading images... " << endl;//输出reading images...
    vector<Mat> images;
    for (int i = 0; i < 10; i++) {
        string path = "/home/robot/test_project/BoW/slambook/data/" + to_string(i + 1) + ".png";//图像读取路径
        images.push_back(imread(path));
    }

    // 这里我们用它们生成的字典比较它们本身的相似性,这可能会产生过拟合
    // detect ORB features
    cout << "detecting ORB features ... " << endl;//输出detecting ORB features ...(正在检测ORB特征)
    Ptr<Feature2D> detector = ORB::create();//默认图像500个特征点
    vector<Mat> descriptors;//描述子  将10张图像提取ORB特征并存放在vector容器里
    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 :
    cout << "comparing images with images " << endl;//输出comparing images with images
    for (int i = 0; i < images.size(); i++)
    {
        DBoW3::BowVector v1;
        //descriptors[i]表示图像i中所有的ORB描述子集合,函数transform()计算出用先前字典来描述的单词向量,每个向量中元素的值要么是0,表示图像i中没有这个单词;要么是该单词的权重
        //BoW描述向量中含有每个单词的ID和权重,两者构成了整个稀疏的向量
        //当比较两个向量时,DBoW3会为我们计算一个分数
        vocab.transform(descriptors[i], v1);
        for (int j = i; j < images.size(); j++)
        {
            DBoW3::BowVector v2;
            vocab.transform(descriptors[j], v2);
            double score = vocab.score(v1, v2);
            cout << "image " << i << " vs image " << j << " : " << score << endl;//输出一幅图像与另外一幅图像之间的相似度评分
        }
        cout << endl;
    }

    // or with database
    //在进行数据库查询时,DBoW对上面的分数进行排序,给出最相似的结果
    cout << "comparing images with database " << endl;
    DBoW3::Database db(vocab, false, 0);
    for (int i = 0; i < descriptors.size(); i++)
        db.add(descriptors[i]);
    cout << "database info: " << db << endl;//输出database info(数据库信息)为
    for (int i = 0; i < descriptors.size(); i++)
    {
        DBoW3::QueryResults ret;
        db.query(descriptors[i], ret, 4);      // max result=4
        cout << "searching for image " << i << " returns " << ret << endl << endl;
    }
    cout << "done." << endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值