Search code examples
c++point-cloud-librarypoint-cloudssegment

Region growing segmentation clusters are wrong?


I am performing a region growing segmentation of a point cloud I have of my room via PCL point cloud library. The colored cloud looks like the following: colored cloud

As you can see most of the clusters look according to the surface. However, when i show each cluster separatedly, these are some of the results: results 1

results 2

Clearly the clusters are not the same as in the colored cloud, but I dont understand why. I am using this code to store the clusters into separated point clouds:

//Store clusters into new pcls and all the clusters in an array of pcls 
    std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> clusters_pcl; 
    for (int i = 0; i < clusters.size(); ++i) { 
            pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster( 
                            new pcl::PointCloud<pcl::PointXYZRGB>); 
            cloud_cluster->width = clusters[i].indices.size(); 
            cloud_cluster->height = 1; 
            cloud_cluster->is_dense = true; 
            for (int j = 0; j < clusters[i].indices.size(); ++j) { 
                    //Take the corresponding point of the filtered cloud from the indices for the new pcl 
                    cloud_cluster->push_back( 
                                    point_cloud_ptr->at(clusters[i].indices[j])); 
            } 
            indices2.clear(); 
            //pcl::removeNaNFromPointCloud(*cloud_cluster, *cloud_cluster, indices2); 
            clusters_pcl.push_back(cloud_cluster); 
    } 

Is it my code that is doing something wrong or is the output of the region growing segmentation actually right?

Cheers

-------------EDIT-----------------

Here is the point cloud I am using for the tests.

Here is the complete region growing segmentation code, it is similar to the one in the tutorial:

std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> region_growing_segmentation(
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr) {
pcl::PointCloud<pcl::PointXYZRGB>& point_cloud = *point_cloud_ptr;
std::vector<int> indices2;
// Create the filtering object: downsample the dataset using a leaf size of 1cm
pcl::VoxelGrid<pcl::PointXYZRGB> vg;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered(
        new pcl::PointCloud<pcl::PointXYZRGB>);
vg.setInputCloud(point_cloud_ptr);
vg.setLeafSize(0.025f, 0.025f, 0.025f);
vg.filter(*cloud_filtered);
std::cout << "PointCloud after filtering has: "
        << cloud_filtered->points.size() << " data points." << std::endl;

pcl::search::Search<pcl::PointXYZRGB>::Ptr tree = boost::shared_ptr<
        pcl::search::Search<pcl::PointXYZRGB> >(
        new pcl::search::KdTree<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> normal_estimator;
normal_estimator.setSearchMethod(tree);
normal_estimator.setInputCloud(cloud_filtered);
normal_estimator.setKSearch(50);
normal_estimator.compute(*normals);

pcl::RegionGrowing<pcl::PointXYZRGB, pcl::Normal> reg;
reg.setMinClusterSize(50);
reg.setMaxClusterSize(1000000);
reg.setSearchMethod(tree);
reg.setNumberOfNeighbours(100);
reg.setInputCloud(cloud_filtered);
reg.setInputNormals(normals);
reg.setSmoothnessThreshold(5.0 / 180.0 * M_PI);
reg.setCurvatureThreshold(1);

std::vector<pcl::PointIndices> clusters;
reg.extract(clusters);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr colored_cloud =
        reg.getColoredCloud();
pcl::visualization::CloudViewer viewer("Cluster viewer");
viewer.showCloud(colored_cloud);
while (!viewer.wasStopped()) {
}
std::vector<pcl::PointCloud<pcl::PointXYZRGB>::Ptr> clusters_pcl;
 for (int i = 0; i < clusters.size(); ++i) {
 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_cluster(
 new pcl::PointCloud<pcl::PointXYZRGB>);
 cloud_cluster->width = clusters[i].indices.size();
 cloud_cluster->height = 1;
 cloud_cluster->is_dense = true;
 for (int j = 0; j < clusters[i].indices.size(); ++j) {
 //Take the corresponding point of the filtered cloud from the indices for the new pcl
 cloud_cluster->push_back(
 point_cloud_ptr->at(clusters[i].indices[j]));
 }
 indices2.clear();
 //pcl::removeNaNFromPointCloud(*cloud_cluster, *cloud_cluster, indices2);
 clusters_pcl.push_back(cloud_cluster);
 }

return clusters_pcl;
}

Solution

  • So I just figured it out, it was too simple I could not see it; sorry. When I was copying the points into the clusters I was using the original point cloud instead of the filtered one. Maybe as results where like that I did not even think of this.

    So this:

    cloud_cluster->push_back(
                    point_cloud_ptr->at(clusters[i].indices[j]));
    

    has to be replaced with:

    cloud_cluster->push_back(
                    cloud_filtered->at(clusters[i].indices[j]));
    

    Cheers