SLAM14讲 ch6 g2o曲线拟合程序问题

这一讲包含了一个用g2o库进行曲线拟合的实例,但是在按照书中实际步骤实际运行发现了几个问题。

(1)g2o库的的依赖项安装

书中所写命令如下:

sudo apt-get install libqt4-dev qt4-qmake libqglviewer-dev libsuitesparse-dev libcxsparse3.1.2 libcholmod-dev

但是最后一项(libcholmod-dev)会提示不能安装,此时应该用tab键进行补全完成安装

(2)编译出错

在使用cmake编译中,出现了以下报错:

 1  By not providing "FindG2O.cmake" in CMAKE_MODULE_PATH this project has
 2   asked CMake to find a package configuration file provided by "G2O", but
 3   CMake did not find one.
 4 
 5   Could not find a package configuration file provided by "G2O" with any of
 6   the following names:
 7 
 8     G2OConfig.cmake
 9     g2o-config.cmake
10 
11   Add the installation prefix of "G2O" to CMAKE_PREFIX_PATH or set "G2O_DIR"
12   to a directory containing one of the above files.  If "G2O" provides a
13   separate development package or SDK, be sure it has been installed.

原因在于CMakeLists.txt中,虽然定义了CMAKE_MODULE_PATH,但没有对应的文件和文件夹,也就是没有找到FindG2O.cmake文件

解决办法:找到findg2o.cmake文件的位置,一般在g2o安装包中的"cmake_modules"文件夹中,将整个文件夹复制到相应工程的下面,与build文件夹在同一目录,然后编译可以正常通过

*(3)CMakeLists.txt文件问题

高博在GitHub中给出的代码如下:

 1 cmake_minimum_required( VERSION 2.8 )
 2 project( g2o_curve_fitting )
 3 
 4 set( CMAKE_BUILD_TYPE "Release" )
 5 set( CMAKE_CXX_FLAGS "-std=c++11 -O3" )
 6 
 7 # 添加cmake模块以使用ceres库
 8 list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules )
 9 
10 # 寻找G2O
11 find_package( G2O REQUIRED )
12 include_directories( 
13     ${G2O_INCLUDE_DIRS}
14     "/usr/include/eigen3"
15 )
16 
17 # OpenCV
18 find_package( OpenCV REQUIRED )
19 include_directories( ${OpenCV_DIRS} )
20 
21 add_executable( curve_fitting main.cpp )
22 # 与G2O和OpenCV链接
23 target_link_libraries( curve_fitting 
24     ${OpenCV_LIBS}
25     g2o_core g2o_stuff
26 )

由于项目名定的是g2o_curve_fitting,因此需要修改最后的target_link_libraries(g2o_curve_fitting ${OpenCV_LIBS} g2o_core g2o_stuff)

(这个只是我遇见的问题,如果还有其他的问题,欢迎一起交流讨论)

最后整个代码的运行结果如下

