'How to get a specific cluster after region growing using pcl library?
I am trying to segment the input point cloud using surface normal based region growing, following the tutorial here:http://pointclouds.org/documentation/tutorials/region_growing_segmentation.php. I have been able to achieve satisfactory results for my use case and I could also visualize the segments using pcl::PointCloud ::Ptr colored_cloud = reg.getColoredCloud ();
However, I am not able to find a way to extract a particular cluster that contains all points with normals oriented in a particular direction, for example, the cluster which has all the points with point normals along the z-axis. I assume that such an access to the cluster should be possible, please correct me if I am wrong. Also, any alternative methods to get the feature/s of the individual cluster would be very helpful. Thanks in advance.
Solution 1:[1]
Take a look at these lines (from the tutorial you mention):
std::vector <pcl::PointIndices> clusters;
reg.extract (clusters);
Each "cluster" is repressented by pcl::PointIndices, which is just a wrapper for a vector<int> of indices in the input cloud.
Here is a simple example for calculating the mean normal for each cluster:
std::vector<Eigen::Vector3f> mean_normals(clusters.size(), Eigen::Vector3f({ 0.,0.,0. }));
for (auto cluster_idx = 0; cluster_idx < clusters.size(); cluster_idx++)
{
for (auto point_idx: clusters[cluster_idx].indices)
{
mean_normals[cluster_idx] += normals->points[point_idx].getNormalVector3fMap();
}
mean_normals[cluster_idx].normalize();
}
You may also use pcl::PointIndices to create a new cloud for each cluster using pcl::ExtractIndices, as in this tutorial. Short example:
pcl::PointCloud<pcl::PointXYZ>::Ptr cluster_cloud(new pcl::PointCloud<pcl::PointXYZ>)
pcl::ExtractIndices<pcl::PointXYZ> extract;
extract.setInputCloud (cloud);
extract.setIndices (clusters[cluster_idx]);
extract.filter (*cluster_cloud);
Solution 2:[2]
You gotta convert cluster which is pcl::PointIndices to pcl::PointIndicesConstPtr to feed it into extract.setIndices(). Here is an example:
pcl::PointCloud<pcl::PointXYZRGB>::Ptr desired_object_cloud (new pcl::PointCloud<pcl::PointXYZRGB> ());
pcl::ExtractIndices<pcl::PointXYZRGB> extract;
extract.setInputCloud (cloud);
pcl::PointIndices::Ptr cluster_ptr(new pcl::PointIndices(clusters[cluster_idx]));
extract.setIndices (cluster_ptr);
extract.setNegative (false);
extract.filter(*desired_object_cloud);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mark Loyman |
| Solution 2 | S. Alperen |
