ceres_solver

1:google 官网 http://ceres-solver.org/

2:一篇中文博客讲的很好:http://m.blog.csdn.net/HUAJUN998/article


简单的总结一下ceres solver

google的ceres-slover 个人感觉和g2o差不多。

(1)最重要的就是构建CostFunction。根据选择的微分模型的不同有三种构建方式(自动微分,数值微分,手动微分)

          1:对于AutoDiffCostFunction类型的CostFunction,我们构造一个结构体,重写template operator(),注意类型为模板类型,重新定义了()函数,将结构体作为AutoDiffCostFunction的参数。

        

// structstruct CostFunctor { 
  template <typename T>   
  bool operator()(const T* const x, T* residual) const {    
  residual[0] = T(10.0) - x[0];     return true;   }};
// make CostFunction
CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
problem.AddResidualBlock(cost_function, NULL, &x);

      2:对于NumericDiffCostFunction类型的CostFunction,与AutoDiffCostFunction类似,只不过将结构体的接收类型不再是模板类型,用double类型代替了模板类型

      

// struct
struct NumericDiffCostFunctor {
  bool operator()(const double* const x, double* residual) const {
    residual[0] = 10.0 - x[0];
    return true;
  }
};
// make CostFunction
CostFunction* cost_function =
new NumericDiffCostFunction<NumericDiffCostFunctor, ceres::CENTRAL, 1, 1>(
  new NumericDiffCostFunctor);
problem.AddResidualBlock(cost_function, NULL, &x);
      3:  在有些情况下,不使用AutoDiffCostFunction,例如我们用近似的方式计算导数,而不是用AutoDiff的链式法则,我们需要自己的残差和Jacobin计算。这时我们定义一个CostFunction或者SizedCostFunction的子类。

class QuadraticCostFunction : public ceres::SizedCostFunction<1, 1> {
 public:
  virtual ~QuadraticCostFunction() {}
  virtual bool Evaluate(double const* const* parameters,
                        double* residuals,
                        double** jacobians) const {
    const double x = parameters[0][0];
    residuals[0] = 10 - x;

    // Compute the Jacobian if asked for.
    if (jacobians != NULL && jacobians[0] != NULL) {
      jacobians[0][0] = -1;
    }
    return true;
  }
};
基本都用自动微分(链式求导法则)。数值微分是有线性误差的,而且这也会导致收敛比较慢。手动求导容易出错(g2o用的就是手动求导,有没有感觉很麻烦)
(2)添加误差项Problem.AddResidualBlock(cost_fuction,NULL/loss_function,input_param1,input_param2,...)

中loss_function的目的是排除外点。即误差较大的项被剔除。除此之外,还有个函数和Problem.AddResidualBlock类似

void Problem::AddParameterBlock(double *values, int size, LocalParameterization*local_parameterization)
void Problem::AddParameterBlock(double *values, int size)
这个函数的目的是告诉Problem在目标函数中有哪些是变量。其实这个可以不添加。Google的官网有说明:

The user has the option of explicitly adding the parameter blocks using AddParameterBlock. This causes additional correctness checking; however, AddResidualBlock implicitly adds the parameter blocks if they are not present, so calling AddParameterBlock explicitly is not required.

但也不是完全没用,比如要固定一些变量,就需要设定Problem::SetParametersBlockConstant(&x),x为你要设定的固定变量。

这里还需要注意的是在构建cost_function时,例如

CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1,1>(new CostFunctor); CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 1,1>(new CostFunctor); 
这里的第一个1代表误差项的个数,第二个1代表 input_param1的 维数。第三个1代表input_param2的维数。即cost_function 要和AddResidualBlock相对应


(3)求解器的构建

  Solver::Options options;
  options.minimizer_progress_to_stdout = true;
  Solver::Summary summary;
  Solve(options, &problem, &summary);
以上几步骤是必须的。以下部分是根据需要选择的部分(主要是BA时的一些选项)

DEFINE_string(trust_region_strategy, "levenberg_marquardt",
              "Options are: levenberg_marquardt, dogleg.");
DEFINE_string(dogleg, "traditional_dogleg", "Options are: traditional_dogleg,"
              "subspace_dogleg.");

DEFINE_bool(inner_iterations, false, "Use inner iterations to non-linearly "
            "refine each successful trust region step.");

DEFINE_string(blocks_for_inner_iterations, "automatic", "Options are: "
            "automatic, cameras, points, cameras,points, points,cameras");

DEFINE_string(linear_solver, "sparse_schur", "Options are: "
              "sparse_schur, dense_schur, iterative_schur, sparse_normal_cholesky, "
              "dense_qr, dense_normal_cholesky and cgnr.");
DEFINE_bool(explicit_schur_complement, false, "If using ITERATIVE_SCHUR "
            "then explicitly compute the Schur complement.");
