PCL:NDT2D

正态分布变换基于统计的方法估计变换矩阵。
代码如下:

#include <pcl/console/parse.h>
#include <pcl/console/print.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/registration/ndt_2d.h>
#include <pcl/registration/transformation_estimation_lm.h>
#include <pcl/registration/warp_point_rigid_3d.h>

#include <string>
#include <iostream>
#include <fstream>
#include <vector>

typedef pcl::PointXYZ PointType;
typedef pcl::PointCloud<PointType> Cloud;
typedef Cloud::ConstPtr CloudConstPtr;
typedef Cloud::Ptr CloudPtr;


void
selfTest ()
{
  CloudPtr model (new Cloud);
  model->points.push_back (PointType (1,1,0));  
  model->points.push_back (PointType (4,4,0)); 
  model->points.push_back (PointType (5,6,0));
  model->points.push_back (PointType (3,3,0));
  model->points.push_back (PointType (6,7,0));
  model->points.push_back (PointType (7,11,0));
  model->points.push_back (PointType (12,15,0));
  model->points.push_back (PointType (7,12,0));

  CloudPtr data (new Cloud);
  data->points.push_back (PointType (3,1,0));
  data->points.push_back (PointType (7,4,0));
  data->points.push_back (PointType (9,6,0));

  pcl::console::setVerbosityLevel (pcl::console::L_DEBUG);  
  
  pcl::NormalDistributionsTransform2D<PointType, PointType> ndt;

  ndt.setMaximumIterations (40);
  ndt.setGridCentre (Eigen::Vector2f (0,0));
  ndt.setGridExtent (Eigen::Vector2f (20,20));
  ndt.setGridStep (Eigen::Vector2f (20,20));
  ndt.setOptimizationStepSize (Eigen::Vector3d (0.4,0.4,0.1));
  ndt.setTransformationEpsilon (1e-9);

  ndt.setInputTarget (model);
  ndt.setInputSource (data);

  CloudPtr tmp (new Cloud);
  ndt.align (*tmp);
  std::cout << ndt.getFinalTransformation () << std::endl;
}


int
main (int argc, char **argv)
{
  int iter = 10;
  double grid_step = 3.0;
  double grid_extent = 25.0;
  double optim_step = 1.0;

  pcl::console::parse_argument (argc, argv, "-i", iter);
  pcl::console::parse_argument (argc, argv, "-g", grid_step);
  pcl::console::parse_argument (argc, argv, "-e", grid_extent);
  pcl::console::parse_argument (argc, argv, "-s", optim_step);

  std::vector<int> pcd_indices;
  pcd_indices = pcl::console::parse_file_extension_argument (argc, argv, ".pcd");

  CloudPtr model (new Cloud);
  if (pcl::io::loadPCDFile (argv[pcd_indices[0]], *model) == -1)
  {
    std::cout << "Could not read file" << std::endl;
    return -1;
  }
  std::cout << argv[pcd_indices[0]] << " width: " << model->width << " height: " << model->height << std::endl;

  std::string result_filename (argv[pcd_indices[0]]);
  result_filename = result_filename.substr (result_filename.rfind ("/") + 1);
  try
  {
    pcl::io::savePCDFile (result_filename.c_str (), *model);
    std::cout << "saving first model to " << result_filename << std::endl;
  }
  catch(pcl::IOException& e)
  {
    std::cerr << e.what() << std::endl;
  }

  Eigen::Matrix4f t (Eigen::Matrix4f::Identity ());

  for (size_t i = 1; i < pcd_indices.size (); i++)
  {
    CloudPtr data (new Cloud);
    if (pcl::io::loadPCDFile (argv[pcd_indices[i]], *data) == -1)
    {
      std::cout << "Could not read file" << std::endl;
      return -1;
    }
    std::cout << argv[pcd_indices[i]] << " width: " << data->width << " height: " << data->height << std::endl;

    //pcl::console::setVerbosityLevel(pcl::console::L_DEBUG);

    pcl::NormalDistributionsTransform2D<PointType, PointType> ndt;

    ndt.setMaximumIterations (iter);
    ndt.setGridCentre (Eigen::Vector2f (15,0));
    ndt.setGridExtent (Eigen::Vector2f (grid_extent,grid_extent));
    ndt.setGridStep (Eigen::Vector2f (grid_step,grid_step));
    ndt.setOptimizationStepSize (optim_step);
    ndt.setTransformationEpsilon (1e-5);

    ndt.setInputTarget (model);
    ndt.setInputSource (data);

    CloudPtr tmp (new Cloud);
    ndt.align (*tmp);

    t = t* ndt.getFinalTransformation ();

    pcl::transformPointCloud (*data, *tmp, t);

    std::cout << ndt.getFinalTransformation () << std::endl;

    *model = *data;

    try
    {
      std::string result_filename (argv[pcd_indices[i]]);
      result_filename = result_filename.substr (result_filename.rfind ("/") + 1);
      pcl::io::savePCDFileBinary (result_filename.c_str (), *tmp);
      std::cout << "saving result to " << result_filename << std::endl;
    }
    catch(pcl::IOException& e)
    {
      std::cerr << e.what() << std::endl;
    }
  }

  return 0;
}

来源:PCL官方示例

PCL(Point Cloud Library)是一个开源的点云处理库,拥有丰富的功能和算法,可以用于激光扫描仪、RGB-D相机等设备采集的点云数据处理。 NDT(Normal Distributions Transform)是一种点云配准方法,用于将不同视角的点云数据配准在同一个坐标系下,以实现建图和定位。 回环检测是指在建图过程中,通过判断当前扫描到的点云与之前存储的点云数据之间的差异,来检测是否发生了回环。回环检测对于建立大规模的点云地图和精确的定位至关重要。 在PCL中,使用NDT算法进行回环检测的步骤如下: 1. 预处理:对每一帧新扫描到的点云数据进行滤波、降采样等预处理操作,以减少噪声和点云数据量。 2. 特征提取:从预处理后的点云数据中提取特征,例如表面法线、曲率等,以描述点云的局部特征。 3. 特征匹配:将当前帧的特征与之前的点云地图中的特征进行匹配,寻找匹配度较高的特征点对。 4. 姿态估计:基于特征匹配的结果,通过最小化误差函数来估计当前帧相对于之前帧的姿态变换。 5. 迭代优化:使用迭代优化算法,对姿态估计的结果进行优化,进一步提高姿态的准确性。 6. 回环检测:根据优化后的姿态估计结果,判断当前扫描到的点云是否与之前存储的点云数据发生了回环。 通过以上步骤,PCL的NDT回环检测算法可以实现对建图过程中的回环进行检测和处理,从而提高点云建图的准确性和鲁棒性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值