g2o(General Graphic Optimization)是基于图优化的库。图优化是把优化问题表现成图的一种方式。一个图由若干个顶点(Vertex),以及连接这这些顶点的边(Edge)组成。用顶点表示优化变量,用边表示误差项。

对任意非线性最小二乘问题都可以构建一个与之对应的图。也可以用概率图里的定义,称之为贝叶斯图或因子图。

以相机为例,相机不同时刻的位姿和路标点可以表示为顶点,相机的运动模型可以表示相机之间的边,相机的观测模型可以表示相机与路标之间的边。 如下关系图:

Untitled

g2o-20230223_git.zip

mkdir build && cd build 
cmake ..
make -j4
sudo make install

在安装完该g2o之后 运行一些程序 如高翔的ch6 代码会出现如下错误

CMake Warning at CMakeLists.txt:10 (FIND_PACKAGE):
  By not providing "FindG2O.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "G2O", but
  CMake did not find one.

  Could not find a package configuration file provided by "G2O" with any of
  the following names:

    G2OConfig.cmake
    g2o-config.cmake

  Add the installation prefix of "G2O" to CMAKE_PREFIX_PATH or set "G2O_DIR"
  to a directory containing one of the above files.  If "G2O" provides a
  separate development package or SDK, be sure it has been installed.

解决方式很简单: 主要是CMakeLists.txt上要修改几行代码

本来是这样的就会报错,

list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ) 
find_package(G2O REQUIRED) 
include_directories( 
${G2O_INCLUDE_DIRS} ) 

修改为:

list( APPEND CMAKE_MODULE_PATH /home/×××/g2o/cmake_modules ) 
set(G2O_ROOT /usr/local/include/g2o) 
find_package(G2O REQUIRED) 
include_directories( 
${G2O_INCLUDE_DIRS} ) 

然后再cmake便不会出现问题了。

主要原因就是原始代码中

${PROJECT_SOURCE_DIR}/cmake

是ch6中的文件含有的cmake文件夹中FindG2O.cmake在你的文件夹不存在,只要把list那行路径改到FindG2O.cmake存在的文件夹就行。