Rosanswers logo

Hello all,

I am trying to write a program that subscribes to the openni_launch's pointcloud topic, downsamples the data, and publishes an extracted pointcloud plane. So far the code compiles fine but upon running it produces this error:

/usr/include/boost/smart_ptr/shared_ptr.hpp:412: boost::shared_ptr::reference boost::shared_ptr::operator*() const [with T = sensor_msgs::PointCloud2_std::allocator<void >, boost::shared_ptr::reference = sensor_msgs::PointCloud2_std::allocator<void >&]: Assertion `px != 0' failed. Aborted (core dumped)

I am unsure of where the error is coming from and would appreciate any insight. Here is the code I have written thus far:

   // Includes and whatnot

    ros::Publisher pub;
    sensor_msgs::PointCloud2::Ptr downsampled,output;
    pcl::PointCloud<pcl::PointXYZ>::Ptr output_p, downsampled_XYZ;
    void callback(const sensor_msgs::PointCloud2ConstPtr& input)
      // Do some downsampling to the point cloud
      pcl::VoxelGrid<sensor_msgs::PointCloud2> sor;
      sor.setInputCloud (input);
      sor.setLeafSize (0.01f, 0.01f, 0.01f);
      sor.filter (*downsampled);
      // Change from type sensor_msgs::PointCloud2 to pcl::PointXYZ
      pcl::fromROSMsg (*downsampled, *downsampled_XYZ);
      pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ());
      pcl::PointIndices::Ptr inliers (new pcl::PointIndices ());
      // Create the segmentation object
      pcl::SACSegmentation<pcl::PointXYZ> seg;
      // Optional
      seg.setOptimizeCoefficients (true);
      // Mandatory
      seg.setModelType (pcl::SACMODEL_PLANE);
      seg.setMethodType (pcl::SAC_RANSAC);
      seg.setMaxIterations (1000);
      seg.setDistanceThreshold (0.01);
      // Create the filtering object
      pcl::ExtractIndices<pcl::PointXYZ> extract;
      // Segment the largest planar component from the cloud
      seg.setInputCloud (downsampled_XYZ);
      seg.segment (*inliers, *coefficients);
      if (inliers->indices.size () == 0)
              std::cerr << "Could not estimate a planar model for the given dataset." << std::endl;
      // Extract the inliers
      extract.setInputCloud (downsampled_XYZ);
      extract.setIndices (inliers);
      extract.setNegative (false);
      extract.filter (*output_p);
      std::cerr << "PointCloud representing the planar component: " << output_p->width * output_p->height << " data points." << std::endl;
      // Create the filtering object
      // extract.setNegative (true);
      // extract.filter (*cloud_f);
      // cloud_filtered.swap (cloud_f);
      pcl::toROSMsg (*output_p, *output);
      //Publish the results
    main (int argc, char** argv)
      ros::init (argc, argv, "table");
      ros::NodeHandle nh;
      ros::Subscriber sub = nh.subscribe("/camera/depth_registered/points", 1, callback);
      pub = nh.advertise<sensor_msgs::PointCloud2> ("table", 1);
      return (0);

Thank you in advance!

Cheers, Martin

Originally posted by MartinW on ROS Answers with karma: 464 on 2012-07-25

Post score: 1

Original comments

Comment by MartinW on 2012-07-25:
Found my answer here: http://stackoverflow.com/questions/3541179/shared-ptr-assertion-px-0-failed


2 Answers 2


Rosanswers logo

I am guessing that you should allocate the memory for output_p and downsampled_XYZ when you enter the callback.

Also, one more advice, after

pcl::toROSMsg (*output_p, *output);

You should set the frame_id of output to the same value as input

output.header.frame_id = input->header.frame_id;

otherwise if you try to visualize the point cloud with rviz, it will complain because it would not know the frame of reference for output.

Originally posted by Martin Peris with karma: 5625 on 2012-07-25

This answer was ACCEPTED on the original site

Post score: 3

Original comments

Comment by MartinW on 2012-07-26:
Thanks! I got my program running and I've extracted the pointcloud plane of a table in front of my Kinect! I tried to add your line of code "output.header.frame_id = input->header.frame_id;" But when I did it said sensor_msgs::pointcloud2 has no member header. But I could see it in Rviz anyway!


Rosanswers logo

pcl::PointCloudpcl::PointXYZ::Ptr output_p(new pcl::PointCloudpcl::PointXYZ);

That did it for me :)

Originally posted by sharky with karma: 36 on 2013-12-01

This answer was NOT ACCEPTED on the original site

Post score: 2


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.