點云索引其實就是將點云中不同點加上標(biāo)簽诡必,方便后面的分類提取搔扁。有了點云的索引值可以方便的對點云進(jìn)行不同操作:
以下舉例說明:(代碼僅顯示主要部分,忽略模型設(shè)置部分)
(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);//將對應(yīng)索引的點存儲
如indexs代表點云中某些特定的點稿蹲,如排序為第1,2,5三個點,這些順序通常是有kdtree得到的涂炎。如果沒有這個參數(shù)焰盗,就是將cloud中所有的點全部填入到cloudout。
(2)通過索引迭代輸出每個聚類對應(yīng)的點云
pcl::EuclideanClusterExtractionec; ec.extract (cluster_indices);
std::cerr << cluster_indices.size() << std::endl; //輸出聚類的數(shù)目 int j = 0;//通過兩次迭代熬拒,總共產(chǎn)生j個聚類
for (std::vector::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)//迭代j次將j個聚類中對應(yīng)索引的點存到j(luò)個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 ()下面對應(yīng)的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)保存模型(平面,圓柱等等)分割中的內(nèi)點
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);//計算模型內(nèi)點索引
extract.setInputCloud (cloud_filtered);//輸入點云
extract.setIndices (inliers_plane);//輸入模型內(nèi)點索引
pcl::PointCloud::Ptr cloud_plane (new pcl::PointCloud());
extract.filter (*cloud_plane);//根據(jù)輸入點云和模型內(nèi)點索引分割出內(nèi)點對應(yīng)點云
writer.write ("table_scene_mug_stereo_textured_plane.pcd", *cloud_plane, false);//存儲模型內(nèi)點對應(yīng)點云
(4)待補充