ceres关于图优化问题

摘要:
ceres关于图优化问题首先是图的节点,一般为位姿;再者,边代表节点与节点之间的相对变换,一般是真实测量的数据,如里程计、激光雷达数据、imu数据等。

ceres关于图优化问题

首先是图的节点,一般为位姿;再者,边代表节点与节点之间的相对变换(旋转和平移),一般是真实测量的数据,如里程计、激光雷达数据、imu数据等。如下图,三角形代表位姿、边代表测量数据;虚线代表回环检测的约束边。

ceres关于图优化问题第1张

#include <fstream>#include <iostream>#include <map>#include <string>#include <vector>
#include "angle_local_parameterization.h"#include "ceres/ceres.h"#include "common/read_g2o.h"#include "gflags/gflags.h"#include "glog/logging.h"#include "pose_graph_2d_error_term.h"#include "types.h"
DEFINE_string(input, "", "The pose graph definition filename in g2o format.");

namespaceceres {
namespaceexamples {
namespace{

//Constructs the nonlinear least squares optimization problem from the pose
//graph constraints.
void BuildOptimizationProblem(const std::vector<Constraint2d>&constraints,
                              std::map<int, Pose2d>*poses,
                              ceres::Problem*problem) {
  CHECK(poses !=NULL);
  CHECK(problem !=NULL);
  if(constraints.empty()) {
    LOG(INFO) << "No constraints, no problem to optimize.";
    return;
  }

  ceres::LossFunction* loss_function =NULL;
  ceres::LocalParameterization* angle_local_parameterization =AngleLocalParameterization::Create();  //创建参数项

  for (std::vector<Constraint2d>::const_iterator constraints_iter =constraints.begin();
       constraints_iter !=constraints.end();
       ++constraints_iter) 
  {
    const Constraint2d& constraint = *constraints_iter;

    std::map<int, Pose2d>::iterator pose_begin_iter =poses->find(constraint.id_begin);
    CHECK(pose_begin_iter != poses->end())
        << "Pose with ID: " << constraint.id_begin << "not found.";
    std::map<int, Pose2d>::iterator pose_end_iter =poses->find(constraint.id_end);
    CHECK(pose_end_iter != poses->end())
        << "Pose with ID: " << constraint.id_end << "not found.";

    const Eigen::Matrix3d sqrt_information =constraint.information.llt().matrixL();
    //Ceres will take ownership of the pointer.
    ceres::CostFunction* cost_function =PoseGraph2dErrorTerm::Create(
        constraint.x, constraint.y, constraint.yaw_radians, sqrt_information);  //传参进去
    problem->AddResidualBlock(cost_function,
                              loss_function,
                              &pose_begin_iter->second.x,
                              &pose_begin_iter->second.y,
                              &pose_begin_iter->second.yaw_radians,
                              &pose_end_iter->second.x,
                              &pose_end_iter->second.y,
                              &pose_end_iter->second.yaw_radians);  //添加残差项  传入优化变量
problem->SetParameterization(&pose_begin_iter->second.yaw_radians,
                                 angle_local_parameterization);
    problem->SetParameterization(&pose_end_iter->second.yaw_radians,
                                 angle_local_parameterization);
  }

  //The pose graph optimization problem has three DOFs that are not fully
  //constrained. This is typically referred to as gauge freedom. You can apply
  //a rigid body transformation to all the nodes and the optimization problem
  //will still have the exact same cost. The Levenberg-Marquardt algorithm has
  //internal damping which mitigate this issue, but it is better to properly
  //constrain the gauge freedom. This can be done by setting one of the poses
  //as constant so the optimizer cannot change it.
  std::map<int, Pose2d>::iterator pose_start_iter = poses->begin();
  CHECK(pose_start_iter != poses->end()) << "There are no poses.";
  problem->SetParameterBlockConstant(&pose_start_iter->second.x);
  problem->SetParameterBlockConstant(&pose_start_iter->second.y);
  problem->SetParameterBlockConstant(&pose_start_iter->second.yaw_radians);
}

//Returns true if the solve was successful.
bool SolveOptimizationProblem(ceres::Problem*problem) {
  CHECK(problem !=NULL);

  ceres::Solver::Options options;
  options.max_num_iterations = 100;
  options.linear_solver_type =ceres::SPARSE_NORMAL_CHOLESKY;

  ceres::Solver::Summary summary;
  ceres::Solve(options, problem, &summary);

  std::cout << summary.FullReport() << '';

  returnsummary.IsSolutionUsable();
}

//Output the poses to the file with format: ID x y yaw_radians.
bool OutputPoses(const std::string&filename,
                 const std::map<int, Pose2d>&poses) {
  std::fstream outfile;
  outfile.open(filename.c_str(), std::istream::out);
  if (!outfile) {
    std::cerr << "Error opening the file: " << filename << '';
    return false;
  }
  for (std::map<int, Pose2d>::const_iterator poses_iter =poses.begin();
       poses_iter !=poses.end();
       ++poses_iter) {
    const std::map<int, Pose2d>::value_type& pair = *poses_iter;
    outfile << pair.first << " " << pair.second.x << " " << pair.second.y << ' '
            << pair.second.yaw_radians << '';
  }
  return true;
}

}  //namespace
}  //namespace examples
}  //namespace ceres

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

  CHECK(FLAGS_input != "") << "Need to specify the filename to read.";

  std::map<int, ceres::examples::Pose2d>poses;
  std::vector<ceres::examples::Constraint2d>constraints;

  CHECK(ceres::examples::ReadG2oFile(FLAGS_input, &poses, &constraints))
      << "Error reading the file: " <<FLAGS_input;

  std::cout << "Number of poses: " << poses.size() << '';
  std::cout << "Number of constraints: " << constraints.size() << '';

  CHECK(ceres::examples::OutputPoses("poses_original.txt", poses))
      << "Error outputting to poses_original.txt";

  ceres::Problem problem;
  ceres::examples::BuildOptimizationProblem(constraints, &poses, &problem);

  CHECK(ceres::examples::SolveOptimizationProblem(&problem))
      << "The solve was not successful, exiting.";

  CHECK(ceres::examples::OutputPoses("poses_optimized.txt", poses))
      << "Error outputting to poses_original.txt";

  return 0;
}

