使用Point Cloud Library从Kinect存储点云

时间:2012-07-23 01:59:32

标签: kinect openni point-clouds point-cloud-library

在Ubuntu上使用Point Cloud Library,我试图从Kinect中获取多个点云并将它们存储在内存中以供以后在程序中使用。我在本文底部显示的代码旨在存储Kinect中的第一个Point Cloud并输出其宽度和高度。该程序给我一个运行时错误:

/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.

非常感谢所有帮助,我总是接受答案!

代码:

  #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;
}

3 个答案:

答案 0 :(得分:2)

试试这个,我没有安装Kinect驱动程序,所以我无法测试。基本上在我的版本中,prevCloud在构造函数中实例化,因此(!prevCloud)将始终等于'false'。也就是说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;
}

答案 1 :(得分:0)

您正在创建新的本地变量prevCloud并将cloud复制到其中,而不是复制到prevCloud字段。因此,如果字段的值在if {}之前为空,则在if {}之后它仍然为空,因此当您尝试取消引用它时会抛出错误。

答案 2 :(得分:0)

可能这段代码可以帮助你,将云保存在“pcd”文件中,看看here

其他选择是与PCL的“Kinfu”项目合作