PCL点云分割-区域生长法

我概括的来说,就是从曲率小的面播种,从种子的位置出发,开始往四周搜索点,然后比对点于点之间的曲率和法线方向,如果差距小于阈值就视为同一个cluster。如果一个cluster无法再蔓延,在剩下的点云里再找曲率小的面播种,然后继续重复直到遍历完毕。

基于颜色的区域生长分割 :原理上和基于曲率,法线的分割方法是一致的。只不过比较目标换成了颜色,去掉了点云规模上限的限制。可以认为,同一个颜色且挨得近,是一类的可能性很大。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
int main (int argc, char** argv)
{
  pcl::PointCloud::Ptr cloud (new pcl::PointCloud);
  if ( pcl::io::loadPCDFile  ("region_growing_tutorial.pcd", *cloud) == -1)
  {
    std::cout << "Cloud reading failed." << std::endl;
    return (-1);
  }
 
  pcl::search::Search::Ptr tree (new pcl::search::KdTree);
  pcl::PointCloud ::Ptr normals (new pcl::PointCloud );
  pcl::NormalEstimation normal_estimator;
  normal_estimator.setSearchMethod (tree);
  normal_estimator.setInputCloud (cloud);
  normal_estimator.setKSearch (50);
  normal_estimator.compute (*normals);
 
  pcl::IndicesPtr indices (new std::vector );
  pcl::PassThrough pass;
  pass.setInputCloud (cloud);
  pass.setFilterFieldName ("z");
  pass.setFilterLimits (0.0, 1.0);
  pass.filter (*indices);
 
  pcl::RegionGrowing reg;
  reg.setMinClusterSize (50);
  reg.setMaxClusterSize (1000000);
  reg.setSearchMethod (tree);
  reg.setNumberOfNeighbours (30);
  reg.setInputCloud (cloud);
  //reg.setIndices (indices);
  reg.setInputNormals (normals);
  reg.setSmoothnessThreshold (3.0 / 180.0 * M_PI);
  reg.setCurvatureThreshold (1.0);
 
  std::vector  clusters;
  reg.extract (clusters);
 
  std::cout << "Number of clusters is equal to " << clusters.size () << std::endl;
  std::cout << "First cluster has " << clusters[0].indices.size () << " points." << endl;
  std::cout << "These are the indices of the points of the initial" <<
    std::endl << "cloud that belong to the first cluster:" << std::endl;
  int counter = 0;
  while (counter < clusters[0].indices.size ())
  {
    std::cout << clusters[0].indices[counter] << ", ";
    counter++;
    if (counter % 10 == 0)
      std::cout << std::endl;
  }
  std::cout << std::endl;
 
  pcl::PointCloud ::Ptr colored_cloud = reg.getColoredCloud ();
  pcl::visualization::CloudViewer viewer ("Cluster viewer");
  viewer.showCloud(colored_cloud);
  while (!viewer.wasStopped ())
  {
  }
 
  return (0);
}

 

你可能感兴趣的:(PCL)