需要准备的知识点:http://www.cnblogs.com/zjiaxing/p/5616653.html

            http://www.cnblogs.com/zjiaxing/p/5616664.html

        http://www.cnblogs.com/zjiaxing/p/5616670.html

        http://www.cnblogs.com/zjiaxing/p/5616679.html

#include <iostream>
#include <vector> // DBoW2
#include "DBoW2.h" // defines Surf64Vocabulary and Surf64Database #include <DUtils/DUtils.h>
#include <DVision/DVision.h> // OpenCV
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp> using namespace DBoW2;
using namespace DUtils;
using namespace std; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void loadFeatures(vector<vector<vector<float> > > &features);
void changeStructure(const vector<float> &plain, vector<vector<float> > &out,
int L);
void testVocCreation(const vector<vector<vector<float> > > &features);
void testDatabase(const vector<vector<vector<float> > > &features); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // number of training images
const int NIMAGES = ; // extended surf gives 128-dimensional vectors
const bool EXTENDED_SURF = false; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void wait()
{
cout << endl << "Press enter to continue" << endl;
getchar();
} // ---------------------------------------------------------------------------- int main()
{
vector<vector<vector<float> > > features;
loadFeatures(features);
testVocCreation(features);
wait(); testDatabase(features); return ;
} // ---------------------------------------------------------------------------- void loadFeatures(vector<vector<vector<float> > > &features)
{
features.clear();
features.reserve(NIMAGES);
cv::Ptr<cv::xfeatures2d::SURF> surf = cv::xfeatures2d::SURF::create(, , , EXTENDED_SURF);
cout << "Extracting SURF features..." << endl;
for(int i = ; i < NIMAGES; ++i)
{
stringstream ss;
ss << "images/image" << i << ".png";
cv::Mat image = cv::imread(ss.str(), );
cv::Mat mask;
vector<cv::KeyPoint> keypoints;
vector<float> descriptors; surf->detectAndCompute(image, mask, keypoints, descriptors); features.push_back(vector<vector<float> >());
changeStructure(descriptors, features.back(), surf->descriptorSize());
}
} // ---------------------------------------------------------------------------- void changeStructure(const vector<float> &plain, vector<vector<float> > &out,
int L)
{
out.resize(plain.size() / L);
unsigned int j = ;
for(unsigned int i = ; i < plain.size(); i += L, ++j)
{
out[j].resize(L);
std::copy(plain.begin() + i, plain.begin() + i + L, out[j].begin());
}
} // ---------------------------------------------------------------------------- void testVocCreation(const vector<vector<vector<float> > > &features)
{
// Creates a vocabulary from the training features, setting the branching
factor and the depth levels of the tree and the weighting and scoring
schemes * Creates k clusters from the given descriptors with some seeding algorithm. const int k = ;
const int L = ;
const WeightingType weight = TF_IDF;
const ScoringType score = L1_NORM; Surf64Vocabulary voc(k, L, weight, score); cout << "Creating a small " << k << "^" << L << " vocabulary..." << endl;
voc.create(features);
cout << "... done!" << endl; cout << "Vocabulary information: " << endl
<< voc << endl << endl; // lets do something with this vocabulary
cout << "Matching images against themselves (0 low, 1 high): " << endl;
BowVector v1, v2;
for(int i = ; i < NIMAGES; i++)
{
//Transforms a set of descriptores into a bow vector
voc.transform(features[i], v1);
for(int j = ; j < NIMAGES; j++)
{
voc.transform(features[j], v2); double score = voc.score(v1, v2);
cout << "Image " << i << " vs Image " << j << ": " << score << endl;
}
} // save the vocabulary to disk
cout << endl << "Saving vocabulary..." << endl;
voc.save("small_voc.yml.gz");
cout << "Done" << endl;
} // ---------------------------------------------------------------------------- void testDatabase(const vector<vector<vector<float> > > &features)
{
cout << "Creating a small database..." << endl; // load the vocabulary from disk
Surf64Vocabulary voc("small_voc.yml.gz"); Surf64Database db(voc, false, ); // false = do not use direct index
// (so ignore the last param)
// The direct index is useful if we want to retrieve the features that
// belong to some vocabulary node.
// db creates a copy of the vocabulary, we may get rid of "voc" now // add images to the database
for(int i = ; i < NIMAGES; i++)
{
db.add(features[i]);
} cout << "... done!" << endl; cout << "Database information: " << endl << db << endl; // and query the database
cout << "Querying the database: " << endl; QueryResults ret;
for(int i = ; i < NIMAGES; i++)
{
db.query(features[i], ret, ); // ret[0] is always the same image in this case, because we added it to the
// database. ret[1] is the second best match. cout << "Searching for Image " << i << ". " << ret << endl;
} cout << endl; // we can save the database. The created file includes the vocabulary
// and the entries added
cout << "Saving database..." << endl;
db.save("small_db.yml.gz");
cout << "... done!" << endl; // once saved, we can load it again
cout << "Retrieving database once again..." << endl;
Surf64Database db2("small_db.yml.gz");
cout << "... done! This is: " << endl << db2 << endl;
}

