SLAM闲谈(一)--词袋模型

前言

BoW3和FBow是常用的词袋模型的开源算法。Bow(bag-of-words)也就是词袋,它的出发点是用几个特征表述一幅图像,其示意如图。在图像检索的过程中,将特征出现的次数进行统计(统计直方图 ),然后生成特征向量,经过分类器检索图片。在实际中,我们使用词袋的目的是为了加快图片检索的速度。其主要用于slam算法中的回环检测。
Bow3原理讲解词袋模型在SLAM算法中的应用,视觉SLAM十四讲中也用对此的原理讲解。
本文主要说明了BoW3和FBow算法的使用与性能对比。

1.BoW3

ORB-SLAM2中用到的词袋模型算法是BoW2,Bow3是它升级版本,它的升级是减少了依赖库,只依赖opencv,具体区别为;

  1. DBoW3依赖项只有OpenCV,DBoW2依赖项DLIB被移除。
  2. DBoW3可以直接使用二值和浮点特征描述子,不需要再为这些特征描述子重写新类。
  3. DBoW3可以在Linux和Windows下编译。
  4. 为了优化执行速度,重写了部分代码(特征的操作都写入类DescManip);DBoW3的接口也被简化了。
  5. 可以使用二进制视觉词典文件;二进制文件在加载和保存上比.yml文件快4-5倍;而且,二进制文件还能被压缩。
  6. 仍然和DBoW2yml文件兼容。

1.1安装

源码地址,其本身是一个cmake的工程,使用cmake的安装方式即可。

mkdir build
cd build
cmake ..
make
sudo make install

注:DBoW3依赖opencv,使用前请查看opencv版本。

pkg-config --modversion opencv

本机版本:3.4.14。

1.2使用

这个库的说明文档写的很简单,使用这个库时,建议自己写脚本,这里在视觉SLAM十四讲ch12中的代码上修改,得到测试脚本。
cmakelist的编写

cmake_minimum_required( VERSION 2.8 )
project( loop_closure )

set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++11 -O3" )

# opencv 
find_package( OpenCV 3.1 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

# dbow3 
# dbow3 is a simple lib so I assume you installed it in default directory 
set( DBoW3_INCLUDE_DIRS "/usr/local/include" )
set( DBoW3_LIBS "/usr/local/lib/libDBoW3.so" )  #这里的源代码有误,按这个写

add_executable( feature_training feature_training.cpp )
target_link_libraries( feature_training ${OpenCV_LIBS} ${DBoW3_LIBS} )

add_executable( loop_closure loop_closure.cpp )
target_link_libraries( loop_closure ${OpenCV_LIBS} ${DBoW3_LIBS} )

词袋训练代码feature_training.cpp

#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 image 
    cout<<"reading images... "<<endl;
    vector<Mat> images; 
    for ( int i=0; i<35; i++ )  //35是图片数
    {
        string path = "./test/"+to_string(i+1)+".jpg";  //./test/训练图片的目录,可以自己修改,图片要以数字命名
        images.push_back( imread(path) );
    }
    //使用ORB特征
    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 );
    }  
    // 创建字典
    cout<<"creating vocabulary ... "<<endl;
    DBoW3::Vocabulary vocab;
    vocab.create( descriptors );
    cout<<"vocabulary info: "<<vocab<<endl;
    vocab.save( "vocabulary.yml.gz" );
    cout<<"done"<<endl;
      
    return 0;
}

图片匹配代码loop_closure.cpp。

#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");
    if ( vocab.empty() )
    {
        cerr<<"Vocabulary does not exist."<<endl;
        return 1;
    }
    cout<<"reading images... "<<endl;
    vector<Mat> images; 
   for ( int i=0; i<35; i++ )
   {
       string path = "./ex/"+to_string(i+1)+".jpg";
       images.push_back(imread(path));
    }
    // 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 );
    }
    
    // images :
    cout<<"comparing images with images "<<endl;
    DBoW3::BowVector v1;
    vocab.transform( descriptors[1], v1 );  //用第2号图片与其他的比较
    for ( int j=0; j<images.size(); j++ )
    {
        DBoW3::BowVector v2;
        vocab.transform( descriptors[j], v2 );
        double score = vocab.score(v1, v2);
        cout<<"image 2 "<<" vs image "<<(j+1)<<" : "<<score<<endl;
     }
     cout<<endl;
    // or with database 
    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;
    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;
}

