Ceres Solver 协方差的求解思路

一、理论

(1)、系统观测值是均值为0,协方差为单位矩阵

(2)、系统观测值是均值为0,协方差不是单位矩阵

注意:这个S是观测值的协方差矩阵;比如对于GPS/INS组合的导航系统,S应该是GPS位置观测值的协方差矩阵;

二、SVD分解求矩阵伪逆

C++代码

#include <iostream>
#include <Eigen/SVD>
#include <Eigen/Core>

using namespace std;


MatrixXd Estimator::pinv(MatrixXd  A)
{
   Eigen::JacobiSVD<Eigen::MatrixXd> svd(A, Eigen::ComputeFullU | Eigen::ComputeFullV);//M=USV*
    double  pinvtoler = 1.e-8; //tolerance
    int row = A.rows();
    int col = A.cols();
    int k = min(row,col);
    MatrixXd X = MatrixXd::Zero(col,row);
    MatrixXd singularValues_inv = svd.singularValues();//奇异值
    MatrixXd singularValues_inv_mat = MatrixXd::Zero(col, row);
    for (long i = 0; i<k; ++i) 
    {
        if (singularValues_inv(i) > pinvtoler)
                singularValues_inv(i) = 1.0 / singularValues_inv(i);
        else singularValues_inv(i) = 0;
    }
    for (long i = 0; i < k; ++i) 
    {
        singularValues_inv_mat(i, i) = singularValues_inv(i);
    }
    
    //X=VS+U*
    X=(svd.matrixV())*(singularValues_inv_mat)*(svd.matrixU().transpose());
     
    return X;
}



int main() {
    // 设置矩阵行数、列数
    const int ROW = 3;
    const int COL = 4;

    // 生成大小 ROW * COL 的随机矩阵
    Eigen::MatrixXd A;
    A = Eigen::MatrixXd::Random(ROW, COL);
    
    // 打印矩阵A
    cout << "矩阵A为:" << endl;
    cout << A << endl;
    
    // 打印矩阵A的伪逆矩阵
    cout << "矩阵A的伪逆为:" << endl;
    cout << pinv(A) << endl;
}

三、ceres-solver  求协方差

      Eigen::Matrix<double,6,6, Eigen::RowMajor> cov_pose = Eigen::Matrix<double,6,6, Eigen::RowMajor>::Zero();
      
           ceres::Covariance::Options cov_options;
      ceres::Covariance covariance(cov_options);
      
      std::vector<std::pair<const double*, const double*>> covariance_blocks;
      for(int i = 0; i <= WINDOW_SIZE; ++i)
      covariance_blocks.push_back(std::make_pair(para_Pose[i], para_Pose[i]));
  
     // 有时候出现 rank问题,忽略一下
      
      Ceres_cov_pose.setZero();
      Eigen::Matrix<double,6,6, Eigen::RowMajor> Ceres_cov_mid = Eigen::Matrix<double,6,6, Eigen::RowMajor>::Zero();
      
     if( covariance.Compute(covariance_blocks, &problem) )
     {
         for(int i = 0; i <= WINDOW_SIZE; ++i)
         {
             covariance.GetCovarianceBlockInTangentSpace(para_Pose[i],para_Pose[i], Ceres_cov_mid.data());  
             Ceres_cov_pose += Ceres_cov_mid; 
         }    

     }
      
     std::cout <<"cov_pose.diagonal() ="<< cov_pose.diagonal() << std::endl;

别人的思路

 1. ceres 官网

 官网简介链接

 2. 案例

double x[3];
double y[2];

Problem problem;
problem.AddParameterBlock(x, 3);
problem.AddParameterBlock(y, 2);
<Build Problem>
<Solve Problem>

Covariance::Options options;
Covariance covariance(options);

vector<pair<const double*, const double*> > covariance_blocks;
covariance_blocks.push_back(make_pair(x, x));
covariance_blocks.push_back(make_pair(y, y));
covariance_blocks.push_back(make_pair(x, y));

CHECK(covariance.Compute(covariance_blocks, &problem));

