需要准备的知识点: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

  1. #include <iostream>
  2. #include <vector>
  3.  
  4. // DBoW2
  5. #include "DBoW2.h" // defines Surf64Vocabulary and Surf64Database
  6.  
  7. #include <DUtils/DUtils.h>
  8. #include <DVision/DVision.h>
  9.  
  10. // OpenCV
  11. #include <opencv2/core.hpp>
  12. #include <opencv2/highgui.hpp>
  13. #include <opencv2/xfeatures2d/nonfree.hpp>
  14.  
  15. using namespace DBoW2;
  16. using namespace DUtils;
  17. using namespace std;
  18.  
  19. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  20.  
  21. void loadFeatures(vector<vector<vector<float> > > &features);
  22. void changeStructure(const vector<float> &plain, vector<vector<float> > &out,
  23. int L);
  24. void testVocCreation(const vector<vector<vector<float> > > &features);
  25. void testDatabase(const vector<vector<vector<float> > > &features);
  26.  
  27. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  28.  
  29. // number of training images
  30. const int NIMAGES = ;
  31.  
  32. // extended surf gives 128-dimensional vectors
  33. const bool EXTENDED_SURF = false;
  34.  
  35. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  36.  
  37. void wait()
  38. {
  39. cout << endl << "Press enter to continue" << endl;
  40. getchar();
  41. }
  42.  
  43. // ----------------------------------------------------------------------------
  44.  
  45. int main()
  46. {
  47. vector<vector<vector<float> > > features;
  48. loadFeatures(features);
  49. testVocCreation(features);
  50. wait();
  51.  
  52. testDatabase(features);
  53.  
  54. return ;
  55. }
  56.  
  57. // ----------------------------------------------------------------------------
  58.  
  59. void loadFeatures(vector<vector<vector<float> > > &features)
  60. {
  61. features.clear();
  62. features.reserve(NIMAGES);
  63. cv::Ptr<cv::xfeatures2d::SURF> surf = cv::xfeatures2d::SURF::create(, , , EXTENDED_SURF);
  64. cout << "Extracting SURF features..." << endl;
  65. for(int i = ; i < NIMAGES; ++i)
  66. {
  67. stringstream ss;
  68. ss << "images/image" << i << ".png";
  69. cv::Mat image = cv::imread(ss.str(), );
  70. cv::Mat mask;
  71. vector<cv::KeyPoint> keypoints;
  72. vector<float> descriptors;
  73.  
  74. surf->detectAndCompute(image, mask, keypoints, descriptors);
  75.  
  76. features.push_back(vector<vector<float> >());
  77. changeStructure(descriptors, features.back(), surf->descriptorSize());
  78. }
  79. }
  80.  
  81. // ----------------------------------------------------------------------------
  82.  
  83. void changeStructure(const vector<float> &plain, vector<vector<float> > &out,
  84. int L)
  85. {
  86. out.resize(plain.size() / L);
  87. unsigned int j = ;
  88. for(unsigned int i = ; i < plain.size(); i += L, ++j)
  89. {
  90. out[j].resize(L);
  91. std::copy(plain.begin() + i, plain.begin() + i + L, out[j].begin());
  92. }
  93. }
  94.  
  95. // ----------------------------------------------------------------------------
  96.  
  97. void testVocCreation(const vector<vector<vector<float> > > &features)
  98. {
  99. // Creates a vocabulary from the training features, setting the branching
  100. factor and the depth levels of the tree and the weighting and scoring
  101. schemes * Creates k clusters from the given descriptors with some seeding algorithm.
  102.  
  103. const int k = ;
  104. const int L = ;
  105. const WeightingType weight = TF_IDF;
  106. const ScoringType score = L1_NORM;
  107.  
  108. Surf64Vocabulary voc(k, L, weight, score);
  109.  
  110. cout << "Creating a small " << k << "^" << L << " vocabulary..." << endl;
  111. voc.create(features);
  112. cout << "... done!" << endl;
  113.  
  114. cout << "Vocabulary information: " << endl
  115. << voc << endl << endl;
  116.  
  117. // lets do something with this vocabulary
  118. cout << "Matching images against themselves (0 low, 1 high): " << endl;
  119. BowVector v1, v2;
  120. for(int i = ; i < NIMAGES; i++)
  121. {
  122. //Transforms a set of descriptores into a bow vector
  123. voc.transform(features[i], v1);
  124. for(int j = ; j < NIMAGES; j++)
  125. {
  126. voc.transform(features[j], v2);
  127.  
  128. double score = voc.score(v1, v2);
  129. cout << "Image " << i << " vs Image " << j << ": " << score << endl;
  130. }
  131. }
  132.  
  133. // save the vocabulary to disk
  134. cout << endl << "Saving vocabulary..." << endl;
  135. voc.save("small_voc.yml.gz");
  136. cout << "Done" << endl;
  137. }
  138.  
  139. // ----------------------------------------------------------------------------
  140.  
  141. void testDatabase(const vector<vector<vector<float> > > &features)
  142. {
  143. cout << "Creating a small database..." << endl;
  144.  
  145. // load the vocabulary from disk
  146. Surf64Vocabulary voc("small_voc.yml.gz");
  147.  
  148. Surf64Database db(voc, false, ); // false = do not use direct index
  149. // (so ignore the last param)
  150. // The direct index is useful if we want to retrieve the features that
  151. // belong to some vocabulary node.
  152. // db creates a copy of the vocabulary, we may get rid of "voc" now
  153.  
  154. // add images to the database
  155. for(int i = ; i < NIMAGES; i++)
  156. {
  157. db.add(features[i]);
  158. }
  159.  
  160. cout << "... done!" << endl;
  161.  
  162. cout << "Database information: " << endl << db << endl;
  163.  
  164. // and query the database
  165. cout << "Querying the database: " << endl;
  166.  
  167. QueryResults ret;
  168. for(int i = ; i < NIMAGES; i++)
  169. {
  170. db.query(features[i], ret, );
  171.  
  172. // ret[0] is always the same image in this case, because we added it to the
  173. // database. ret[1] is the second best match.
  174.  
  175. cout << "Searching for Image " << i << ". " << ret << endl;
  176. }
  177.  
  178. cout << endl;
  179.  
  180. // we can save the database. The created file includes the vocabulary
  181. // and the entries added
  182. cout << "Saving database..." << endl;
  183. db.save("small_db.yml.gz");
  184. cout << "... done!" << endl;
  185.  
  186. // once saved, we can load it again
  187. cout << "Retrieving database once again..." << endl;
  188. Surf64Database db2("small_db.yml.gz");
  189. cout << "... done! This is: " << endl << db2 << endl;
  190. }