2.FBoW

FBOW(Fast Bag of Words)是 DBow2/DBow3 库的极端优化版本,其加速了词袋的创建速度

2.1安装

源码地址,安装方法和1.1一致。

mkdir build
cd build
cmake ..
make
sudo make install

2.2 使用

这里建立了与1.2相同名字的测试程序。
cmakelist的编写

cmake_minimum_required( VERSION 2.8 )
project( loop_closure )

set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++11 -O3" )

# opencv 
find_package( OpenCV 3.1 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

set( FBOW_INCLUDE_DIRS "/usr/local/include" )
set( FBOW_LIBS "/usr/local/lib/libfbow.so" )

add_executable( feature_training feature_training.cpp )
target_link_libraries( feature_training ${OpenCV_LIBS} ${FBOW_LIBS} )

add_executable( loop_closure loop_closure.cpp )
target_link_libraries( loop_closure ${OpenCV_LIBS} ${FBOW_LIBS} )

词袋训练代码feature_training.cpp

#include "fbow/fbow.h"
#include "fbow/vocabulary_creator.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 image 
    cout<<"reading images... "<<endl;
    vector<Mat> images; 
    for ( int i=0; i<35; i++ )
    {
        string path = "./test/"+to_string(i+1)+".jpg";
        images.push_back( imread(path) );
    }
    // 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 );
    } 
    //训练词点
    //训练词典的参数
    fbow::VocabularyCreator::Params params;
    //fbow::VocabularyCreator::Params params;
    //叶子节点的个数
    params.k = 10;
    //树的高度
    params.L = 5;
    //使用的线程数量
    params.nthreads=1;
    //最大迭代次数
    params.maxIters=0;
    //词典创建工具
    fbow::VocabularyCreator vocabCat;
    //词典
    cout<<"creating vocabulary ... "<<endl;
    fbow::Vocabulary vocab;
    vocabCat.create(vocab,descriptors,"hf-net",params);
    //保存词典
    vocab.saveToFile("filename");
    cout<<"done"<<endl;
    return 0;
}

图片匹配代码loop_closure.cpp。

#include "fbow/fbow.h"
#include "fbow/vocabulary_creator.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;
    fbow::Vocabulary vocab;
    vocab.readFromFile("filename");
  
    if (vocab.isValid() == 0)
    {
        cerr<<"Vocabulary does not exist."<<endl;
        return 1;
    }
    cout<<"reading images... "<<endl;
    vector<Mat> images; 
    for ( int i=0; i<35; i++ )
    {
       string path = "./ex/"+to_string(i+1)+".jpg";
       images.push_back(imread(path));
     }  
    // 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 );
    }
    // images :
    cout<<"comparing images with images "<<endl;
    fbow::fBow bowvector1;
    bowvector1 = vocab.transform(descriptors[1]);
    for ( int j=0; j<images.size(); j++ )
    { 
        fbow::fBow bowvector2;
        bowvector2 = vocab.transform(descriptors[j]);
        //两个向量评分,调用静态方法
        double score = fbow::fBow::score(bowvector1,bowvector2);
        cout<<"image 2 "<<" vs image "<<(j+1)<<" : "<<score<<endl;
     }
    cout<<endl;
    cout<<"done."<<endl;
}

3.性能比较

运行程序的指令都为:

build/feature_training    #生成词袋
build/loop_closure   #比较

测试运行时间,这里使用time命令进行测试:
词袋生成程序运行时间

BOW3FBOW
real1.297s0.474s
user(执行时间)1.634s0.799s
sys0.091s0.097s

图片检索程序运行时间

BOW3FBOW
real0.531s0.508s
user(执行时间)0.871s0.777s
sys0.090s0.117s

注:FBOW的优势主要在词袋的生成上,但它的依赖库比较多。
图片匹配测试:

12345626272829303132
BOW30.0761.0000.0830.0400.0400.0140.0210.0110.0070.0060.0120.0040.003
FBOW0.1750.9990.2140.1980.1720.1960.1900.2000.0290.0720.0430.0530.108

**注:数字为图片编号,以上测试数据是在利用词袋的情况下,图片2(目标图片)**与剩下的图片一一匹配的结果。BOW3与不一样的图片匹配得分偏低。

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值