??★,°:.☆( ̄▽ ̄)/$:.°★ ??
這篇文章主要介紹ceres和g2o非線性優(yōu)化庫配置使用。
無專精則不能成,無涉獵則不能通?!簡⒊?/font>
歡迎來到我的博客,一起學(xué)習(xí),共同進(jìn)步。
喜歡的朋友可以關(guān)注一下,下次更新不迷路??
??1. 項(xiàng)目介紹
ceres
項(xiàng)目Github地址:https://github.com/ceres-solver/ceres-solver
g2o
項(xiàng)目Github地址:https://github.com/RainerKuemmerle/g2o
Ceres Solver和g2o都是用于求解非線性最小二乘問題的C++庫,主要用于圖優(yōu)化等領(lǐng)域。它們有一些共同點(diǎn),但也有一些區(qū)別。
Ceres Solver:
- Ceres Solver是一個(gè)功能強(qiáng)大的C++庫,專門用于求解大規(guī)模稀疏和稠密非線性最小二乘問題。
- 它支持各種類型的誤差函數(shù),如光束法平差、非線性回歸、SLAM、視覺定位等。
- Ceres Solver提供了多種優(yōu)化算法,包括LM(Levenberg-Marquardt)、GN(Gauss-Newton)等,并且可根據(jù)問題特點(diǎn)進(jìn)行自定義優(yōu)化策略。
- 它具有靈活的接口和標(biāo)準(zhǔn)化的問題表示方式,可以輕松地與其他庫進(jìn)行集成。
- Ceres Solver支持自動求導(dǎo),可以通過使用用戶提供的誤差函數(shù)的解析梯度或數(shù)值微分來計(jì)算導(dǎo)數(shù)。
- Ceres Solver是開源的,遵循BSD許可證。
g2o:
- g2o是一個(gè)通用的C++庫,用于求解圖優(yōu)化問題,例如視覺SLAM、3D重建、機(jī)器人運(yùn)動估計(jì)等。
- g2o支持稀疏矩陣和濾波器算法,并提供了靈活的接口和模塊化設(shè)計(jì)。
- 它支持多種頂點(diǎn)和邊類型,并允許用戶自定義頂點(diǎn)、邊類型和優(yōu)化策略。
- g2o提供了多種優(yōu)化算法,如GN(Gauss-Newton)、LM(Levenberg-Marquardt)等。
- g2o也是開源的,遵循BSD許可證。
Ceres Solver和g2o在SLAM、機(jī)器人運(yùn)動估計(jì)等領(lǐng)域得到了廣泛應(yīng)用。
??2. 環(huán)境配置
下面進(jìn)行環(huán)境配置:
ceres:
# 安裝依賴
sudo apt install cmake libgoogle-glog-dev libgflags-dev libatlas-base-dev libsuitesparse-dev -y
# ceres-1.14
wget ceres-solver.org/ceres-solver-1.14.0.tar.gz
tar -zxvf ceres-solver-1.14.0.tar.gz
cd ceres-solver-1.14.0
mkdir build && cd build
cmake .. && make
sudo make install
編譯:g++ -o main main.cpp -lceres -lglog && ./main
g2o:
# 安裝依賴
sudo apt-get install libeigen3-dev libsuitesparse-dev qt5-qmake libqglviewer-dev-qt5
git clone https://github.com/RainerKuemmerle/g2o.git
cd g2o
mkdir build && cd build
cmake .. && make
sudo make install
??3. 使用說明
下面進(jìn)行使用分析:
ceres:
構(gòu)建代價(jià)函數(shù)Cost_Functor:
// 定義一個(gè)實(shí)例化時(shí)才知道的類型T
template <typename T>
// 運(yùn)算符()的重載,用來得到殘差fi
bool operator()(const T* const x, T* residual) const {
residual[0] = T(10.0) - x[0];
return true;
}
構(gòu)建最小二乘問題problem:
Problem problem;
CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
problem.AddResidualBlock(cost_function, NULL, &x);
求解器參數(shù)配置Solver:
Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary);
cout << summary.BriefReport() << "\n";//輸出優(yōu)化的簡要信息
用Ceres Solver庫解決一個(gè)簡單的非線性最小二乘問題示例:
#include <iostream>
#include <ceres/ceres.h>
// 代價(jià)函數(shù)類定義
struct CostFunctor {
template <typename T>
bool operator()(const T* const x, T* residual) const {
// 定義目標(biāo)函數(shù):f(x) = 10 - x
residual[0] = T(10.0) - x[0];
return true;
}
};
int main(int argc, char** argv) {
// 初始化問題
ceres::Problem problem;
// 添加一個(gè)殘差塊
double initial_x = 5.0; // 初始值
ceres::CostFunction* cost_function =
new ceres::AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
problem.AddResidualBlock(cost_function, nullptr, &initial_x);
// 配置求解器選項(xiàng)
ceres::Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
options.minimizer_progress_to_stdout = true;
// 求解問題
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
// 打印結(jié)果
std::cout << summary.BriefReport() << "\n";
std::cout << "Final x = " << initial_x << "\n";
return 0;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-634367.html
以上。文章來源地址http://www.zghlxwxcb.cn/news/detail-634367.html
到了這里,關(guān)于【C++】開源:ceres和g2o非線性優(yōu)化庫配置使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!