Search code examples
kinectpoint-cloudspoint-cloud-library

PCL create a pcd cloud


This is what I have so far and I want to save pcd file from it I know I have to do something like this but not exactly sure pcl::PointCloud::PointPointXYZRGBA> cloud; pcl::io:;savePCDFileASCII("test.pcd",cloud);

what do i have to add in my current code that i will have test.pcd Thanks

    #include <pcl/point_cloud.h>
    #include <pcl/point_types.h>
    #include <pcl/io/openni_grabber.h>
    #include <pcl/visualization/cloud_viewer.h>
    #include <pcl/common/time.h>

    class SimpleOpenNIProcessor
    {
        public:
            SimpleOpenNIProcessor () : viewer ("PCL OpenNI Viewer") {}
            void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr &cloud)
            {
                static unsigned count = 0;
                static double last = pcl::getTime ();
                if (++count == 30)
                {
                    double now = pcl::getTime ();
                    std::cout << "distance of center pixel :" << cloud->points [(cloud->width >> 1) * (cloud->height + 1)].z << " mm. Average framerate: " << double(count)/double(now - last) << " Hz" <<  std::endl;
                    count = 0;
                    last = now;
                }
                if (!viewer.wasStopped())
                    viewer.showCloud (cloud);
            }

            void run ()
            {
                // create a new grabber for OpenNI devices
                pcl::Grabber* interface = new pcl::OpenNIGrabber();

                // make callback function from member function
                boost::function<void (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&)> f =
                    boost::bind (&SimpleOpenNIProcessor::cloud_cb_, this, _1);

                // connect callback function for desired signal. In this case its a point cloud with color values
                boost::signals2::connection c = interface->registerCallback (f);

                // start receiving point clouds
                interface->start ();

                // wait until user quits program with Ctrl-C, but no busy-waiting -> sleep (1);
                while (true)
                    boost::this_thread::sleep (boost::posix_time::seconds (1));

                // stop the grabber
                interface->stop ();
            }

            pcl::visualization::CloudViewer viewer;
    };

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

Solution

  • #include <iostream>
    #include <string>
    #include <sstream>
    
    #include <pcl/io/pcd_io.h>
    #include <pcl/point_types.h>
    #include <pcl/io/openni_grabber.h>
    #include <pcl/visualization/cloud_viewer.h>
    
    using namespace std; 
    
    const string OUT_DIR = "D:\\frame_saver_output\\"; 
    
    class SimpleOpenNIViewer 
    { 
    public: 
        SimpleOpenNIViewer () : viewer ("PCL Viewer") 
        { 
                    frames_saved = 0; 
                    save_one = false; 
        } 
    
        void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr &cloud) 
        { 
                    if (!viewer.wasStopped()) { 
                            viewer.showCloud (cloud); 
    
                            if( save_one ) { 
                                    save_one = false; 
                                    std::stringstream out; 
                                    out << frames_saved; 
                                    std::string name = OUT_DIR + "cloud" + out.str() + ".pcd"; 
                                    pcl::io::savePCDFileASCII( name, *cloud ); 
                            } 
                    } 
        } 
    
        void run () 
        { 
                    pcl::Grabber* interface = new pcl::OpenNIGrabber(); 
    
                    boost::function<void (const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr&)> f = 
                            boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1); 
    
                    interface->registerCallback (f); 
    
                    interface->start (); 
    
                    char c; 
    
                    while (!viewer.wasStopped()) 
                    { 
                            //sleep (1); 
    
                            c = getchar(); 
                            if( c == 's' ) { 
                                    cout << "Saving frame " << frames_saved << ".\n"; 
                                    frames_saved++; 
                                    save_one = true; 
                            } 
                    } 
    
                    interface->stop (); 
            } 
    
            pcl::visualization::CloudViewer viewer; 
    
            private: 
                    int frames_saved; 
                    bool save_one; 
    
    }; 
    
    int main () 
    { 
        SimpleOpenNIViewer v; 
        v.run (); 
        return 0; 
    } 
    

    Here you go.