Segmentation fault (core dumped) when try to visualize point clouds culster

asked 2020-06-15 23:18:24 -0500

Astronaut gravatar image

updated 2020-06-15 23:30:32 -0500

Hi. I was following the PCL tutorial Euclidean Cluster Extraction. The code was compiled without error but when run it gives me the following error

PointCloud representing the planar component: 2874 data points. Segmentation fault (core dumped)

Here the original point clouds and the original image original point clouds original image

Here is the code

ros::Publisher pub, markers_pub_;

void cloud_cb(const sensor_msgs::PointCloud2::ConstPtr &msg){

    //Voxel Filtering 
    // Convert ros_pcl2 to pcl2
    pcl::PCLPointCloud2::Ptr input_cloud (new pcl::PCLPointCloud2 ());
    pcl_conversions::toPCL(*msg,*input_cloud);

    //Deklaration cloud pointers for Voxel
    pcl::PCLPointCloud2::Ptr cloud_filtered_voxel (new pcl::PCLPointCloud2 ());

    //Voxel Filter
    pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
    sor.setInputCloud (input_cloud);
    sor.setLeafSize (0.01f, 0.01f, 0.01f);
    sor.filter (*cloud_filtered_voxel);

    //StatisticalOutlierRemoval Filetring
    //Convert pcl2 to pclxyzrgba
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_sor_input (new pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::fromPCLPointCloud2(*cloud_filtered_voxel, *cloud_sor_input);

    //Deklaration cloud pointers for SOR
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_sor_filtered (new pcl::PointCloud<pcl::PointXYZRGB>);


    //statistical outliner removal
    pcl::StatisticalOutlierRemoval<pcl::PointXYZRGB> sor1;
    sor1.setInputCloud (cloud_sor_input);
    sor1.setMeanK (6); 
    sor1.setStddevMulThresh (0.04);  
    sor1.filter (*cloud_sor_filtered);

    //Deklaration cloud pointers
    //pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered_x(new pcl::PointCloud<pcl::PointXYZRGB>);
    //pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered_y(new pcl::PointCloud<pcl::PointXYZRGB>);
    //pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered_z(new pcl::PointCloud<pcl::PointXYZRGB>);

    // Get segmentation ready
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_plane (new pcl::PointCloud<pcl::PointXYZRGB> ());
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_f (new pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
    pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
    pcl::SACSegmentation<pcl::PointXYZRGB> seg;
    pcl::ExtractIndices<pcl::PointXYZRGB> extract;
    seg.setOptimizeCoefficients (true);
    seg.setModelType (pcl::SACMODEL_PLANE);
    seg.setMethodType (pcl::SAC_RANSAC);
    seg.setMaxIterations (100);// orig bez ova 
    seg.setDistanceThreshold(0.09); //original 0.09

    // Create pointcloud to publish inliers
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_pub(new pcl::PointCloud<pcl::PointXYZRGB>);

    int i=0, nr_points = (int) cloud_sor_filtered->points.size ();
    while (cloud_sor_filtered->points.size () > 0.1 * nr_points)
    {
        // Segment the largest planar component from the remaining cloud
        seg.setInputCloud (cloud_sor_filtered);
        seg.segment (*inliers, *coefficients);
        if (inliers->indices.size () == 0)
        {
            std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
            break;
        }

        // Extract the planar inliers from the input cloud
        pcl::ExtractIndices<pcl::PointXYZRGB> extract;
        extract.setInputCloud (cloud_sor_filtered);
        extract.setIndices (inliers);
        extract.setNegative (false);

        // Get the points associated with the planar surface
        extract.filter (*cloud_plane);
        std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size () << " data points." << std::endl;

        // Remove the planar inliers, extract the rest
        extract.setNegative (true);
        extract.filter (*cloud_f);
        *cloud_sor_filtered = *cloud_f;
      }

      // Euclidean Cluster
      // Creating the KdTree object for the search method of the extraction
      pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB>);
      tree->setInputCloud (cloud_sor_filtered);// cloud_sor_filtered

      // create the extraction object for the clusters
      std::vector<pcl::PointIndices> *cluster_indices;
      //pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_box (new pcl::PointCloud<pcl::PointXYZRGB> ());
      pcl::EuclideanClusterExtraction<pcl::PointXYZRGB> ec;
      // specify euclidean cluster parameters
      ec.setClusterTolerance (0.09); // 2cm
      ec.setMinClusterSize (100);
      ec.setMaxClusterSize (25000);
      ec.setSearchMethod (tree);
      ec.setInputCloud (cloud_sor_filtered);// cloud_sor_filtered
      ec.extract (*cluster_indices);
         sensor_msgs::PointCloud2 cloud_publish;
     pcl::toROSMsg(*cloud_f, cloud_publish);
     pub.publish(cloud_publish);
}

int
main (int argc, char** argv)
{
  // Initialize ROS
  ros::init (argc, argv, "my_pcl_tutorial");
  ros::NodeHandle nh;
  ros::Subscriber sub = nh.subscribe ("/zed/zed_node/point_cloud/cloud_registered ...
(more)
edit retag flag offensive close merge delete