ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

PCL-ROS run time error while doing plane segmentation.

asked 2013-01-02 18:44:35 -0600

metal gravatar image

updated 2013-01-02 19:11:26 -0600

Hello everyone, I am trying to do plane segmentation using PCL and ROS. I am using the sensor Asus xtion pro live. I was able to launch openni_kinect and got the sensor pumping out necessary coordinates.

Here is my program:

ros::Publisher pub;
sensor_msgs::PointCloud2::Ptr downsampled,output;   

void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
pcl::PointCloud<pcl::pointxyz>::Ptr output_p(new pcl::PointCloud<pcl::pointxyz>);
pcl::PointCloud<pcl::pointxyz>::Ptr downsampled_XYZ(new pcl::PointCloud<pcl::pointxyz>);
 output_p->width  = 15;
  output_p->height = 1;
  output_p->points.resize (output_p->width * output_p->height);

downsampled_XYZ->width  = 15;
  downsampled_XYZ->height = 1;
  downsampled_XYZ->points.resize (output_p->width * output_p->height);

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


  // Publish the model coefficients

int main (int argc, char** argv)
  // Initialize ROS
  ros::init (argc, argv, "pcs200");
  ros::NodeHandle nh;

  // Create a ROS subscriber for the input point cloud
  ros::Subscriber sub = nh.subscribe ("/camera/depth/points", 100, cloud_cb);

  // Create a ROS publisher for the output point cloud
  pub = nh.advertise<sensor_msgs::pointcloud2> ("output/points", 100);

  // Spin
  ros::spin ();

so the run time error is:

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

I am aware of the shared pointer issue through this post . As a matter of fact the code developed is based on this.

I know the answer is very near ,Can you guys please help me out on this?. Thanks.


edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-01-03 04:50:48 -0600

jlowenz gravatar image

If you haven't figured it out already (or it's an omission in your code copying), it looks like the downsampled cloud pointer has not been initialized with a valid cloud. You pass the dereferenced shared pointer in the sor.filter(*downsampled) line...

At some point, you need: downsampled.reset(new sensor_msgs::PointCloud2)

In addition, the output cloud pointer has not been initialized, so the call to pcl::toROSMsg(*output_p, *output) will also generate an error.

If that is not the problem, you'll probably have to provide some more information (like a backtrace from gdb or something)...

Hope this helps.

edit flag offensive delete link more


downsampled->width = 15; downsampled->height = 1; output->width=15; output->height=1; As you can see I did the intialization,but it still shows the same run-time error.

metal gravatar image metal  ( 2013-01-03 07:58:27 -0600 )edit

Except that the code you posted says downsampled_XYZ->width = 15; ... and I still don't see where you call new sensor_msgs::PointCloud2 anywhere.

jlowenz gravatar image jlowenz  ( 2013-01-03 10:03:03 -0600 )edit

Question Tools


Asked: 2013-01-02 18:44:35 -0600

Seen: 1,431 times

Last updated: Jan 03 '13