generating data: 
0 2.71828
0.01 2.93161
0.02 2.12942
0.03 2.46037
0.04 4.18814
0.05 2.73368
0.06 2.42751
0.07 3.44729
0.08 3.72543
0.09 2.1358
0.1 4.12333
0.11 3.38199
0.12 4.81164
0.13 1.62582
0.14 1.76862
0.15 3.21555
0.16 3.0922
0.17 5.82752
0.18 4.29855
0.19 2.74081
0.2 5.75724
0.21 3.53729
0.22 1.95514
0.23 2.99195
0.24 3.28739
0.25 4.70749
0.26 6.24365
0.27 5.81645
0.28 4.88402
0.29 4.75991
0.3 7.25246
0.31 5.92933
0.32 7.00306
0.33 5.22286
0.34 5.16179
0.35 7.26191
0.36 6.40545
0.37 6.25549
0.38 6.56094
0.39 6.53523
0.4 8.14891
0.41 7.77616
0.42 7.40141
0.43 8.75638
0.44 7.20606
0.45 7.57795
0.46 8.21564
0.47 9.84032
0.48 6.96725
0.49 9.90619
0.5 9.27125
0.51 9.87567
0.52 10.3412
0.53 9.55315
0.54 11.3635
0.55 10.8815
0.56 13.0648
0.57 11.4756
0.58 11.337
0.59 13.2393
0.6 13.5299
0.61 14.0441
0.62 13.31
0.63 13.672
0.64 14.8504
0.65 14.2599
0.66 14.7724
0.67 17.4339
0.68 17.4632
0.69 17.7598
0.7 16.8223
0.71 19.9468
0.72 20.5446
0.73 21.3767
0.74 20.1435
0.75 20.3088
0.76 23.2543
0.77 23.4349
0.78 22.8706
0.79 24.094
0.8 25.4183
0.81 25.5237
0.82 27.9738
0.83 28.5861
0.84 29.5703
0.85 29.6744
0.86 32.667
0.87 34.2698
0.88 33.5124
0.89 36.1479
0.9 39.2485
0.91 40.988
0.92 41.5716
0.93 41.3686
0.94 44.285
0.95 42.8312
0.96 47.7941
0.97 48.5931
0.98 51.8487
0.99 51.0258
start optimization
iteration= 0     chi2= 30373.727656     time= 0.000163358     cumTime= 0.000163358     edges= 100     schur= 0     lambda= 699.050482     levenbergIter= 7
iteration= 1     chi2= 13336.948288     time= 5.5717e-05     cumTime= 0.000219075     edges= 100     schur= 0     lambda= 1864.134619     levenbergIter= 3
iteration= 2     chi2= 6946.262996     time= 3.5332e-05     cumTime= 0.000254407     edges= 100     schur= 0     lambda= 1242.756412     levenbergIter= 1
iteration= 3     chi2= 271.023166     time= 3.5225e-05     cumTime= 0.000289632     edges= 100     schur= 0     lambda= 414.252137     levenbergIter= 1
iteration= 4     chi2= 118.903887     time= 4.2411e-05     cumTime= 0.000332043     edges= 100     schur= 0     lambda= 138.084046     levenbergIter= 1
iteration= 5     chi2= 113.568660     time= 3.5035e-05     cumTime= 0.000367078     edges= 100     schur= 0     lambda= 46.028015     levenbergIter= 1
iteration= 6     chi2= 107.476457     time= 4.2275e-05     cumTime= 0.000409353     edges= 100     schur= 0     lambda= 15.342672     levenbergIter= 1
iteration= 7     chi2= 103.014522     time= 3.5151e-05     cumTime= 0.000444504     edges= 100     schur= 0     lambda= 5.114224     levenbergIter= 1
iteration= 8     chi2= 101.988348     time= 4.9545e-05     cumTime= 0.000494049     edges= 100     schur= 0     lambda= 1.704741     levenbergIter= 1
iteration= 9     chi2= 101.937388     time= 4.9565e-05     cumTime= 0.000543614     edges= 100     schur= 0     lambda= 0.568247     levenbergIter= 1
iteration= 10     chi2= 101.937021     time= 2.6884e-05     cumTime= 0.000570498     edges= 100     schur= 0     lambda= 0.378831     levenbergIter= 1
iteration= 11     chi2= 101.937020     time= 3.4683e-05     cumTime= 0.000605181     edges= 100     schur= 0     lambda= 0.252554     levenbergIter= 1
iteration= 12     chi2= 101.937020     time= 3.5072e-05     cumTime= 0.000640253     edges= 100     schur= 0     lambda= 0.168370     levenbergIter= 1
iteration= 13     chi2= 101.937020     time= 6.239e-05     cumTime= 0.000702643     edges= 100     schur= 0     lambda= 3678.088107     levenbergIter= 6
iteration= 14     chi2= 101.937020     time= 5.4525e-05     cumTime= 0.000757168     edges= 100     schur= 0     lambda= 19616.469906     levenbergIter= 3
iteration= 15     chi2= 101.937020     time= 4.9616e-05     cumTime= 0.000806784     edges= 100     schur= 0     lambda= 836969.382664     levenbergIter= 4
iteration= 16     chi2= 101.937020     time= 5.7449e-05     cumTime= 0.000864233     edges= 100     schur= 0     lambda= 224672257893341.656250     levenbergIter= 7
solve time cost = 0.0013663 seconds. 
estimated model: 0.890911   2.1719 0.943629

猜你喜欢

转载自www.cnblogs.com/qi-zhang/p/9365199.html