g2o学习:实践GraphSLAM_tutorials_code及报错解决

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Discoverhfub/article/details/96598694

先看g2o代码效果
在这里插入图片描述
g2o优化后
在这里插入图片描述
首先是g2o库安装(在文末)
下载代码:(GraphSLAM_tutorials_code代码,编译过程的错误已修改解决)
GraphSLAM_tutorials_code代码编译

mkdir build
cd build
cmake ..
make

运行:

cd ..
cd bin/
./g2o_test   (在data文件夹生成一个sphere_after.g2o)

g2o_viewer查看:

cd ..
cd data/
g2o_viewer sphere_bignoise_vertex3.g2o   (这是优化前)

g2o_viewer sphere_after.g2o 

出现指针问题:

/home/~/code/GraphSLAM_tutorials_code/g2o_test/main.cpp:26:62: error: no matching function for call to ‘g2o::BlockSolver<g2o::BlockSolverTraits<-1, -1> >::BlockSolver(g2o::BlockSolver<g2o::BlockSolverTraits<-1, -1> >::LinearSolverType*&)’
BlockSolverX* blockSolver = new BlockSolverX(linearSolver);


error: no matching function for call to ‘g2o::OptimizationAlgorithmLevenberg::OptimizationAlgorithmLevenberg(g2o::BlockSolverX*&)’
nberg* optimizationAlgorithm = new OptimizationAlgorithmLevenberg(blockSolver);


原因:指针的初始化不符合函数要求,unique_ptr“唯一”拥有其所指对象,同一时刻只能有一个unique_ptr指向给定对象(通过禁止拷贝语义、只有移动语义来实现)。

解决:
第26行

BlockSolverX* blockSolver = new BlockSolverX(linearSolver);
修改为
BlockSolverX* blockSolver = new BlockSolverX(std::unique_ptr<BlockSolverX::LinearSolverType>(linearSolver) );

29行

OptimizationAlgorithmLevenberg* optimizationAlgorithm = new OptimizationAlgorithmLevenberg(blockSolver);

修改为:
OptimizationAlgorithmLevenberg* optimizationAlgorithm = new OptimizationAlgorithmLevenberg(unique_ptr<BlockSolverX>(blockSolver));

g2o库安装

依赖项:

sudo apt-get install libeigen3-dev libsuitesparse-dev libqt4-dev qt4-qmake libqglviewer-qt4-dev  

接着把g2o代码下载到相应文件夹里
g2o安装源码下载
编译和安装

mkdir build  
cd build  
cmake ../  
make  

猜你喜欢

转载自blog.csdn.net/Discoverhfub/article/details/96598694
g2o