Top Related Projects
A large scale non-linear optimization library
C++ implementation of Lie Groups using Eigen.
GTSAM is a library of C++ classes that implement smoothing and mapping (SAM) in robotics and vision, using factor graphs and Bayes networks as the underlying computing paradigm rather than sparse matrices.
The Kalibr visual-inertial calibration toolbox
Quick Overview
g2o is an open-source C++ framework for optimizing graph-based nonlinear error functions. It is particularly useful for applications in robotics and computer vision, such as SLAM (Simultaneous Localization and Mapping) and bundle adjustment. g2o provides a flexible and efficient implementation of graph optimization using various solvers and robust cost functions.
Pros
- Highly efficient and optimized for large-scale graph optimization problems
- Supports a wide range of solvers, including sparse linear solvers and nonlinear optimization methods
- Extensible architecture allowing users to implement custom vertices, edges, and solvers
- Well-documented with examples and tutorials for various use cases
Cons
- Steep learning curve for beginners due to its complex architecture and mathematical foundations
- Limited support for Windows platforms compared to Linux and macOS
- Requires external dependencies, which may complicate the build process for some users
- Documentation can be outdated or incomplete for some advanced features
Code Examples
- Creating a simple graph optimization problem:
#include <g2o/core/sparse_optimizer.h>
#include <g2o/core/block_solver.h>
#include <g2o/solvers/linear_solver_eigen.h>
#include <g2o/core/optimization_algorithm_levenberg.h>
g2o::SparseOptimizer optimizer;
auto linearSolver = g2o::make_unique<g2o::LinearSolverEigen<g2o::BlockSolverX::PoseMatrixType>>();
auto blockSolver = g2o::make_unique<g2o::BlockSolverX>(std::move(linearSolver));
auto algorithm = new g2o::OptimizationAlgorithmLevenberg(std::move(blockSolver));
optimizer.setAlgorithm(algorithm);
- Adding vertices and edges to the graph:
auto v1 = new g2o::VertexSE3Expmap();
v1->setId(0);
v1->setEstimate(g2o::SE3Quat());
optimizer.addVertex(v1);
auto v2 = new g2o::VertexSE3Expmap();
v2->setId(1);
v2->setEstimate(g2o::SE3Quat());
optimizer.addVertex(v2);
auto edge = new g2o::EdgeSE3Expmap();
edge->setVertex(0, v1);
edge->setVertex(1, v2);
edge->setMeasurement(g2o::SE3Quat());
edge->setInformation(Eigen::Matrix<double, 6, 6>::Identity());
optimizer.addEdge(edge);
- Optimizing the graph:
optimizer.initializeOptimization();
optimizer.optimize(10);
// Access optimized results
g2o::VertexSE3Expmap* v = dynamic_cast<g2o::VertexSE3Expmap*>(optimizer.vertex(0));
g2o::SE3Quat estimate = v->estimate();
Getting Started
-
Clone the repository:
git clone https://github.com/RainerKuemmerle/g2o.git
-
Install dependencies (Ubuntu example):
sudo apt-get install cmake libeigen3-dev libsuitesparse-dev qtdeclarative5-dev qt5-qmake libqglviewer-dev-qt5
-
Build g2o:
cd g2o mkdir build && cd build cmake .. make
-
Include g2o in your project's CMakeLists.txt:
find_package(g2o REQUIRED) target_link_libraries(your_target g2o::core)
Competitor Comparisons
A large scale non-linear optimization library
Pros of Ceres Solver
- More extensive documentation and tutorials
- Wider range of problem types supported, including non-linear least squares
- Better integration with other libraries and tools
Cons of Ceres Solver
- Steeper learning curve for beginners
- More complex setup and configuration process
- Potentially slower for small-scale problems
Code Comparison
g2o example:
g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm(new g2o::OptimizationAlgorithmLevenberg);
g2o::VertexSE3Expmap* pose = new g2o::VertexSE3Expmap();
optimizer.addVertex(pose);
Ceres Solver example:
ceres::Problem problem;
ceres::Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
Both libraries offer powerful optimization capabilities, but Ceres Solver provides more flexibility and features at the cost of increased complexity. g2o is generally easier to use for simpler problems, especially in robotics and computer vision applications.
C++ implementation of Lie Groups using Eigen.
Pros of Sophus
- Lightweight and focused on Lie groups and algebras
- Easier to integrate into existing projects due to its simplicity
- Header-only library, simplifying build processes
Cons of Sophus
- Limited scope compared to g2o's broader optimization capabilities
- Fewer features for complex graph-based optimizations
- Smaller community and less frequent updates
Code Comparison
Sophus example (transformation):
#include <sophus/se3.hpp>
Sophus::SE3d pose = Sophus::SE3d::rotX(0.1) * Sophus::SE3d::transY(1.0);
Eigen::Vector3d point(1, 2, 3);
Eigen::Vector3d transformed = pose * point;
g2o example (vertex and edge creation):
#include <g2o/core/sparse_optimizer.h>
#include <g2o/core/block_solver.h>
#include <g2o/solvers/linear_solver_dense.h>
g2o::SparseOptimizer optimizer;
auto linearSolver = g2o::make_unique<g2o::LinearSolverDense<BlockSolverType::PoseMatrixType>>();
auto blockSolver = g2o::make_unique<BlockSolverType>(std::move(linearSolver));
optimizer.setAlgorithm(new g2o::OptimizationAlgorithmLevenberg(std::move(blockSolver)));
GTSAM is a library of C++ classes that implement smoothing and mapping (SAM) in robotics and vision, using factor graphs and Bayes networks as the underlying computing paradigm rather than sparse matrices.
Pros of GTSAM
- More comprehensive documentation and tutorials
- Better support for factor graphs and Bayes trees
- Wider range of applications beyond SLAM, including robotics and computer vision
Cons of GTSAM
- Steeper learning curve due to its more abstract nature
- Less optimized for large-scale SLAM problems compared to g2o
Code Comparison
GTSAM example:
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
#include <gtsam/nonlinear/LevenbergMarquardtOptimizer.h>
#include <gtsam/slam/PriorFactor.h>
#include <gtsam/slam/BetweenFactor.h>
gtsam::NonlinearFactorGraph graph;
graph.add(PriorFactor<Pose2>(1, Pose2(0, 0, 0), priorNoise));
g2o example:
#include <g2o/core/sparse_optimizer.h>
#include <g2o/core/block_solver.h>
#include <g2o/solvers/linear_solver_eigen.h>
g2o::SparseOptimizer optimizer;
auto linearSolver = g2o::make_unique<LinearSolverEigen<BlockSolverType::PoseMatrixType>>();
optimizer.setAlgorithm(new OptimizationAlgorithmLevenberg(std::move(solver)));
Both libraries provide powerful optimization frameworks for SLAM and related problems. GTSAM offers a more flexible and abstract approach, while g2o is often preferred for its efficiency in large-scale SLAM applications. The choice between them depends on the specific requirements of your project and your familiarity with factor graphs versus graph-based optimization.
The Kalibr visual-inertial calibration toolbox
Pros of kalibr
- Specialized for camera-IMU calibration and multi-sensor calibration
- Provides comprehensive toolset for sensor calibration workflows
- Supports various camera models and IMU types
Cons of kalibr
- More focused on calibration tasks, less general-purpose than g2o
- May have a steeper learning curve for users not familiar with ROS
Code comparison
kalibr (Python):
imu_data = ImuData(imu_measurements, imu_timestamps)
cam_data = CameraData(images, timestamps)
calibrator = Calibrator(imu_data, cam_data)
results = calibrator.calibrate()
g2o (C++):
g2o::SparseOptimizer optimizer;
g2o::BlockSolver_6_3::LinearSolverType* linearSolver = new g2o::LinearSolverCholmod<g2o::BlockSolver_6_3::PoseMatrixType>();
g2o::BlockSolver_6_3* solver_ptr = new g2o::BlockSolver_6_3(linearSolver);
optimizer.setAlgorithm(new g2o::OptimizationAlgorithmLevenberg(solver_ptr));
The code snippets highlight the different focus areas of the two libraries. kalibr provides a higher-level interface for calibration tasks, while g2o offers a more general-purpose optimization framework.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
g2o - General Graph Optimization
g2o is an open-source C++ framework for optimizing graph-based nonlinear error functions. g2o has been designed to be easily extensible to a wide range of problems and a new problem typically can be specified in a few lines of code. The current implementation provides solutions to several variants of SLAM and BA.
A wide range of problems in robotics as well as in computer-vision involve the minimization of a non-linear error function that can be represented as a graph. Typical instances are simultaneous localization and mapping (SLAM) or bundle adjustment (BA). The overall goal in these problems is to find the configuration of parameters or state variables that maximally explain a set of measurements affected by Gaussian noise. g2o is an open-source C++ framework for such nonlinear least squares problems. g2o has been designed to be easily extensible to a wide range of problems and a new problem typically can be specified in a few lines of code. The current implementation provides solutions to several variants of SLAM and BA. g2o offers a performance comparable to implementations of state-of-the-art approaches for the specific problems (02/2011).
Python and updated memory management
The branch pymem contains a python wrapper and switches to smart pointer instead of RAW pointers. It is currently experimental but PRs and improvements are welcome - as always.
See g2o-python for the pypi release of g2o's python bindings.
Papers Describing the Approach
Rainer Kuemmerle, Giorgio Grisetti, Hauke Strasdat, Kurt Konolige, and Wolfram Burgard g2o: A General Framework for Graph Optimization IEEE International Conference on Robotics and Automation (ICRA), 2011
Documentation
A detailed description of how the library is structured and how to use and extend it can be found in /doc/g2o.pdf The API documentation can be generated as described in doc/doxygen/readme.txt
License
g2o is licensed under the BSD License. However, some libraries are available under different license terms. See below.
The following parts are licensed under LGPL v2.1+:
- csparse_extension
The following parts are licensed under GPL3+:
- g2o_viewer
- g2o_incremental
- slam2d_g2o (example for 2D SLAM with a QGLviewer GUI)
Please note that some features of CHOLMOD (which may be used by g2o, see libsuitesparse below) are licensed under the GPL. To avoid the GPL, you may have to re-compile CHOLMOD without including its GPL features. The CHOLMOD library distributed with, for example, Ubuntu or Debian includes the GPL features. For example, the supernodal factorization that is licensed under GPL is considered by g2o if it is available.
Within sub-folders we include software not written by us to guarantee easy compilation and integration into g2o itself.
-
ceres: BSD (see g2o/autodiff/LICENSE) Extracted headers to perform Automatic Differentiation.
-
freeglut: X-Consortium (see g2o/EXTERNAL/freeglut/COPYING) Copyright (c) 1999-2000 Pawel W. Olszta We use a stripped down version for drawing text in OpenGL.
See the doc folder for the full text of the licenses.
g2o is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the licenses for more details.
Requirements
- C++17 compiler (CI pipeline runs with gcc, clang and MSVC)
- cmake http://www.cmake.org
- Eigen3 http://eigen.tuxfamily.org
On Ubuntu / Debian these dependencies are resolved by installing the following packages.
- cmake
- libeigen3-dev
Optional requirements
- spdlog https://github.com/gabime/spdlog
- suitesparse http://faculty.cse.tamu.edu/davis/suitesparse.html
- Qt5 http://qt-project.org
- libQGLViewer http://www.libqglviewer.com
On Ubuntu / Debian these dependencies are resolved by installing the following packages.
- libspdlog-dev
- libsuitesparse-dev
- qtdeclarative5-dev
- qt5-qmake
- libqglviewer-dev-qt5
You can install those packages with the following command
sudo apt install libeigen3-dev libspdlog-dev libsuitesparse-dev qtdeclarative5-dev qt5-qmake libqglviewer-dev-qt5
Mac OS X
If using Homebrew, then
brew install g2o
will install g2o together with its required dependencies. In this case no manual compilation is necessary.
Windows
If using vcpkg, then
script\install-deps-windows.bat
or for full dependencies installation
script\install-additional-deps-windows.bat
will build and install the dependencies. The location of vcpkg
and required
triplet can be passed as cli arguments respectively. Note that usually vcpkg
will auto detect the triplet. Set it only if your are not using the default
build for your OS.
Compilation
Our primary development platform is Linux. Experimental support for Mac OS X, Android and Windows (MinGW or MSVC). We recommend a so-called out of source build which can be achieved by the following command sequence.
mkdir build
cd build
cmake ../
make
The binaries will be placed in bin and the libraries in lib which are both located underneath cmake's build folder.
On Windows with vcpkg
the following commands will generate build scripts (please change the Visual Studio version number in accordance with your system):
mkdir build
cd build
cmake -DG2O_BUILD_APPS=ON -DG2O_BUILD_EXAMPLES=ON-DVCPKG_TARGET_TRIPLET="%VCPKG_DEFAULT_TRIPLET%" -DCMAKE_TOOLCHAIN_FILE="%VCPKG_ROOT_DIR%\scripts\buildsystems\vcpkg.cmake" ..`
cmake --build . --target ALL_BUILD
If you are compiling on Windows and you are for some reasons not using vcpkg
please download Eigen3 and extract it.
Within cmake-gui set the variable EIGEN3_INCLUDE_DIR to that directory.
mkdir build
cd build
cmake .. -DG2O_BUILD_APPS=ON -DG2O_BUILD_EXAMPLES=ON -DEIGEN3_INCLUDE_DIR="<THE_PATH_WHERE_YOU_PLACED_EIGEN3_AND_THE_EIGEN3_CMakeLists.txt>"
Cross-Compiling for Android
mkdir build`
cd build`
cmake -DCMAKE_TOOLCHAIN_FILE=../script/android.toolchain.cmake -DANDROID_NDK=<YOUR_PATH_TO_ANDROID_NDK_r10d+> -DCMAKE_BUILD_TYPE=Release -DANDROID_ABI="armeabi-v7a with NEON" -DEIGEN3_INCLUDE_DIR="<YOUR_PATH_TO_EIGEN>" -DEIGEN3_VERSION_OK=ON ..
cmake --build .
Acknowledgments
We thank the following contributors for providing patches:
- Simon J. Julier: patches to achieve compatibility with Mac OS X and others.
- Michael A. Eriksen for submitting patches to compile with MSVC.
- Mark Pupilli for submitting patches to compile with MSVC.
Projects using g2o
- g2o-python: Python binding which is also installable via
pip
- .Net wrapper
Top Related Projects
A large scale non-linear optimization library
C++ implementation of Lie Groups using Eigen.
GTSAM is a library of C++ classes that implement smoothing and mapping (SAM) in robotics and vision, using factor graphs and Bayes networks as the underlying computing paradigm rather than sparse matrices.
The Kalibr visual-inertial calibration toolbox
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot