PCL點雲變換與移除NaN


對點雲的操作可以直接應用變換矩陣,即旋轉,平移,尺度,3D的變換就是要使用4*4 的矩陣,例如:

       

 

 

等等模型

在這里直接使用程序開實現一個點雲的旋轉,新建文件matrix.cpp

#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>
// 命令行的幫助提示
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;
}

int main (int argc, char** argv)
{
  if (pcl::console::find_switch (argc, argv, "-h") || pcl::console::find_switch (argc, argv, "--help")) {
    showHelp (argv[0]);
    return 0;
  }
  // 讀取文件
  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;
    }
  }
 //載入文件
  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 定義旋轉的角度  再有角度計算出旋轉矩陣
  float theta = M_PI/4; // 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;//意思就是在第一行第四個元素的值為2.5,也就是在x軸的平移為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> ());
  // 你可以使用 transform_1 或者 transform_2;效果都是一樣的 
  pcl::transformPointCloud (*source_cloud, *transformed_cloud, transform_2);

  // 可視化的
  printf(  "\nPoint cloud colors :  white  = original point cloud\n"
      "                        red  = transformed point cloud\n");
  pcl::visualization::PCLVisualizer viewer ("Matrix transformation example");

   // 為點雲設置RGB的值
  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, 0);
  viewer.setBackgroundColor(0.05, 0.05, 0.05, 0); //設置背景顏色
  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文件查看效果,也可以該程序的參數,查看不同的參數的結果

命令窗口打印的結果

可視化的結果

 (2)移除 NaNs:

從傳感器獲得的點雲可能包含幾種測量誤差和/或不准確。其中之一是在一些點的坐標中存在NaN(不是數)值,正如你在下面的文件中看到的那樣:

# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z rgba
SIZE 4 4 4 4
TYPE F F F U
COUNT 1 1 1 1
WIDTH 640
HEIGHT 480
VIEWPOINT 0 0 0 1 0 0 0
POINTS 307200
DATA ascii
nan nan nan 10135463
nan nan nan 10398635
nan nan nan 10070692
nan nan nan 10268071
...

 點雲對象的成員函數有稱為“is_dense()”,如果所有的點都有效的返回true是為有限值。一個NaNs表明測量傳感器距離到該點的距離值是有問題的,可能是因為傳感器太近或太遠,或者因為表面反射。那么當存在無效點雲的NaNs值作為算法的輸入的時候,可能會引起很多問題,比如“"Assertion `point_representation_->isValid (point) && "Invalid (NaN, Inf) point coordinates given to radiusSearch!"' failed."”如果發生這樣的錯誤就要移除這些點,那么下面就是為了解決移除無效點的程序

#include <pcl/io/pcd_io.h>
#include <pcl/filters/filter.h>
#include <iostream>
#include <pcl/visualization/cloud_viewer.h>

int main(int argc,char** argv)
{
if(argc !=3)
 {
  std::cout <<"\tUsage: "<<argv[0] <<"<input cloud> <output cloud>" <<std::endl;

  return  -1;
 }

//object for string the point cloud
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);
//read a PCDfile from disk
if(pcl::io::loadPCDFile<pcl::PointXYZRGBA>(argv[1],*cloud) !=0)
{
 return -1;
}

//the mapping tells you to that points of the oldcloud the new ones correspond
//but we  will not use it
std::vector<int> mapping;
pcl::removeNaNFromPointCloud(*cloud, *cloud, mapping);
//pcl::removeNaNFromPointCloud(*cloud, *cloud, mapping);
//save it back
pcl::io::savePCDFileASCII(argv[2],*cloud);

pcl::visualization::CloudViewer viewer(argv[2]);
         viewer.showCloud(cloud);
        while (!viewer.wasStopped())
    {
        // Do nothing but wait.
    }

}

然后可以顯示移除NaNs點后的可視圖,

 

 這張點雲是我自己用kinect 生成的點雲,在沒有移除NaNs的時候可以先讀取以下,顯示他的點雲數值在命令窗口,你會發現會有很多的NaNs的無效點,經過

移除這些點之后在read一些打印處的結果就不會存在NaNs的無效點,這樣在后期的使用算法的時候就不會出現錯誤了。

 

這種方法的問題是它不會保持點雲仍然是有序點雲。所有的點雲都存儲一個“寬度”和“高度”變量。在無序點雲,總數為寬度相同,而高度設置為1。在有序的點雲(像從相機拍攝像傳感器如Kinect或Xtion的),寬度和高度都相同的像素的圖像分辨率傳感器的工作。點雲分布在深度圖像的行中,每一個點對應一個像素。成員函數”isorganized()”如果高度大於1時返回真。
由於移除NaNs無效點會改變點雲的點的數量,它不再能保持組織與原來的寬高比,所以函數將設置高度1。這不是一個大問題,只有少數的PCL的算法工作明確要求是有序的點雲(大多這樣情況下會使用在優化上),但你必須考慮其中的影響。

暫時就到這里了。。。。。。

 

微信公眾號號可掃描二維碼一起共同學習交流

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM