点云索引其实就是将点云中不同点加上标签,方便后面的分类提取。有了点云的索引值可以方便的对点云进行不同操作:
以下举例说明:(代码仅显示主要部分,忽略模型设置部分)
(1)保存一点云中某些特定的点
pcl::PointCloud::Ptr cloud(new pcl::PointCloud);//输入点云 pcl::io::loadPCDFile("~/xxx.pcd", *cloud); pcl::PointCloud::Ptr cloudOut(new pcl::PointCloud);//输出点云 std::vectorindexs = { 1, 2, 5 };//声明索引值pcl::copyPointCloud(*cloud, indexs, *cloudOut);//将对应索引的点存储
如indexs代表点云中某些特定的点,如排序为第1,2,5三个点,这些顺序通常是有kdtree得到的。如果没有这个参数,就是将cloud中所有的点全部填入到cloudout。
(2)通过索引迭代输出每个聚类对应的点云
pcl::EuclideanClusterExtractionec; ec.extract (cluster_indices);
std::cerr << cluster_indices.size() << std::endl; //输出聚类的数目 int j = 0;//通过两次迭代,总共产生j个聚类
for (std::vector::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)//迭代j次将j个聚类中对应索引的点存到j个pcd中
{ pcl::PointCloud::Ptr cloud_cluster (new pcl::PointCloud);
for (std::vector::const_iterator pit = it->indices.begin (); pit != it->indices.end (); pit++)//每个it =cluster_indices.begin ()下面对应的it->indices.begin ()代表单个聚类包含的索引,indices[i]代表单个聚类中的第i个点
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 << "/home/exbot/文档/pp/pcd/cloud_cluster_" << j << ".pcd"; writer.write(ss.str (), *cloud_cluster, false);
j++;
/* Dynamic naming
std::cout << "Cluster " << currentClusterNum << " has " << cluster->points.size() << " points." << std::endl;
std::string fileName = clusterType+"_cluster" + boost::to_string(currentClusterNum) + ".pcd";
pcl::io::savePCDFileASCII(fileName, *cluster);
currentClusterNum++;
*/
}
return (0);
(3)保存模型(平面,圆柱等等)分割中的内点
pcl::PointIndices::Ptr inliers_plane (new pcl::PointIndices);//声明指向PointIndices的共享指针
pcl::ModelCoefficients::Ptr coefficients_plane;
pcl::SACSegmentationFromNormalsseg;
seg.setInputCloud (cloud_filtered);
seg.setInputNormals (cloud_normals);
seg.segment (*inliers_plane, *coefficients_plane);//计算模型内点索引
extract.setInputCloud (cloud_filtered);//输入点云
extract.setIndices (inliers_plane);//输入模型内点索引
pcl::PointCloud::Ptr cloud_plane (new pcl::PointCloud());
extract.filter (*cloud_plane);//根据输入点云和模型内点索引分割出内点对应点云
writer.write ("table_scene_mug_stereo_textured_plane.pcd", *cloud_plane, false);//存储模型内点对应点云
(4)待补充