ceres的學習筆記

2021-10-23 20:15:36 字數 4254 閱讀 5689

剛剛在專案中使用的到了ceres,從完全不了解、到安裝使用解決問題,花費了整整乙個周的時間。所以使用的時候過程中踩了不少的坑,所以特別想記錄下來。

ceres solver 是谷歌開發的一款用於非線性優化的庫,在谷歌的開源雷射雷達slam專案cartographer中被大量使用。ceres的官網比較詳細的使用教程,內容還是挺豐富。

ceres主要用於解決如下的非線性最小二乘的問題

依賴ceres依賴挺多開源庫的,但並不是必須的,可以在構建時關閉相應的依賴。

命令列安裝

安裝依賴

$ sudo

apt-get

install cmake

# google-glog + gflags

$ sudo

apt-get

install libgoogle-glog-dev

# blas & lapack

$ sudo

apt-get

install libatlas-base-dev

# eigen3

$ sudo

apt-get

install libeigen3-dev

# suitesparse and cxsparse (optional)

$ sudo

apt-get

install libsuitesparse-dev

# - however, if you want to build ceres as a *shared* library, you must

# add the following ppa:

$ sudo add-apt-repository ppa:bzindovic/suitesparse-bugfix-1319687

$ sudo

apt-get update

$ sudo

apt-get

install libsuitesparse-dev

編譯ceres依賴eigen和glog,因此需要先拉取eigen和glog。

# 拉取glog

$ git clone

# 修改option

# option (with_gflags "use gflags" off)

$ vi cmakelists.txt

# 開始編譯

$ cmake ..

$ make -j4

$ make destdir=./output install

# 拉取eigen

$ git clone

# 拉取ceres

$ git clone

$ cd ceres-solver

# 專案需求,切換到1.13.0版本

$ git checkout 1.13.0

$ git branch

* (head detached at 1.13.0)

# 根據實際需求,修改option

# option(gflags "enable google flags." off)

# option(lapack "enable use of lapack." off)

# option(openmp "enable threaded solving in ceres (requires openmp)" off)

# option(build_testing "enable tests" off)

# option(build_examples "build examples" off)

vi cmakelist.txt

$ mkdir build &&

cd build

# 開始編譯

$ cmake ..

$ make -j4

$ make destdir=./output install

$ ls

build cmakelists.txt include lib test1.cpp

$ ls include

ceres eigen glog

$ ls lib

libceres.a libglog.a

test1.cpp是源自ceres官網的乙個例子:
#include

#include

using

namespace std;

using

namespace ceres;

struct costfunctor};

intmain

(int argc,

char

**ar**)

cmakelist.txt
$ cat cmakelist.txt

cmake_minimum_required(version 3.4)

project(samples cxx)

set(cmake_c_standard 11)

set(cmake_c_standard_required on)

set(cmake_c_extensions off)

# this project requires c++11.

set(cmake_cxx_standard 11)

set(cmake_cxx_standard_required on)

set(cmake_cxx_extensions off)

set(cmake_thread_prefer_pthread true)

set(threads_prefer_pthread_flag true)

find_package (threads required)

add_definitions(-d_glibcxx_use_cxx11_abi=0)

add_compile_options(-wall)

file(glob samples_src_list $/*.cpp)

message(

"$")

link_directories(

$/lib/)

foreach(src $

) string(regex match "[^/]+$" src_file $

) string(replace ".cpp"

"" exe_file_name $

) set(exe_target $_$

) add_executable(

$_$$

) target_include_directories(

$_$ public include)

target_link_libraries(

$_$$

ceres

glog

)install(targets $_$ runtime destination $/bin)

endforeach(src)

測試
$ ./samples_test1 

iter cost cost_change |gradient|

|step| tr_ratio tr_radius ls_iter iter_time total_time

0 1.250000e+01 0.00e+00 5.00e+00 0.00e+00 0.00e+00 1.00e+04 0 7.20e-05 1.33e-04

1 1.249750e-07 1.25e+01 5.00e-04 5.00e+00 1.00e+00 3.00e+04 1 6.20e-05 2.84e-04

2 1.388518e-16 1.25e-07 1.67e-08 5.00e-04 1.00e+00 9.00e+04 1 2.19e-05 3.28e-04

ceres solver report: iterations: 3, initial cost: 1.250000e+01, final cost: 1.388518e-16, termination: convergence

x : 5 -> 10

Ceres學習筆記之CMakeLists寫法總結

高博說 不要長期徘徊在自己的舒適區里猶豫不決,這樣是沒有進步的。這句話開啟了我的slam後端優化學習之路。本文從cmakelists開始,總結常用的各個庫在cmakelists中的寫法。眾人 哇!又是這麼小兒科的嗎?博主 哎呀 不要這樣講嘛 qwq我會一直努力的 通常的cmakelists結構是 c...

基於aloam研究ceres的構建方式

aloam相比於loam增加了ceres庫自動微分,而不需要手推高斯牛頓,這是非常方便的。對於aloam原始碼,在laserodometry中,有關ceres的部分 ceres lossfunction loss function new ceres huberloss 0.1 魯棒核 ceres ...

如何用ceres進行兩幀之間的BA優化

學習高博的書已有很長一段時間了,一直看理論,看 而沒有自己親自上手,最近在做ba優化,大部分slam是用g2o進行的,而對於ceres用的很少,由於博主根本看不懂g2o的 風格,個人覺得很無語 其實是博主zz 那咋辦?於是就想乾脆用ceres實現ba優化吧。而關於ceres,其實主要還是殘差的定義了...