002:pcl 點雲投影


1.首先包含的對應的ModelCoefficients.h以及filter中向平面投影的project_inlier.h

#include <iostream>

#include <pcl/io/pcd_io.h>

#include <pcl/point_types.h>

#include <pcl/ModelCoefficients.h>

#include <pcl/filters/project_inliers.h>

2.增加可視化顯示的代碼

int user_data;

void

viewerOneOff(pcl::visualization::PCLVisualizer& viewer)

{

       viewer.setBackgroundColor(1.0, 0.5, 1.0);

       pcl::PointXYZ o;

       o.x = 1.0;

       o.y = 0;

       o.z = 0;

       viewer.addSphere(o, 0.25, "sphere", 0);

       std::cout << "i only run once" << std::endl;
}

void

viewerPsycho(pcl::visualization::PCLVisualizer& viewer)

{

       static unsigned count = 0;

       std::stringstream ss;

       ss << "Once per viewer loop: " << count++;

       viewer.removeShape("text", 0);

       viewer.addText(ss.str(), 200, 300, "text", 0);

       //FIXME: possible race condition here:

       user_data++;

}


3.創建點雲對象指針並初始化,輸出到屏幕

/2.初始化該對象

  cloud->width  = 5;//對於未組織的點雲的相當於points個數

  cloud->height = 1; //對未組織的點雲指定為1

  cloud->points.resize (cloud->width * cloud->height); //修剪或追加值初始化的元素

  for (size_t i = 0; i < cloud->points.size (); ++i)

  {

    cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);

    cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);

    cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);

  }

  // 3.cerr 輸出對象放置刷屏

  std::cerr << "Cloud before projection: " << std::endl;

  for (size_t i = 0; i < cloud->points.size (); ++i)

    std::cerr << "    " << cloud->points[i].x << " " 

                        << cloud->points[i].y << " " 

                        << cloud->points[i].z << std::endl;


//投影前點
`Cloud before projection:
    1.28125 577.094 197.938
    828.125 599.031 491.375
    358.688 917.438 842.563
    764.5 178.281 879.531
    727.531 525.844 311.281

4.設置ModelCoefficients值。在這種情況下,我們使用一個平面模型,其中ax + by + cz + d = 0,其中a = b = d = 0,c = 1,或者換句話說,XY平面

  // 4.創建一個系數為X=Y=0,Z=1的平面

  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());

  coefficients->values.resize (4);

  coefficients->values[0] = coefficients->values[1] = 0;

  coefficients->values[2] = 1.0;

  coefficients->values[3] = 0;

5.通過該濾波將所有的點投影到創建的平面上,並輸出結果
** 注意這里在使用的時候再創建濾波后對象不規范,應該放在程序開始的時候**

  //5.創建濾波后對象,並通過濾波投影,並顯示結果

  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_projected(new 
pcl::PointCloud<pcl::PointXYZ>);

  // 創建濾波器對象

  pcl::ProjectInliers<pcl::PointXYZ> proj;

  proj.setModelType (pcl::SACMODEL_PLANE);

  proj.setInputCloud (cloud);

  proj.setModelCoefficients (coefficients);

  proj.filter (*cloud_projected);
  //可視化顯示
   pcl::visualization::CloudViewer viewer("Cloud Viewer");

  //showCloud函數是同步的,在此處等待直到渲染顯示為止

  viewer.showCloud(cloud);

  //該注冊函數在可視化時只調用一次

  viewer.runOnVisualizationThreadOnce(viewerOneOff);

  //該注冊函數在渲染輸出時每次都調用

  viewer.runOnVisualizationThread(viewerPsycho);

  while (!viewer.wasStopped())

  {

         //在此處可以添加其他處理

         user_data++;

  }
  

  std::cerr << "Cloud after projection: " << std::endl;

  for (size_t i = 0; i < cloud_projected->points.size (); ++i)

    std::cerr << "    " << cloud_projected->points[i].x << " " 

                        << cloud_projected->points[i].y << " " 

                        << cloud_projected->points[i].z << std::endl;
  return (0);
//投影后點
Cloud before projection:
    1.28125 577.094 197.938
    828.125 599.031 491.375
    358.688 917.438 842.563
    764.5 178.281 879.531
    727.531 525.844 311.281
Cloud after projection:
    1.28125 577.094 0
    828.125 599.031 0
    358.688 917.438 0
    764.5 178.281 0
    727.531 525.844 0

6.參考網址
pcl官網例程
all-in_one 中的有api 以及例子,但是具體理論說明還是參考官網吧!
...\PCL-1.8.1-AllInOne-msvc2017-win64(1)\share\doc\pcl-1.8\tutorials\sources中 例子要比pcl入門精通要全


免責聲明!

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



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