版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lch_vison/article/details/80691428
利用pcl进行点云索引提取时,经常需要迭代分割提取多块子点云,为了方便存储管理,通常使用向量来进行。这时有两种思路存储分割后的子点云:一种是将子点云的索引存入向量,另一种是将点云存入向量。
需要的注意的是,如果将子点云的索引存入向量,容易出现问题:循环迭代的输入点云发生了变化,所以得到的子点云索引是新输入点云中的索引,并非最初的输入点云的索引。所以,最可靠的方法是将子点云存入向量。这时关于点云向量的定义如下:
std::vector<pcl::PointCloud<pcl::PointXYZ>, Eigen::aligned_allocator<pcl::PointXYZ> > clouds_vector;
分割提取多个子点云,并存入点云向量的代码如下:
int i = 0, nr_points = (int)cloud2->points.size();
while (cloud2->points.size()>0.3*nr_points){
pcl::PointCloud<PointT> cloud;
// 从余下的点云中分割最大平面组成部分
seg.setInputCloud(cloud2);
seg.segment(*inliers, *coefficients);
if (inliers->indices.size()==0)
{
cout << "Could not estimate the model for the given dataset." << endl;
break;
}
//分离内存
extract.setInputCloud(cloud2);
extract.setIndices(inliers); //设置分割后的内点为需要提取的点集
extract.setNegative(false); //设置提取内点而非外点
extract.filter(cloud); //提取输出存储到 cloud
cout << "PointCloud representing the model component: " << cloud.width * cloud.height << " data points." <<endl;
clouds_vector.push_back(cloud); //将点云存入点云向量。
// 创建滤波器对象, 剔除已经判断的模型点
extract.setNegative(true);
extract.filter(*cloud_f);
cloud2->swap(*cloud_f); //j交换两个点云
i++;
}
Enjoy!