视觉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. android回调函数

    在我们进行android开发的时候,常常遇到一些回调函数,当中,我们最常常使用的回调就是,当我们对一个组件设置监听的时候,事实上就相对于设置的回调函数.比如: Button btn = (Button ...

  2. 算法笔记_167:算法提高 矩阵翻转(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 Ciel有一个N*N的矩阵,每个格子里都有一个整数. N是一个奇数,设X = (N+1)/2.Ciel每次都可以做这样的一次操作:他从矩阵 ...

  3. 04-spring-控制反转

    使用myeclipse开发spring一个Demo. 第一步:新建一个web project. 第二步:安装spring开发的支持包. 安装后多了这几个东西 3,定义一个操作接口: package c ...

  4. JS回调函数全解析教程(callback)

    自学jQuery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速google之,发现原来中文翻译成回调.也就是回调函数了.不懂啊,于是在google回调函数,发现网上的中文解释实在是 ...

  5. C++之声明与定义的区别

    直接举例,在C++中,声明与定义的区别如下: extern int a;//若有extern关键字,则只是声明 int b;//若没有extern关键字,则为声明+定义 int a;//若之前已经声明 ...

  6. 算法----堆排序(heap sort)

    堆排序是利用堆进行排序的高效算法,其能实现O(NlogN)的排序时间复杂度,详细算法分析能够点击堆排序算法时间复杂度分析. 算法实现: 调整堆: void sort::sink(int* a, con ...

  7. cisco asa5510 配置

    anyconnect 查看vpn链接 ASA版本8.4(7)    anyconnect版本3.1  亲测sh vpn-sessiondb anyconnect  查看登录用户详情sh vpn-ses ...

  8. access denied XXXXXXXXXXXX

    这个异常是做支付的时候出现的,貌似是没有权限访问之类的,网上查了有N个解决的办法,我写一下我解决的办法吧....... 出现的异常具体: java.security.AccessControlExce ...

  9. CentOS6X安装PHP5.5

    CentOS6.x默认的php版本是php5.3,已经过时喽,现在最新的稳定版是5.5.38. 安装方法: 1.先下载2个源 rpm -Uvh https://dl.fedoraproject.org ...

  10. c3p0 一个数据库链接的例子

    首先需要准备三个依赖包 c3p0-0.9.5.2.jar.mchange-commons-java-0.2.11.jar.mysql-connector-java-5.1.47.jar 下载链接 ht ...