brad
brad

Reputation: 954

How can I get the PointIndices of a given PointCloud?

For example:

pcl::PointCloud<pcl::PointXYZI>::Ptr example_cloud(new pcl::PointCloud<pcl::PointXYZI>);

//cloud is populated

pcl::PointIndices::Ptr point_indices(new pcl::PointIndices); 

How do I populate point_indices with the indices of example_cloud?

Upvotes: 2

Views: 6444

Answers (2)

In my case, I was using RANSAC with PCL, and the object pcl::RandomSampleConsensus has the method getInliers(vector<int>&), and with this method, I got the indices of the result after applying RANSAC. The vector of getInliers(vector<int>&) can be assigned to your point_indices. In the end, I needed a method, as you answered in a comment, for the subtraction of a point cloud from another. The code for the subtraction of point clouds is next:

void subtract_points(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, const vector<int>& inliers)
{
  pcl::PointIndices::Ptr fInliers (new pcl::PointIndices);

  fInliers->indices = inliers;

  pcl::ExtractIndices<pcl::PointXYZ> extract;
  extract.setInputCloud (cloud);
  extract.setIndices (fInliers);
  extract.setNegative(true);
  extract.filter(*cloud);
}

The concrete code for obtain the vector of indices is (with RANSAC):

vector<int> inliers;
// Plane model
pcl::SampleConsensusModelPlane<pcl::PointXYZ>::Ptr model_p (new pcl::SampleConsensusModelPlane<pcl::PointXYZ> (cloud));

pcl::RandomSampleConsensus<pcl::PointXYZ> ransac (model_p);
ransac.getInliers(inliers);

As you can see, later we can define a PointIndice object and assign our vector of indices directly: point_indices->indices = my_indices

I hope it helps someone looking for the same (the code for the subtraction of point clouds works).

Upvotes: 1

Ardiya
Ardiya

Reputation: 725

The other answer with pcl::ExtractIndices works great, but I just want to add the possibilities of using pcl::copyPointCloud (const pcl::PointCloud< PointT > &cloud_in, const std::vector< pcl::PointIndices > &indices, pcl::PointCloud< PointT > &cloud_out)

So that you can use it like this

pcl::PointCloud<pcl::PointXYZI>::Ptr example_cloud(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PointCloud<pcl::PointXYZI>::Ptr extracted_cloud(new pcl::PointCloud<pcl::PointXYZI>);
pcl::PointIndices::Ptr point_indices(new pcl::PointIndices); 

pcl::copyPointCloud(*example_cloud, *point_indices, *extracted_cloud);

Also, if you are looking for the difference, you can try to use c++ set_difference. PointIndices::indices is essentially a std::vector so you can follow the c++ tutorial link to do the difference.

Upvotes: 0

Related Questions