DEFINE_string(preconditioner, "jacobi", "Options are: "
              "identity, jacobi, schur_jacobi, cluster_jacobi, "
              "cluster_tridiagonal.");
DEFINE_string(visibility_clustering, "canonical_views",
              "single_linkage, canonical_views");

DEFINE_string(sparse_linear_algebra_library, "suite_sparse",
              "Options are: suite_sparse and cx_sparse.");
DEFINE_string(dense_linear_algebra_library, "eigen",
              "Options are: eigen and lapack.");
DEFINE_string(ordering, "automatic", "Options are: automatic, user.");

DEFINE_bool(use_quaternions, false, "If true, uses quaterni
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果您在Ubuntu 18.04系统上安装ceres-solver,您可以使用以下步骤进行安装: 1. 更新您的系统: ``` sudo apt-get update sudo apt-get upgrade ``` 2. 安装必要的软件包: ``` sudo apt-get install libatlas-base-dev libsuitesparse-dev ``` 3. 下载并安装Ceres Solver: ``` wget http://ceres-solver.org/ceres-solver-1.14.0.tar.gz tar xvf ceres-solver-1.14.0.tar.gz cd ceres-solver-1.14.0 mkdir build cd build cmake .. make sudo make install ``` 4. 检查安装是否成功: ``` ceres_version ``` 如果它输出了Ceres Solver的版本号,则说明安装成功。 请注意,在安装过程中,您可能需要先安装一些其他的依赖项,如果您在安装过程中遇到问题,请检查您的系统是否缺少任何必要的软件包。 ### 回答2: Ubuntu18.04是一种广泛使用的开源操作系统,而Ceres-solver则是一种用于最小化非线性代数方程的C ++库,被广泛应用于计算机视觉和机器人领域。在本文中,我们将探讨如何在Ubuntu18.04上安装Ceres-solver。 方法一:通过软件包管理器安装 Ubuntu18.04已经包含了Ceres-solver,因此我们可以通过软件包管理器轻松地安装它。具体步骤如下: 1. 通过终端窗口打开软件包管理器: ``` sudo apt-get update sudo apt-get install libceres-dev ``` 2. 安装后,我们可以检查Ceres-solver是否成功安装。通过终端窗口输入以下命令进行检查: ``` pkg-config --modversion ceres ``` 如果Ceres-solver成功安装,则会显示Ceres-solver的版本号。 方法二:通过源代码安装 如果您想安装最新版本的Ceres-solver,或者如果软件包管理器无法提供所需的版本,则可以通过源代码安装。具体步骤如下: 1. 从Ceres-solver官方网站下载 Ceres-solver的源代码:http://ceres-solver.org/installation.html 2. 下载后,将压缩文件解压缩。在终端窗口中进入解压缩后的文件夹,并运行以下命令: ``` sudo mkdir build cd build sudo cmake .. sudo make sudo make install ``` 3. 安装完成后,我们可以检查Ceres-solver是否成功安装。通过终端窗口输入以下命令进行检查: ``` pkg-config --modversion ceres ``` 如果Ceres-solver成功安装,则会显示Ceres-solver的版本号。 总结: 通过软件包管理器安装Ceres-solver可能更为简单,但这并不意味着源代码安装不是好选择。如果您想安装最新版本的软件,或者 Ubuntu 软件包管理器没有所需软件包。我们希望这篇文章对您有所帮助,让您成功在Ubuntu18.04上安装Ceres-solver。 ### 回答3: ceres-solver是一个广泛应用于计算机视觉、机器人和自动驾驶领域的C++库,它提供了许多优秀的优化算法。在ubuntu18.04中安装ceres-solver非常简单,下面给出步骤: 1. 打开终端,安装必要的依赖库:sudo apt-get install cmake libgoogle-glog-dev libatlas-base-dev 2. 从官方网站下载ceres-solver的源代码包,解压tar.gz包到/home/user/文件夹中(user为当前用户名),并进入解压后的文件夹 3. 在终端中进入解压后的文件夹,先建立build文件夹:`mkdir build`, 然后进入该文件夹:`cd build` 4. 对于64位系统的用户,可以在终端中使用如下命令设置编译器参数:cmake .. -DCMAKE_CXX_FLAGS="-std=c++11"【注:-std=c++11是C++11标准,用于支持新的特性】;对于32位系统的用户,使用cmake .. 就可以了 5. 在终端中输入:make -j4(其中- j 后面的数字是并行编译的数量,按实际情况进行调整),编译源代码,此时电脑会开始编译并生成ceres-solver库,时间较长,请耐心等待。 6. 输入如下命令进行安装:sudo make install 7. 检查是否安装成功,在终端中输入:pkg-config --modversion ceres。若显示版本号,则代表安装成功。 这样,ceres-solver已经成功安装到了ubuntu18.04系统中。此时您可以在自己的程序中使用ceres-solver库,实现自动优化和计算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值