免责声明:文章转载自《ceres关于图优化问题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C#(4):XML序列化Ubuntu安装lrzsz下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

kaldi 三个脚本cmd.sh path.sh run.sh

参考 kaldi的全部资料_v0.4 cmd.sh 脚本为: 可以很清楚的看到有 3 个分类分别对应 a,b,c。a 和 b 都是集群上去运行这个样子, c 就是我们需要的。我们在虚拟机上运行的。你需要修改这个脚本 # "queue.pl"uses qsub. The options to it are # options to qsub. If yo...

diedaiqi

转自:https://www.cnblogs.com/maluning/p/8570717.html https://www.cnblogs.com/ShaneZhang/p/4249173.html 正文 迭代器是一种检查容器内元素并遍历元素的数据类型。C++更趋向于使用迭代器而不是下标操作,因为标准库为每一种标准容器(如vector)定义了一种迭代器类...

C++ 的erase和remove remove_if删除元素

1. erase   很多时候,我在删除vector元素的时候使用的是遍历指针,符合条件时使用erase删除, 可是这样的使用办法会有个问题,在vec.erase(iter)后,需要使用erase的返回值,并且这个返回值是等价iter++,也就是iter的下一个元素, 否则的话,iter将会是个无效指针 vector<int> vec_...

句柄类与继承

前一小节《容器与继承》http://blog.csdn.net/thefutureisour/article/details/7744790提到过: 对于容器,假设定义为基类类型,那么则不能通过容器訪问派生类新增的成员;假设定义为派生类类型,一般不能用它承载基类的对象,即使利用类型转化强行承载,则基类对象能够訪问没有意义的派生类成员,这样做是非常危...

sklearn参数优化方法

学习器模型中一般有两个参数:一类参数可以从数据中学习估计得到,还有一类参数无法从数据中估计,只能靠人的经验进行指定,后一类参数就叫超参数 比如,支持向量机里的C,Kernel,gama,朴素贝叶斯里的alpha等,在学习其模型的设计中,我们要搜索超参数空间为学习器模型找到最合理的超参数,可以通过以下方法获得学习器模型的参数列表和当前取值:estimator...

C++中容器的使用(一)

      C++中有两种类型的容器:顺序容器和关联容器。       顺序容器主要有vector、list、deque等。其中vector表示一段连续的内存,基于数组实现,list表示非连续的内存,基于链表实现,deque与vector类似,但是对首元素提供插入和删除的双向支持。       关联容器主要有map和set。map是key-value形式,...