Ubuntu安装使用Ceres Solver

9 篇文章 0 订阅
9 篇文章 0 订阅

【版权声明】
本文为博主原创文章,未经博主允许严禁转载,我们会定期进行侵权检索。  

        Ceres Solver是一个开源的C++库,用于建模和求解大型复杂的非线性最小二乘问题。它被广泛应用于计算机视觉、机器人学、统计学等领域,特别是在三维重建、相机标定、导航、地图制作等任务中。Ceres Solver由谷歌研发并维护,因其强大的功能和灵活性而受到许多研究者和工程师的青睐。

1、核心特性

        自动微分:Ceres提供自动微分功能,可以自动计算给定函数的导数,这对于最小二乘问题的求解至关重要。

        多种求解器支持:Ceres内置了多种线性求解器和非线性求解策略,包括稀疏和密集矩阵的处理方法,用户可以根据问题特点选择最合适的求解器。

        鲁棒核函数:为了处理离群点对最小二乘优化的影响,Ceres提供了多种鲁棒核函数,帮助改善优化结果的稳定性和准确性。

        高效:Ceres针对性能进行了优化,能够高效地处理大规模的优化问题。

        易用性:尽管Ceres是一个功能强大的库,但它的API设计得非常易于使用,使得用户可以相对容易地构建和求解最小二乘问题。

2、安装

        官方安装地址:Installation — Ceres Solverhttp://ceres-solver.org/installation.html

        Ceres Solver可以在多种操作系统上安装,包括Linux、macOS和Windows。以下是在Ubuntu上安装Ceres的基本步骤:

        2.1 安装依赖

        首先需要安装Ceres的依赖库,包括CMake、Google glog、Eigen等。

sudo apt-get update
sudo apt-get install  liblapack-dev libsuitesparse-dev libcxsparse3 libgflags-dev libgoogle-glog-dev libgtest-dev libatlas-base-dev libeigen3-dev

        2.2 下载Ceres Solver

        从Ceres Solver的官方网站或GitHub仓库下载源代码。

git clone https://github.com/ceres-solver/ceres-solver

        2.3 编译和安装

        使用CMake构建并安装Ceres。

cd ceres-solver
mkdir build
cd build
cmake ..
make -j
sudo make install

        2.4 编译失败案例

        大部分编译失败案例需要是由安装版本与系统环境中依赖项的版本不一致导致的,往往需要降低Ceres版本,如参考文献[1]。

        如果出现错误“internal/ceres/CMakeFiles/ceres_internal.dir/cuda_sparse_matrix.cc.o”,那么可能是因为glog版本导致的。理论上glog版本至少为0.3.5即可,但是编译过程中仍然出现了以上问题。报错完整内容如下所示。

internal/ceres/CMakeFiles/ceres_internal.dir/build.make:1083: recipe for target 'internal/ceres/CMakeFiles/ceres_internal.dir/cuda_sparse_matrix.cc.o' failed
make[2]: *** [internal/ceres/CMakeFiles/ceres_internal.dir/cuda_sparse_matrix.cc.o] Error 1
/root/project/cpp_project/ceres-solver/internal/ceres/cuda_kernels_bsm_to_crs.cu.cc(135): warning #2912-D: constexpr if statements are a C++17 feature
/root/project/cpp_project/ceres-solver/internal/ceres/cuda_kernels_bsm_to_crs.cu.cc(135): warning #2912-D: constexpr if statements are a C++17 feature
/root/project/cpp_project/ceres-solver/internal/ceres/cuda_kernels_bsm_to_crs.cu.cc(135): warning #2912-D: constexpr if statements are a C++17 feature
/root/project/cpp_project/ceres-solver/internal/ceres/cuda_kernels_bsm_to_crs.cu.cc(135): warning #2912-D: constexpr if statements are a C++17 feature
/root/project/cpp_project/ceres-solver/internal/ceres/cuda_kernels_bsm_to_crs.cu.cc(135): warning #2912-D: constexpr if statements are a C++17 feature
[ 26%] Linking CUDA static library ../../lib/libceres_cuda_kernels.a
[ 26%] Built target ceres_cuda_kernels
CMakeFiles/Makefile2:2071: recipe for target 'internal/ceres/CMakeFiles/ceres_internal.dir/all' failed
make[1]: *** [internal/ceres/CMakeFiles/ceres_internal.dir/all] Error 2
Makefile:145: recipe for target 'all' failed
make: *** [all] Error 2

        上述问题解决方式是重新安装glog。不能再通过apt-get install libgoogle-glog-dev方法安装,这种方法很可能安装的是低版本glog,且无法制定版本。因此,下面采用源码编译的方式来进行安装。步骤如下所示。

sudo apt-get remove libgoogle-glog-dev
git clone https://gitee.com/boxingcao/glog.git
apt-get install autoconf automake libtool
cd glog
mkdir build
cd build
cmake ..
make
sudo make install

3、示例程序

        以下是一个简单的Ceres Solver示例,演示如何使用Ceres求解一个简单的最小二乘问题:

#include <ceres/ceres.h>
#include <iostream>

// 定义一个代价函数
struct CostFunctor {
    template <typename T>
    bool operator()(const T* const x, T* residual) const {
        residual[0] = 10.0 - x[0];
        return true;
    }
};

int main(int argc, char** argv) {
    google::InitGoogleLogging(argv[0]);

    // 初始值
    double initial_x = 5.0;
    double x = initial_x;

    // 构建最小二乘问题
    ceres::Problem problem;
    ceres::CostFunction* cost_function =
        new ceres::AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
    problem.AddResidualBlock(cost_function, nullptr, &x);

    // 配置并运行求解器
    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);

    std::cout << summary.BriefReport() << "\n";
    std::cout << "x : " << initial_x << " -> " << x << "\n";

    return 0;
}

        这个简单的例子展示了如何定义一个代价函数,创建一个最小二乘问题,配置求解器并运行求解过程,最后输出优化结果。Ceres Solver的灵活性和强大功能使其成为解决非线性最小二乘问题的首选库之一。

4、CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(hello_world)

find_package(Ceres REQUIRED)
message("Ceres: " ${CERES_LIBRARIES})
message("Ceres_DIR: " ${Ceres_DIR})

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    /usr/local/include
    /usr/include/
    /usr/include/eigen3
)

set(CMAKE_BUILD_TYPE "Debug") # 默认是Release模式,设置为Debug才能调试
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) # 设置可执行文件的输出目录
 
add_executable(hello_world test_ceres.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE Ceres::ceres)

5、运行结果

6、参考文献

[1] https://blog.csdn.net/qq_39369161/article/details/129089592

【版权声明】
本文为博主原创文章,未经博主允许严禁转载,我们会定期进行侵权检索。  

更多python与C++技巧、三维算法、深度学习算法总结、大模型请关注我的博客:https://blog.csdn.net/suiyingy,或”乐乐感知学堂“公众号。Python三维领域专业书籍推荐:《人工智能点云处理及深度学习算法》。

  • 18
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding的叶子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值