PCL 点云数据实现旋转、平移

 这里主要介绍点云的平移与旋转,话不多说,代码如下:该代码可实现点云绕z轴旋转90°,绕x轴平移2.5,如果显示无法打开PDB文件,请翻到结尾查看我的解决方法。

#include <iostream>
 
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_cloud.h>
#include <pcl/console/parse.h>
#include <pcl/common/transforms.h>
#include <pcl/visualization/pcl_visualizer.h>
 
// This function displays the help
void
showHelp(char * program_name)
{
  std::cout << std::endl;
  std::cout << "Usage: " << program_name << " cloud_filename.[pcd|ply]" << std::endl;
  std::cout << "-h:  Show this help." << std::endl;
}
// This is the main function
int
main (int argc, char** argv)
{
  // Show help
  if (pcl::console::find_switch (argc, argv, "-h") || pcl::console::find_switch (argc, argv, "--help")) {
    showHelp (argv[0]);
    return 0;
  }
  // Fetch point cloud filename in arguments | Works with PCD and PLY files
  std::vector<int> filenames;
  bool file_is_pcd = false;
 
  filenames = pcl::console::parse_file_extension_argument (argc, argv, ".ply");
 
  if (filenames.size () != 1)  {
    filenames = pcl::console::parse_file_extension_argument (argc, argv, ".pcd");
 
    if (filenames.size () != 1) {
      showHelp (argv[0]);
      return -1;
    } else {
      file_is_pcd = true;
    }
  }
 
  // Load file | Works with PCD and PLY files
  pcl::PointCloud<pcl::PointXYZ>::Ptr source_cloud (new pcl::PointCloud<pcl::PointXYZ> ());
 
  if (file_is_pcd) {
    if (pcl::io::loadPCDFile (argv[filenames[0]], *source_cloud) < 0)  {
      std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl;
      showHelp (argv[0]);
      return -1;
    }
  } else {
    if (pcl::io::loadPLYFile (argv[filenames[0]], *source_cloud) < 0)  {
      std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl;
      showHelp (argv[0]);
      return -1;
    }
  }
  /* Reminder: how transformation matrices work :
           |-------> This column is the translation
    | 1 0 0 x |  \
    | 0 1 0 y |   }-> The identity 3x3 matrix (no rotation) on the left
    | 0 0 1 z |  /
    | 0 0 0 1 |    -> We do not use this line (and it has to stay 0,0,0,1)
 
    METHOD #1: Using a Matrix4f
    This is the "manual" method, perfect to understand but error prone !
  */
  Eigen::Matrix4f transform_1 = Eigen::Matrix4f::Identity();
 
  // Define a rotation matrix (see https://en.wikipedia.org/wiki/Rotation_matrix)
  float theta = M_PI/2; // The angle of rotation in radians
  transform_1 (0,0) = cos (theta);
  transform_1 (0,1) = -sin(theta);
  transform_1 (1,0) = sin (theta);
  transform_1 (1,1) = cos (theta);
  //    (row, column)
 
  // Define a translation of 2.5 meters on the x axis.
  transform_1 (0,3) = 2.5;
 
  // Print the transformation
  printf ("Method #1: using a Matrix4f\n");
  std::cout << transform_1 << std::endl;
  /*  METHOD #2: Using a Affine3f
    This method is easier and less error prone
  */
  Eigen::Affine3f transform_2 = Eigen::Affine3f::Identity();
  // Define a translation of 2.5 meters on the x axis.
  transform_2.translation() << 2.5, 0.0, 0.0;
  // The same rotation matrix as before; theta radians arround Z axis
  transform_2.rotate (Eigen::AngleAxisf (theta, Eigen::Vector3f::UnitZ()));
  // Print the transformation
  printf ("\nMethod #2: using an Affine3f\n");
  std::cout << transform_2.matrix() << std::endl;
  // Executing the transformation
  pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud (new pcl::PointCloud<pcl::PointXYZ> ());
  // You can either apply transform_1 or transform_2; they are the same
  pcl::transformPointCloud (*source_cloud, *transformed_cloud, transform_2);
 
  // Visualization
  printf(  "\nPoint cloud colors :  white  = original point cloud\n"
      "                        red  = transformed point cloud\n");
  pcl::visualization::PCLVisualizer viewer ("Matrix transformation example");
 
   // Define R,G,B colors for the point cloud
  pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> source_cloud_color_handler (source_cloud, 255, 255, 255);
  // We add the point cloud to the viewer and pass the color handler
  viewer.addPointCloud (source_cloud, source_cloud_color_handler, "original_cloud");
 
  pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> transformed_cloud_color_handler (transformed_cloud, 230, 20, 20); // Red
  viewer.addPointCloud (transformed_cloud, transformed_cloud_color_handler, "transformed_cloud");
 
  viewer.addCoordinateSystem (1.0, "cloud", 0);
  viewer.setBackgroundColor(0.05, 0.05, 0.05, 0); // Setting background to a dark grey
  viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "original_cloud");
  viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "transformed_cloud");
  //viewer.setPosition(800, 400); // Setting visualiser window position
 
  while (!viewer.wasStopped ()) { // Display the visualiser until 'q' key is pressed
    viewer.spinOnce ();
  }
 
  return 0;
}

 

我的解决方法: 

点击菜单栏项目-属性-配置属性-调试-命令参数-编辑,在命令参数中添加用户自己的pcd文件名字。

 

 

  • 3
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
PCL点云处理库是一个功能强大的点云处理工具,支持旋转平移等操作,并提供了多种粗配准算法,如PFH、FPFH、ICP、NDT和3DSC等。这些算法最终目的是将两个不同位置的点云进行匹配,实现点云3D重建等应用。 旋转平移点云位置可以使用PCL提供的Transformations模块实现。其中,旋转矩阵可以通过Eigen::Quaterniond或Eigen::AngleAxisd实现平移矩阵可以使用Eigen::Affine3f实现,从而实现点云旋转平移。使用这些方法完成旋转平移后,可以进行下面几种粗配准算法: 1. PFH(Point Feature Histograms)算法 PFH算法通过计算点云中每个点的特征直方图并对其进行匹配来计算两个点云之间的相似性。这种算法在处理具有复杂形状或不规则边界的点云时表现良好。 2. FPFH(Fast Point Feature Histograms)算法 FPFH算法是PFH算法的优化,能够提高匹配速度和准确性,同时还支持噪声过滤和配准的完全自动化。 3. ICP(Point-to-Point Iterative Closest Point)算法 ICP算法是使用最广泛的粗配准算法之一,它尝试通过迭代比对点云中每个点的最近邻点来计算两个点云之间的转换。ICP算法对初始位置的精度要求较高。 4. NDT(Normal Distributions Transform)算法 NDT算法基于高斯分布模型,通过优化高斯分布参数来计算两个点云之间的转换。NDT算法对初始位置的要求较低,适用于处理含噪声或不规则分布的点云。 5. 3DSC(3D Shape Context)算法 3DSC算法通过计算点云中每个点周围几何结构的一种描述符并进行匹配来计算两个点云之间的相似性。3DSC算法对于具有明显几何结构的点云具有很好的效果。 总之,PCL点云处理库提供了多种粗配准方法,可以根据不同场景和需求选择合适的方法来完成点云的匹配和重建。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值