编译并运行:
sudo mkdir build # 创建build文件夹
cd build
sudo cmake ..
报错1:
Could NOT find CSPARSE (missing: CSPARSE_INCLUDE_DIR CSPARSE_LIBRARY)
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CSPARSE_INCLUDE_DIR
解决方法:
在终端运行: sudo apt-get install libsuitesparse-dev 安装相关依赖包。
重新编译:
sudo clean
sudo cmake ..
sudo make
报错2:
[ 10%] Building CXX object CMakeFiles/pose_estimation_3d3d.dir/pose_estimation_3d3d.cpp.o
/home/zqzy/learn-slam/ch7/pose_estimation_3d3d.cpp:9:34: fatal error: g2o/core/base_vertex.h: No such file or directory
compilation terminated.
CMakeFiles/pose_estimation_3d3d.dir/build.make:62: recipe for target 'CMakeFiles/pose_estimation_3d3d.dir/pose_estimation_3d3d.cpp.o' failed
make[2]: *** [CMakeFiles/pose_estimation_3d3d.dir/pose_estimation_3d3d.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/pose_estimation_3d3d.dir/all' failed
make[1]: *** [CMakeFiles/pose_estimation_3d3d.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
解决方案:
产生这个错误的原因是g2o没有安装,安装即可。
git clone https://github.com/RainerKuemmerle/g2o.git
cd g2o
mkdir build
cd build
cmake ..
make
sudo make install
重新编译:
sudo clean
sudo cmake ..
sudo make
报错:
/home/yk/桌面/slambook-master/ch7/pose_estimation_3d3d.cpp: In function ‘void bundleAdjustment(const std::vector<cv::Point3_<float> >&, const std::vector<cv::Point3_<float> >&, cv::Mat&, cv::Mat&)’:
/home/yk/桌面/slambook-master/ch7/pose_estimation_3d3d.cpp:280:74: error: ‘LinearSolverCSparse’ in namespace ‘g2o’ does not name a template type
std::unique_ptr<Block::LinearSolverType> linearSolver ( new g2o::LinearSolve
^
/home/yk/桌面/slambook-master/ch7/pose_estimation_3d3d.cpp:280:115: error: expected primary-expression before ‘>’ token
verType> linearSolver ( new g2o::LinearSolverCSparse<Block::PoseMatrixType>());
^
/home/yk/桌面/slambook-master/ch7/pose_estimation_3d3d.cpp:280:117: error: expected primary-expression before ‘)’ token
verType> linearSolver ( new g2o::LinearSolverCSparse<Block::PoseMatrixType>());
^
/home/yk/桌面/slambook-master/ch7/pose_estimation_3d3d.cpp:287:9: error: ‘OptimizationAlgorithmLevenberg’ is not a member of ‘g2o’
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgo
^
/home/yk/桌面/slambook-master/ch7/pose_estimation_3d3d.cpp:287:46: error: ‘solver’ was not declared in this scope
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgo
^
/home/yk/桌面/slambook-master/ch7/pose_estimation_3d3d.cpp:287:59: error: expected type-specifier
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgo
^
CMakeFiles/pose_estimation_3d3d.dir/build.make:62: recipe for target 'CMakeFiles/pose_estimation_3d3d.dir/pose_estimation_3d3d.cpp.o' failed
make[2]: *** [CMakeFiles/pose_estimation_3d3d.dir/pose_estimation_3d3d.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/pose_estimation_3d3d.dir/all' failed
make[1]: *** [CMakeFiles/pose_estimation_3d3d.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
解决方案:
将pose_estimation_3d3d.cpp中的 bundleAdjustment() 函数中初始g2o修改一下:
原始代码:
typedef g2o::BlockSolver< g2o::BlockSolverTraits<6,3> > Block; // pose维度为 6, landmark 维度为 3
Block::LinearSolverType* linearSolver = new g2o::LinearSolverEigen<Block::PoseMatrixType>(); // 线性方程求解器
Block* solver_ptr = new Block( linearSolver ); // 矩阵块求解器
g2o::OptimizationAlgorithmGaussNewton* solver = new g2o::OptimizationAlgorithmGaussNewton( solver_ptr );
g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm( solver );
修改为:
typedef g2o::BlockSolver< g2o::BlockSolverTraits<6,3> > Block; // pose维度为 6, landmark 维度为 3
// Block::LinearSolverType* linearSolver = new g2o::LinearSolverEigen<Block::PoseMatrixType>(); // 线性方程求解器
std::unique_ptr<Block::LinearSolverType> linearSolver ( new g2o::LinearSolverEigen<Block::PoseMatrixType>());
//Block* solver_ptr = new Block( linearSolver ); // 矩阵块求解器
std::unique_ptr<Block> solver_ptr ( new Block ( std::move(linearSolver)));
//g2o::OptimizationAlgorithmGaussNewton* solver = new g2o::OptimizationAlgorithmGaussNewton( solver_ptr );
g2o::OptimizationAlgorithmGaussNewton* solver = new g2o::OptimizationAlgorithmGaussNewton ( std::move(solver_ptr));
g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm( solver );
将pose_estimation_3d2d.cpp中的 bundleAdjustment() 函数中初始g2o修改一下:
原始代码:
typedef g2o::BlockSolver< g2o::BlockSolverTraits<6,3> > Block; // pose 维度为 6, landmark 维度为 3
Block::LinearSolverType* linearSolver = new g2o::LinearSolverCSparse<Block::PoseMatrixType>(); // 线性方程求解器
Block* solver_ptr = new Block ( linearSolver ); // 矩阵块求解器
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( solver_ptr );
g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm ( solver );
修改为;
typedef g2o::BlockSolver< g2o::BlockSolverTraits<6,3> > Block; // pose 维度为 6, landmark 维度为 3
//Block::LinearSolverType* linearSolver = new g2o::LinearSolverCSparse<Block::PoseMatrixType>(); // 线性方程求解器
std::unique_ptr<Block::LinearSolverType> linearSolver ( new g2o::LinearSolverCSparse<Block::PoseMatrixType>());
//Block* solver_ptr = new Block ( linearSolver );
//std::unique_ptr<Block> solver_ptr ( new Block ( linearSolver));
std::unique_ptr<Block> solver_ptr ( new Block ( std::move(linearSolver))); // 矩阵块求解器
//g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( solver_ptr);
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( std::move(solver_ptr));
g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm ( solver );
编译并运行:
sudo clean
sudo cmake ..
sudo make
cd ..
./build/feature_extraction 1.png 2.png
运行截图:
特征点图:
所有匹配点对:
优化后匹配点对:
参考:
https://blog.csdn.net/github_39611196/article/details/81146323
https://blog.csdn.net/zhuiqiuzhuoyue583/article/details/92569771