• ORB SLAM3 使用二进制文件 ORBvoc.bin 加载Vocabulary


    使用 二进制文件 ORBvoc.bin 加载Vocabulary,将比ORBvoc.txt 速度快很多倍!

      实测1秒内完成加载:

    一、下载ORBvoc.bin

    百度网盘: ORBvoc.bin下载链接  提取码:dyyk

    解压后,将ORBvoc.bin拷贝到Vocabulary文件夹下

    二、修改代码

    2.1  修改 Thirdparty/DBoW2/DBoW2/TemplatedVocabulary.h

    ①249 行在 “TextFile”类型 的下面 加入 loadFromBinaryFile() 函数 和   saveToBinaryFile() 函数的声明:

    1. /**
    2. * Loads the vocabulary from a binary file
    3. * @param filename
    4. */
    5. bool loadFromBinaryFile(const std::string &filename);
    6. /**
    7. * Saves the vocabulary into a binary file
    8. * @param filename
    9. */
    10. void saveToBinaryFile(const std::string &filename) const;

    如图: 

     

    ② 加入上面两个函数的定义:

     我们就写在 1465行左右 的saveToTextFile()函数定义的后面 :

    1. template<class TDescriptor, class F>
    2. bool TemplatedVocabulary::loadFromBinaryFile(const std::string &filename) {
    3. fstream f;
    4. f.open(filename.c_str(), ios_base::in|ios::binary);
    5. unsigned int nb_nodes, size_node;
    6. f.read((char*)&nb_nodes, sizeof(nb_nodes));
    7. f.read((char*)&size_node, sizeof(size_node));
    8. f.read((char*)&m_k, sizeof(m_k));
    9. f.read((char*)&m_L, sizeof(m_L));
    10. f.read((char*)&m_scoring, sizeof(m_scoring));
    11. f.read((char*)&m_weighting, sizeof(m_weighting));
    12. createScoringObject();
    13. m_words.clear();
    14. m_words.reserve(pow((double)m_k, (double)m_L + 1));
    15. m_nodes.clear();
    16. m_nodes.resize(nb_nodes+1);
    17. m_nodes[0].id = 0;
    18. char buf[size_node]; int nid = 1;
    19. while (!f.eof()) {
    20. f.read(buf, size_node);
    21. m_nodes[nid].id = nid;
    22. // FIXME
    23. const int* ptr=(int*)buf;
    24. m_nodes[nid].parent = *ptr;
    25. //m_nodes[nid].parent = *(const int*)buf;
    26. m_nodes[m_nodes[nid].parent].children.push_back(nid);
    27. m_nodes[nid].descriptor = cv::Mat(1, F::L, CV_8U);
    28. memcpy(m_nodes[nid].descriptor.data, buf+4, F::L);
    29. m_nodes[nid].weight = *(float*)(buf+4+F::L);
    30. if (buf[8+F::L]) { // is leaf
    31. int wid = m_words.size();
    32. m_words.resize(wid+1);
    33. m_nodes[nid].word_id = wid;
    34. m_words[wid] = &m_nodes[nid];
    35. }
    36. else
    37. m_nodes[nid].children.reserve(m_k);
    38. nid+=1;
    39. }
    40. f.close();
    41. return true;
    42. }
    43. // --------------------------------------------------------------------------
    44. template<class TDescriptor, class F>
    45. void TemplatedVocabulary::saveToBinaryFile(const std::string &filename) const {
    46. fstream f;
    47. f.open(filename.c_str(), ios_base::out|ios::binary);
    48. unsigned int nb_nodes = m_nodes.size();
    49. float _weight;
    50. unsigned int size_node = sizeof(m_nodes[0].parent) + F::L*sizeof(char) + sizeof(_weight) + sizeof(bool);
    51. f.write((char*)&nb_nodes, sizeof(nb_nodes));
    52. f.write((char*)&size_node, sizeof(size_node));
    53. f.write((char*)&m_k, sizeof(m_k));
    54. f.write((char*)&m_L, sizeof(m_L));
    55. f.write((char*)&m_scoring, sizeof(m_scoring));
    56. f.write((char*)&m_weighting, sizeof(m_weighting));
    57. for(size_t i=1; i
    58. const Node& node = m_nodes[i];
    59. f.write((char*)&node.parent, sizeof(node.parent));
    60. f.write((char*)node.descriptor.data, F::L);
    61. _weight = node.weight; f.write((char*)&_weight, sizeof(_weight));
    62. bool is_leaf = node.isLeaf(); f.write((char*)&is_leaf, sizeof(is_leaf)); // i put this one at the end for alignement....
    63. }
    64. f.close();
    65. }
    66. // --------------------------------------------------------------------------

    位置如图: 

     

    2.2  修改 ORB_SLAM3/src/System.cc

    修改加载词袋的类型为 :BinaryFile

    1. mpVocabulary = new ORBVocabulary();
    2. //bool bVocLoad = mpVocabulary->loadFromTextFile(strVocFile); //txt加载
    3. bool bVocLoad = mpVocabulary->loadFromBinaryFile(strVocFile); //bin加载

    三、运行

    3.1 重新编译ORB-SLAM3

    ./build.sh

    3.2 运行

    运行时,需要把原来加载词袋命令  ./Vocabulary/ORBvoc.txt  改成  ./Vocabulary/ORBvoc.bin

    如运行 EuRoc 单目时:

    ./Examples/Monocular/mono_euroc ./Vocabulary/ORBvoc.bin ./Examples/Monocular/EuRoC.yaml "$pathDatasetEuroc"/MH01 ./Examples/Monocular/EuRoC_TimeStamps/MH01.txt 
  • 相关阅读:
    Web项目【用户管理系统】完整版
    【重温Python基础】最全Python基础知识点,加班熬夜花了三天终于总结出来了,非常详细
    qtcreator 中使用opencv处理图片
    Vue实现模糊查询:filter()
    【FreeCodeCamp】 ResponsiveWebDesign网页设计 测试1学习笔记
    【BrowserRouter与HashRouter的区别】
    CMake Tutorial 巡礼(3)_添加库的使用需求
    RT-Thread UART设备
    spark-03
    Git工具快速入门_一小时速成
  • 原文地址:https://blog.csdn.net/weixin_62952541/article/details/134443113