点云技术相关产学研社区

 找回密码
 立即注册加入PCL中国点云技术相关产学研社区

扫一扫,访问微社区

查看: 8455|回复: 6

欧式聚类提取,cluster_indices 一直为0,求解决方法

[复制链接]
发表于 2014-3-11 23:10:54 | 显示全部楼层 |阅读模式
本帖最后由 timlee 于 2014-3-13 07:47 编辑

用示例数据能正常分割提取,但是用自己的的数据cluster_indices 一直为0


int
main (int argc, char** argv)
{
  // Read in the cloud data
  pcl::PCDReader reader;
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>), cloud_f (new pcl::PointCloud<pcl::PointXYZ>);
  reader.read ("cut.pcd", *cloud);
  std::cout << "PointCloud before filtering has: " << cloud->points.size () << " data points." << std::endl; //*

  // Create the filtering object: downsample the dataset using a leaf size of 1cm
  pcl::VoxelGrid<pcl::PointXYZ> vg;
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
  vg.setInputCloud (cloud);
  vg.setLeafSize (0.1f, 0.1f, 0.1f);
  vg.filter (*cloud_filtered);
  std::cout << "PointCloud after filtering has: " << cloud_filtered->points.size ()  << " data points." << std::endl; //*

  // Create the segmentation object for the planar model and set all the parameters
  pcl::SACSegmentation<pcl::PointXYZ> seg;
  pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZ> ());
  pcl::PCDWriter writer;
  seg.setOptimizeCoefficients (true);
  seg.setModelType (pcl::SACMODEL_PLANE);
  seg.setMethodType (pcl::SAC_RANSAC);
  seg.setMaxIterations (100);
  seg.setDistanceThreshold (0.02);

  int i=0, nr_points = (int) cloud_filtered->points.size ();
  while (cloud_filtered->points.size() > 0.9 * nr_points)             //have ben modified from 0.3 to 0.8
  {
    // Segment the largest planar component from the remaining cloud
    seg.setInputCloud (cloud_filtered);
    seg.segment (*inliers, *coefficients);
    if (inliers->indices.size () == 0)
    {
      std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
      break;
    }

    // Extract the planar inliers from the input cloud
    pcl::ExtractIndices<pcl::PointXYZ> extract;
    extract.setInputCloud (cloud_filtered);
    extract.setIndices (inliers);
    extract.setNegative (false);

    // Write the planar inliers to disk
    extract.filter (*cloud_plane);
    std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size () << " data points." << std::endl;

    // Remove the planar inliers, extract the rest
    extract.setNegative (true);
    extract.filter (*cloud_f);              
    *cloud_filtered = *cloud_f;
  }

  // Creating the KdTree object for the search method of the extraction
  pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
  tree->setInputCloud (cloud_filtered);

  std::vector<pcl::PointIndices> cluster_indices;
  pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
  ec.setClusterTolerance (0.02); // 2cm
  ec.setMinClusterSize (50);
  ec.setMaxClusterSize (46000);
  ec.setSearchMethod (tree);
  ec.setInputCloud (cloud_filtered);
  ec.extract (cluster_indices);               //这里一直为0,下面的循环就直接跳过,无法提取出分割后的PCD

  int j = 0;
  for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
  {
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
    for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)
      cloud_cluster->points.push_back (cloud_filtered->points[*pit]); //*
    cloud_cluster->width = cloud_cluster->points.size ();
    cloud_cluster->height = 1;
    cloud_cluster->is_dense = true;

    std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
    std::stringstream ss;
    ss << "cloud_cluster_" << j << ".pcd";
    writer.write<pcl::PointXYZ> (ss.str (), *cloud_cluster, false); //*
    j++;

    pcl::visualization::PCLVisualizer viewer("Cloud Viewer"); //窗口显示第pit个聚类
    //pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGBA> color(cloud_cluster); //显示点云RGB信息
    viewer.addPointCloud(cloud_cluster);
    viewer.setBackgroundColor(1,1,1);

    while (!viewer.wasStopped ())
    {
     viewer.spinOnce();
    }
  }

  return (0);
}







求大神指点啊!谢谢。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册加入PCL中国点云技术相关产学研社区

x
回复

使用道具 举报

发表于 2014-3-13 11:29:40 | 显示全部楼层
你能可视化下你去除平面后的点数据吗,不会都成很小的离散区域了吧,或40改成1,确认逻辑上没错啊。
回复 支持 反对

使用道具 举报

发表于 2014-3-13 11:30:02 | 显示全部楼层
你能可视化下你去除平面后的点数据吗,不会都成很小的离散区域了吧,或40改成1,确认逻辑上没错啊。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-13 22:30:35 | 显示全部楼层
mypcl 发表于 2014-3-13 11:30
你能可视化下你去除平面后的点数据吗,不会都成很小的离散区域了吧,或40改成1,确认逻辑上没错啊。 ...

呀,真的成了离散点。。谢谢。我想把房屋面单独提取出来,请问这个能实现吗?刚接触点云,白纸一张,求指教,谢谢。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-17 22:24:07 | 显示全部楼层
mypcl 发表于 2014-3-13 11:30
你能可视化下你去除平面后的点数据吗,不会都成很小的离散区域了吧,或40改成1,确认逻辑上没错啊。 ...

去平面后的点云能显示,就是欧式距离提取那块提取不了,把 ec.setMinClusterSize (50),50改成1就可以。
回复 支持 反对

使用道具 举报

发表于 2014-3-22 10:07:16 | 显示全部楼层
timlee 发表于 2014-3-13 22:30
呀,真的成了离散点。。谢谢。我想把房屋面单独提取出来,请问这个能实现吗?刚接触点云,白纸一张,求指 ...

你把去除后的数据图片贴下,主要是看数据了,如果比较均匀的话,你设置聚类的距离大点,应该可以吧。主要看数据了。如果数据不是比较均匀的话,你可能需要想想其他算法了。
回复 支持 反对

使用道具 举报

发表于 2014-3-22 10:10:31 | 显示全部楼层
timlee 发表于 2014-3-13 22:30
呀,真的成了离散点。。谢谢。我想把房屋面单独提取出来,请问这个能实现吗?刚接触点云,白纸一张,求指 ...

你的数据看的不是太清楚,如果房屋的特征等比较明显,应该分割出来没问题吧,只是算法找合适就行了
回复 支持 反对

使用道具 举报

本版积分规则

QQ|小黑屋|点云技术相关产学研社区 ( 陕ICP备13001629号 )

GMT+8, 2024-5-7 00:44 , Processed in 9.097795 second(s), 17 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表