title: PCL OpenNI2Grabber get Point Cloud without viewer
body: <p>I have an Asus XTION camera. On my PC there is Windows 7 with Visual Studio 2013 installed. I also got PCL, openCV compiled for VS2013 and the openNi Driver is intalled. Now I want to get a Point Cloud from the Camera. With the sample I can view the actual frames and save one:</p>
  
  <pre><code>    // Original code by Geoffrey Biggs, taken from the PCL tutorial in
      // http://pointclouds.org/documentation/tutorials/pcl_visualizer.php
  
      // Simple OpenNI viewer that also allows to write the current scene to a .pcd
      // when pressing SPACE.
      #define _CRT_SERCURE_NO_WARNINGS
      #include &lt;pcl/io/openni2_grabber.h&gt;
      #include &lt;pcl/io/pcd_io.h&gt;
      #include &lt;pcl/visualization/cloud_viewer.h&gt;
      #include &lt;pcl/console/parse.h&gt;
      #include &lt;pcl/filters/passthrough.h&gt;
  
      #include &lt;iostream&gt;
  
      using namespace std;
      using namespace pcl;
  
      PointCloud&lt;PointXYZRGBA&gt;::Ptr cloudptr(new PointCloud&lt;PointXYZRGBA&gt;); // A cloud that will store color info.
      PointCloud&lt;PointXYZ&gt;::Ptr fallbackCloud(new PointCloud&lt;PointXYZ&gt;); // A fallback cloud with just depth data.
      PointCloud&lt;PointXYZRGBA&gt;::Ptr filteredCloud(new PointCloud&lt;PointXYZRGBA&gt;);
      boost::shared_ptr&lt;visualization::CloudViewer&gt; viewer;                 // Point cloud viewer object.
      Grabber* openniGrabber;                                               // OpenNI grabber that takes data from the device.
      unsigned int filesSaved = 0;                                          // For the numbering of the clouds saved to disk.
      bool saveCloud(false), noColor(false);                                // Program control.
  
      void
      printUsage(const char* programName)
      {
          cout &lt;&lt; "Usage: " &lt;&lt; programName &lt;&lt; " [options]"
              &lt;&lt; endl
              &lt;&lt; endl
              &lt;&lt; "Options:\n"
              &lt;&lt; endl
              &lt;&lt; "\t&lt;none&gt;     start capturing from an OpenNI device.\n"
              &lt;&lt; "\t-v FILE    visualize the given .pcd file.\n"
              &lt;&lt; "\t-h         shows this help.\n";
      }
  
      // This function is called every time the device has new data.
      void
      grabberCallback(const PointCloud&lt;PointXYZRGBA&gt;::ConstPtr&amp; cloud)
      {
          if (!viewer-&gt;wasStopped())
              viewer-&gt;showCloud(cloud);
  
          if (saveCloud)
          {
              stringstream stream;
              stream &lt;&lt; "inputCloud" &lt;&lt; filesSaved &lt;&lt; ".pcd";
              string filename = stream.str();
  
              // Filter object.
              PassThrough&lt;pcl::PointXYZRGBA&gt; filter;
              filter.setInputCloud(cloud);
              // Filter out all points with Z values not in the [0-2] range.
              filter.setFilterFieldName("z");
              filter.setFilterLimits(0.0, 1.5);
  
              filter.filter(*filteredCloud);
  
              if (io::savePCDFile(filename, *filteredCloud, true) == 0)
              {
                  filesSaved++;
                  cout &lt;&lt; "Saved " &lt;&lt; filename &lt;&lt; "." &lt;&lt; endl;
              }
              else PCL_ERROR("Problem saving %s.\n", filename.c_str());
  
              saveCloud = false;
          }
      }
  
      // For detecting when SPACE is pressed.
      void
      keyboardEventOccurred(const visualization::KeyboardEvent&amp; event,
      void* nothing)
      {
          if (event.getKeySym() == "space" &amp;&amp; event.keyDown())
              saveCloud = true;
      }
  
      // Creates, initializes and returns a new viewer.
      boost::shared_ptr&lt;visualization::CloudViewer&gt;
      createViewer()
      {
          boost::shared_ptr&lt;visualization::CloudViewer&gt; v
              (new visualization::CloudViewer("OpenNI viewer"));
          v-&gt;registerKeyboardCallback(keyboardEventOccurred);
  
          return (v);
      }
  
      int
      main(int argc, char** argv)
      {
          if (console::find_argument(argc, argv, "-h") &gt;= 0)
          {
              printUsage(argv[0]);
              return -1;
          }
  
          bool justVisualize(false);
          string filename;
          if (console::find_argument(argc, argv, "-v") &gt;= 0)
          {
              if (argc != 3)
              {
                  printUsage(argv[0]);
                  return -1;
              }
  
              filename = argv[2];
              justVisualize = true;
          }
          else if (argc != 1)
          {
              printUsage(argv[0]);
              return -1;
          }
  
          // First mode, open and show a cloud from disk.
          if (justVisualize)
          {
              // Try with color information...
              try
              {
                  io::loadPCDFile&lt;PointXYZRGBA&gt;(filename.c_str(), *cloudptr);
              }
              catch (PCLException e1)
              {
                  try
                  {
                      // ...and if it fails, fall back to just depth.
                      io::loadPCDFile&lt;PointXYZ&gt;(filename.c_str(), *fallbackCloud);
                  }
                  catch (PCLException e2)
                  {
                      return -1;
                  }
  
                  noColor = true;
              }
  
              cout &lt;&lt; "Loaded " &lt;&lt; filename &lt;&lt; "." &lt;&lt; endl;
              if (noColor)
                  cout &lt;&lt; "This cloud has no RGBA color information present." &lt;&lt; endl;
              else cout &lt;&lt; "This cloud has RGBA color information present." &lt;&lt; endl;
          }
          // Second mode, start fetching and displaying frames from the device.
          else
          {
              openniGrabber = new pcl::io::OpenNI2Grabber();
              if (openniGrabber == 0)
                  return -1;
              boost::function&lt;void(const PointCloud&lt;PointXYZRGBA&gt;::ConstPtr&amp;)&gt; f =
                  boost::bind(&amp;grabberCallback, _1);
              openniGrabber-&gt;registerCallback(f);
          }
  
          viewer = createViewer();
  
          if (justVisualize)
          {
              if (noColor)
                  viewer-&gt;showCloud(fallbackCloud);
              else viewer-&gt;showCloud(cloudptr);
          }
          else openniGrabber-&gt;start();
  
          // Main loop.
          while (!viewer-&gt;wasStopped())
              boost::this_thread::sleep(boost::posix_time::seconds(1));
  
          if (!justVisualize)
              openniGrabber-&gt;stop();
      }
  </code></pre>
  
  <p>But with this code I need the viewer. How can I get the Point Cloud without the viewer? It should work without kinect SDK, only with PCL, openni.</p>