double covariance_xx[3 * 3];
double covariance_yy[2 * 2];
double covariance_xy[3 * 2];
covariance.GetCovarianceBlock(x, x, covariance_xx)
covariance.GetCovarianceBlock(y, y, covariance_yy)
covariance.GetCovarianceBlock(x, y, covariance_xy)

备注:

微信:j15212774116,  欢迎讨论 !

参考文献

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Ceres Solver是一个用于非线性最小二乘问题的开源C++库,它可以用于求解各种优化问题,包括二维变换矩阵的求解。 以下是一个使用Ceres Solver求解二维变换矩阵的示例代码: ```c++ #include <ceres/ceres.h> #include <ceres/rotation.h> #include <iostream> // 定义二维点的结构体 struct Point2D { double x; double y; }; // 定义二维变换矩阵的参数结构体 struct TransformParams { double angle; // 旋转角度 double scale; // 缩放因子 double tx; // 水平平移 double ty; // 垂直平移 }; // 定义Ceres Solver的Cost Function struct TransformCostFunctor { TransformCostFunctor(const Point2D& from, const Point2D& to) : from_(from), to_(to) {} template<typename T> bool operator()(const T* const params, T* residual) const { T rotated_x = cos(params[0]) * from_.x - sin(params[0]) * from_.y; T rotated_y = sin(params[0]) * from_.x + cos(params[0]) * from_.y; T scaled_x = params[1] * rotated_x; T scaled_y = params[1] * rotated_y; residual[0] = scaled_x + params[2] - to_.x; residual[1] = scaled_y + params[3] - to_.y; return true; } private: const Point2D from_; const Point2D to_; }; int main() { // 定义源点和目标点 Point2D from = { 2.0, 3.0 }; Point2D to = { 4.0, 5.0 }; // 定义Ceres Solver的Problem和参数块 ceres::Problem problem; double params[4] = { 0.0, 1.0, 0.0, 0.0 }; ceres::ParameterBlock* param_block = problem.AddParameterBlock(params, 4); // 定义Cost Function并添加到Problem中 ceres::CostFunction* cost_function = new ceres::AutoDiffCostFunction<TransformCostFunctor, 2, 4>(new TransformCostFunctor(from, to)); problem.AddResidualBlock(cost_function, nullptr, param_block); // 设置Solver的选项并求解 ceres::Solver::Options options; options.minimizer_progress_to_stdout = true; ceres::Solver::Summary summary; ceres::Solve(options, &problem, &summary); // 输出结果 std::cout << summary.FullReport() << std::endl; std::cout << "Transform matrix: " << std::endl; std::cout << params[1] * cos(params[0]) << " " << -params[1] * sin(params[0]) << " " << params[2] << std::endl; std::cout << params[1] * sin(params[0]) << " " << params[1] * cos(params[0]) << " " << params[3] << std::endl; std::cout << "Residual: " << summary.final_cost << std::endl; return 0; } ``` 在上述代码中,我们首先定义了一个二维点的结构体`Point2D`和一个二维变换矩阵的参数结构体`TransformParams`。然后我们定义了一个Ceres Solver的Cost Function`TransformCostFunctor`,它的作用是计算从源点变换到目标点需要的变换矩阵,并计算该变换矩阵下源点和目标点之间的残差。在`TransformCostFunctor`的`operator()`函数中,我们首先根据旋转角度将源点旋转,然后根据缩放因子进行缩放,并根据平移量进行平移,最后计算残差。 在`main()`函数中,我们首先定义了源点和目标点,并初始化了参数块。然后我们定义了一个Cost Function并将其添加到Problem中。最后我们设置了Solver的选项并调用`ceres::Solve`函数求解问题。求解完成后,我们可以通过`ceres::Solver::Summary`结构体中的信息输出求解结果和残差。 需要注意的是,上述代码只是求解了一个二维变换矩阵的例子,实际应用中可能需要更复杂的Cost Function和更多的参数。Ceres Solver提供了丰富的工具和接口,可以用于各种非线性优化问题的求解

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

他人是一面镜子,保持谦虚的态度

你的鼓励是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值