iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpenNIImageSource.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifdef NRT_HAVE_OPENNI
37 #ifndef INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_OPENNIIMAGESOURCE_H
38 #define INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_OPENNIIMAGESOURCE_H
39 
42 #include <nrt/Core/Typing/Enum.H>
44 
45 #include <nrt/OpenNI/OpenNI.H>
46 
47 namespace nrt
48 {
49  namespace openni
50  {
51  //! OpenNI device resolutions
52  /*! @class Resolution
53 
54  - if @c nrt::openni::Resolution::qvga, 320x240
55  - if @c nrt::openni::Resolution::vga, 640x480
56  - if @c nrt::openni::Resolution::sxga, 800x600
57  - if @c nrt::openni::Resolution::uxga, 1600x1200
58 
59  @relates OpenNIImageSource */
60  NRT_DEFINE_ENUM_CLASS(Resolution, (device_default) (qvga) (vga) (sxga) (uxga));
61 
62  static ParameterCategory ParamCateg("OpenNI Image Source Related Parameters");
63 
64  NRT_DECLARE_PARAMETER(xml, std::string, "XML Configuration file",
66 
67  NRT_DEFINE_ENUM_CLASS(DataType, (None) (RGB) (Depth) (RGBD) (IR) (IRD));
68 
69  NRT_DECLARE_PARAMETER(datatype, DataType, "Data to capture",
70  DataType::RGBD, DataType_Values, ParamCateg, nrt::ParameterFlags::NoOnlineChanges);
71 
72  NRT_DECLARE_PARAMETER(interpolate, bool, "Rescale (with interpolation) lower resolution images (e.g., depth map) "
73  "to the higher resolution images (e.g., RGB image)", true,
75 
76  NRT_DECLARE_PARAMETER(mirror, bool, "Mirror the input", false, ParamCateg, nrt::ParameterFlags::NoOnlineChanges);
77 
78  NRT_DECLARE_PARAMETER(DepthResolution, Resolution, "If in use, the resolution of the depth stream",
79  Resolution::device_default, Resolution_Values,
81 
82  NRT_DECLARE_PARAMETER(ImageResolution, Resolution, "If in use, the resolution of the image stream",
83  Resolution::device_default, Resolution_Values,
85 
86  NRT_DECLARE_PARAMETER(IRResolution, Resolution, "If in use, the resolution of the ir stream",
87  Resolution::device_default, Resolution_Values, ParamCateg,
89 
90  NRT_DECLARE_PARAMETER(DepthFPS, int,
91  "Used to request a non default fps for the depth stream, if enabled",
93 
94  NRT_DECLARE_PARAMETER(ImageFPS, int,
95  "Used to request a non default fps for the image stream, if enabled",
97 
98  NRT_DECLARE_PARAMETER(IRFPS, int,
99  "Used to request a non default fps for the ir stream, if enabled",
101  }
102 
103  //! An image source for capturing image data from an OpenNI compatible device
104  /*! OpenNI devices are asynchronous sources of data and as such a call to in() is not guaranteed to
105  always produce new data - new data is available when ok() returns true.
106 
107  In the case of requesting a mixed data format from the device, such as image + depth (a color depth map),
108  the resultant images will be the size of the largest data source between the desired sources with the
109  smaller source scaled to match the larger one.
110 
111  For example, if your device produces a 640x480 color image and has a 320x240 depth sensor, the depth sensor
112  image will be resized to 640x480. If this functionality is not desired, request the items individually.
113 
114  By default, any mixed data types will attempt to register the depth sensor to the image sensor. */
115  class OpenNIImageSource : public ImageSourceType,
116  public nrt::Parameter<openni::datatype, openni::DepthResolution, openni::ImageResolution,
117  openni::IRResolution, openni::interpolate, openni::DepthFPS,
118  openni::ImageFPS, openni::IRFPS, openni::mirror, openni::xml>
119  {
120  //! Used internally
121  enum class DataType;
122 
123  public:
124  //! A struct containing metadata from the device
125  struct OpenNIData
126  {
127  //! Default constructor
128  OpenNIData() : valid(false) { }
129 
130  //! Whether this struct contains valid data
131  bool valid;
132 
133  struct
134  {
135  float maximumDepth; //!< The maximum depth supported by the depth sensor in meters
136  XnUInt64 shadowValue; //!< Value for shadow (occluded) depth pixels
137  XnUInt64 noSampleValue; //!< Value for pixels without valid disparity measurement
138  float baseline; //!< The baseline of the depth sensor in meters
139  float focalLength; //!< The focal length of the depth sensor in pixels
140  } Depth;
141 
142  struct
143  {
144  XnPixelFormat pixelFormat; //!< The pixel format of the image sensor
145  float focalLength; //!< The focal length of the image sensor in pixels
146  } Image;
147 
148  //! Serialization
149  template <class Archive>
150  void serialize(Archive & ar)
151  {
152  ar(Depth.maximumDepth, Depth.shadowValue, Depth.noSampleValue, Depth.baseline, Depth.focalLength,
153  Image.pixelFormat, Image.focalLength);
154  }
155  };
156 
157  OpenNIImageSource(std::string instanceID = "OpenNI");
158 
160 
161  //! Checks whether there is a new image available from in()
162  const bool ok() const { return itsOk; };
163 
164  //! Returns the latest image from the OpenNI device that matches our parameters
165  GenericImage const in();
166 
167  //! Returns the latest combo RGB+depth+ir data from the OpenNI device that matches our parameters
168  void in_rgbd(GenericImage & rgb, GenericImage & d, GenericImage & ir);
169 
170  //! Get the name associated with our --in type
171  static std::string const id() { return "OpenNI"; }
172 
173  //! Get a description to show for --help
174  static std::string const description()
175  {
176  return "Returns the newest image from an OpenNI device conforming to given parameters on each call to in()";
177  }
178 
179  //! Returns existing device data (data will only be valid after device is initialized)
180  OpenNIData const & getDeviceData() const;
181 
182  protected:
183  //! Capture images and make then available to in() as soon as we are running
184  virtual void run();
185 
186  //! Gets an nrt Image from YUV422 data
187  Image<PixRGB<byte>> getYUV422Image(xn::ImageMetaData & imageMD);
188 
189  //! Gets an nrt Image from RGB24 data
190  Image<PixRGB<byte>> getRGB24Image(xn::ImageMetaData & imageMD);
191 
192  //! Gets an nrt Image from 8 bit grayscale data
193  Image<PixGray<byte>> getGray8Image(xn::ImageMetaData & imageMD);
194 
195  //! Gets an nrt Image from 16 bit grayscale data
196  Image<PixGray<uint16_t>> getGray16Image(xn::ImageMetaData & imageMD);
197 
198  //! Gets an nrt Image from 16 bit ir grayscale data
199  Image<PixGray<uint16_t>> getIRImage(xn::IRMetaData & irMD);
200 
201  //! Gets an nrt Image from 16 bit ir depth data
202  Image<PixGray<uint16_t> > getDepthImage(xn::DepthMetaData & depthMD);
203 
204  void setFps(xn::MapGenerator & gen, int const & fps);
205 
206  void setResolution(xn::MapGenerator & gen, nrt::openni::Resolution const & res);
207 
208  private:
209  //! Whether there is a new image
210  bool itsOk;
211 
212  //! Lock for changing parameter settings
213  mutable std::mutex itsMutex;
214 
215  //! The resultant image from the device
216  GenericImage itsImage;
217  GenericImage itsDepth;
218  GenericImage itsIR;
219 
220  //! Information we know about our device
221  OpenNIData itsDeviceData;
222 
223  xn::Context itsContext;
224  xn::ScriptNode itsScriptNode; //!< Used during XML files
225  xn::DepthGenerator itsDepthGen;
226  xn::ImageGenerator itsImageGen;
227  xn::IRGenerator itsIRGen;
228  };
229 }
230 
231 // inline function implementation
233 
234 #endif // INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_OPENNIIMAGESOURCE_H
235 #endif // NRT_HAVE_OPENNI