视觉slam闭环检测之-DBoW2 -视觉词袋构建的更多相关文章

  1. 《视觉SLAM十四讲》第2讲

    目录 一 视觉SLAM中的传感器 二 经典视觉SLAM框架 三 SLAM问题的数学表述 注:原创不易,转载请务必注明原作者和出处,感谢支持! 本讲主要内容: (1) 视觉SLAM中的传感器 (2) 经 ...

  2. 视觉SLAM之词袋(bag of words) 模型与K-means聚类算法浅析

    原文地址:http://www.cnblogs.com/zjiaxing/p/5548265.html 在目前实际的视觉SLAM中,闭环检测多采用DBOW2模型https://github.com/d ...

  3. 视觉SLAM之词袋(bag of words) 模型与K-means聚类算法浅析(1)

    在目前实际的视觉SLAM中,闭环检测多采用DBOW2模型https://github.com/dorian3d/DBoW2,而bag of words 又运用了数据挖掘的K-means聚类算法,笔者只 ...

  4. (转) SLAM系统的研究点介绍 与 Kinect视觉SLAM技术介绍

          首页 视界智尚 算法技术 每日技术 来打我呀 注册     SLAM系统的研究点介绍 本文主要谈谈SLAM中的各个研究点,为研究生们(应该是博客的多数读者吧)作一个提纲挈领的摘要.然后,我 ...

  5. 视觉SLAM漫谈 (三): 研究点介绍

    1. 前言 读者朋友们大家好!(很久很久)之前,我们为大家介绍了SLAM的基本概念和方法.相信大家对SLAM,应该有了基本的认识.在忙完一堆写论文.博士开题的事情之后,我准备回来继续填坑:为大家介绍S ...

  6. 视觉SLAM关键方法总结

    点"计算机视觉life"关注,置顶更快接收消息! 最近在做基于激光信息的机器人行人跟踪发现如果单独利用激光信息很难完成机器人对行人的识别.跟踪等功能,因此考虑与视觉融合的方法,这样 ...

  7. 视觉SLAM的主要功能模块分析

    视觉SLAM的主要功能模块分析 一.基本概念 SLAM (simultaneous localization and mapping),也称为CML (Concurrent Mapping and L ...

  8. 高翔《视觉SLAM十四讲》从理论到实践

    目录 第1讲 前言:本书讲什么:如何使用本书: 第2讲 初始SLAM:引子-小萝卜的例子:经典视觉SLAM框架:SLAM问题的数学表述:实践-编程基础: 第3讲 三维空间刚体运动 旋转矩阵:实践-Ei ...

  9. 视觉SLAM漫淡

    视觉SLAM漫谈 1.    前言 开始做SLAM(机器人同时定位与建图)研究已经近一年了.从一年级开始对这个方向产生兴趣,到现在为止,也算是对这个领域有了大致的了解.然而越了解,越觉得这个方向难度很 ...

随机推荐

  1. nodejs之SVG转图片下载方案

    本文介绍在nodejs基础上.怎样实现将svg转为png并下载的功能. 所需Webkit和node module简单介绍: phantomjs:一个基于WebKit的server端JavaScript ...

  2. 关于HTML、js加密、混淆、源码保护、代码安全,防止解压直接看源码

    一直有人问HTML加密混淆怎么做,其实这在业内是早已很多人研究过的课题.假日期间整理一篇文章分享给大家. 我们先理下需求,加密的目的是什么?加密到什么级别?为此我们可以牺牲什么?我们知道这个世界不存在 ...

  3. STL源码剖析(deque)

    deque是一个双向开口的容器,在头尾两端进行元素的插入跟删除操作都有理想的时间复杂度. deque使用的是分段连续线性空间,它维护一个指针数组(T** map),其中每个指针指向一块连续线性空间. ...

  4. 【Windows】免费图片提取文字的方法

    今天意外的看到一个可以提取图片中文字的网站,自己试了下,提取效果还不错 网址为: https://zhcn.109876543210.com/ 现在有图片如下 我想从中提取的文字 1.打开网址,上传图 ...

  5. poj3177 Redundant Paths 边双连通分量

    给一个无向图,问至少加入多少条边能够使图变成双连通图(随意两点之间至少有两条不同的路(边不同)). 图中的双连通分量不用管,所以缩点之后建新的无向无环图. 这样,题目问题等效于,把新图中度数为1的点相 ...

  6. 本体论与OWL

    http://semanticweb.org/wiki/Main_Page.html http://owl.man.ac.uk/documentation.shtml  https://zh.wiki ...

  7. SVN服务器更改ip地址客户端怎么设置(转载)

    SVN 服务器 IP 地址修改后,客户端对服务器的连接可以采用以下的方法重定位: 1. 如果客户端工具是TortoiseSVN,直接在工作副本上右键,选择TortoiseSVN->relocat ...

  8. UIView的使用setNeedsDisplay

    1,UIView的setNeedsDisplay和setNeedsLayout方法   首先两个方法都是异步运行的.而setNeedsDisplay会调用自己主动调用drawRect方法.这样能够拿到 ...

  9. jQuery中的text(),html(),val()有什么区别

    text():获取或者改变指定元素的文本html():获取或改变指定元素的html元素以及文本val():获取或者改变指定元素的value值(一般是表单元素) 以上3个都是jquery类库中的语法 第 ...

  10. 新标准C++程序设计读书笔记_继承和多态

    简单继承的例子: #include <iostream> #include <string> using namespace std; class CStudent { pri ...