Search code examples
kinectopennipoint-cloudspoint-cloud-library

Using Point Cloud Library to store Point Clouds from Kinect


Using Point Cloud Library on Ubuntu, I am trying to take multiple point clouds from the Kinect and store them in memory for later use in the program. My code shown at the bottom of this post is designed to store the first Point Cloud from the Kinect and output its width and height. The program gives me a runtime error:

/usr/include/boost/smart_ptr/shared_ptr.hpp:418: T* boost::shared_ptr<T>::operator->() const [with T = pcl::PointCloud<pcl::PointXYZ>]: Assertion `px != 0' failed.

All help is greatly appreciated and I always accept an answer!

The code:

  #include <pcl/io/openni_grabber.h>
  #include <pcl/visualization/cloud_viewer.h>

 class SimpleOpenNIViewer
 {
  public:
  SimpleOpenNIViewer () : viewer ("PCL OpenNI Viewer") {}

 pcl::PointCloud<pcl::PointXYZ>::Ptr prevCloud;


  void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &cloud)
  {


if (!viewer.wasStopped())
   viewer.showCloud (cloud);


//ICP start
if(!prevCloud) {
    pcl::PointCloud<pcl::PointXYZ>::Ptr prevCloud( new pcl::PointCloud<pcl::PointXYZ>());

    pcl::copyPointCloud<pcl::PointXYZ, pcl::PointXYZ>(*cloud, *prevCloud);
}

cout << prevCloud->width << " by " << prevCloud->height << endl; 



  }

  void run ()
  {
    pcl::Grabber* interface = new pcl::OpenNIGrabber();

    boost::function<void (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)> f =
      boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);


    interface->registerCallback (f);

    interface->start ();


    while (!viewer.wasStopped())
    {
      boost::this_thread::sleep (boost::posix_time::seconds (1));
    }

    interface->stop ();
  }

  pcl::visualization::CloudViewer viewer;
 };

  int main ()
{
 SimpleOpenNIViewer v;
 v.run ();
 return 0;
}

Solution

  • Try this, I don't have the Kinect drivers installed so I can't test. Basically in my version prevCloud is instantiated in the constructor so (!prevCloud) will always equal 'false'. Which is to say prevCloud.get() != NULL.

    #include <pcl/io/openni_grabber.h>
    #include <pcl/visualization/cloud_viewer.h>
    
    class SimpleOpenNIViewer
    {
    typedef pcl::PointXYZ                           Point;
    typedef pcl::PointCloud<Point>                  PointCloud;
    public:
    SimpleOpenNIViewer () : viewer ("PCL OpenNI Viewer") {
            prevCloud = PointCloud::Ptr(NULL);
        }
    
    void cloud_cb_ (const PointCloud::ConstPtr &cloud)
    {
        if (!viewer.wasStopped())
            viewer.showCloud (cloud);
                if (!prevCloud) // init previous cloud if first frame
                        prevCloud = PointCloud::Ptr(new PointCloud);
                else.   // else RunICP between cloud and prevCloud
                        //RunICP(cloud,prevCloud);
    
                //Copy new frame in to prevCloud
        pcl::copyPointCloud<Point, Point>(*cloud, *prevCloud);
        cout << prevCloud->width << " by " << prevCloud->height << endl; 
    }
    
    void run ()
    {
        pcl::Grabber* interface = new pcl::OpenNIGrabber();
    
        boost::function<void (const PointCloud::ConstPtr&)> f =
        boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
    
        interface->registerCallback (f);
        interface->start ();
    
    
        while (!viewer.wasStopped())
        {
            boost::this_thread::sleep (boost::posix_time::seconds (1));
        }
    
        interface->stop ();
    }
    
    PointCloud::Ptr prevCloud;
    pcl::visualization::CloudViewer viewer;
    };
    
    int main ()
    {
    SimpleOpenNIViewer v;
    v.run ();
    return 0;
    }