Search code examples
point-cloud-librarypoint-clouds

Generating a point cloud from another labeld point cloud


I have a labeled point cloud data (cloud) that it's points include "x","y", "z" and "label" information while label can be 1,2 or 3.

pcl::PointCloud<pcl::PointXYZL>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZL>);

Now, I want to divide this point cloud to 3 separate point cloud according to their label. for example I want to generate a point cloud which only includes the x,y,z information of those points which their label is 1 (cloud1). I did this:

int ll=0;

pcl::PointCloud<pcl::PointXYZL>::Ptr cloud1 (new pcl::PointCloud<pcl::PointXYZL>);

for (int ii = 0; ii < cloud->points.size (); ++ii){
if(cloud->points[ii].label==1)
{

  cloud1->points[ll].x=cloud->points[ii].x;
  cloud1->points[ll].y=cloud->points[ii].y;
  cloud1->points[ll].z=cloud->points[ii].z;
  ll++;

}
}

for (int ii = 0; ii < cloud->points.size (); ++ii){
{

cloud1->points[ll].x=cloud->points[ii].x;
cloud1->points[ll].y=cloud->points[ii].y;
    cloud1->points[ll].z=cloud->points[ii].z;
ll++;
}
}

But I received "Segmentation fault (core dumped)" error. I was wondering where is the problem?


Solution

  • You're indexing into cloud1 storage vector that doesn't have a size yet. You can't do that because ll is out of bounds, which is why it segmentation faults. You need to append a new point using push_back.

    if (cloud->points[ii].label == 1)
    {
      cloud1->push_back(cloud->points[ii]);
    }