Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_Kinect2.hh
1 #ifndef VIDEOSOURCE_KINECT2_HH
2 #define VIDEOSOURCE_KINECT2_HH
3 
4 #include <VideoSource/VideoSource_Base.hh>
5 #include <Image/Camera.hh>
6 #include <libfreenect2/libfreenect2.hpp>
7 #include <libfreenect2/frame_listener.hpp>
8 #include <semaphore.h>
9 
10 namespace BIAS
11 {
12  /** @class VideoSource_Kinect2
13  @brief Use the Kinect2 class to grab images from the Microsoft Kinect2.
14  The VideoSource_Kinect2 allows you to grab the HD color camera image
15  with GrabSingle(), the depth image with GrabSingleDepth(), and the IR
16  image with GrabSingleIR().
17  Before calling OpenDevice(), set a desired mode with SetMode() to
18  control which images you want to grab. You have to grab all images in
19  the mode you have selected initially.
20  @author jordt 10/14, fkellner 03/15
21  */
22  class BIASVideoSource_EXPORT VideoSource_Kinect2
23  : public VideoSource, public libfreenect2::FrameListener
24  {
25  public:
26 
27  enum Kinect2Mode {
28  all = 0, ///< get color, ir and depth images (default)
29  depthAndIr, ///< get depth and ir only
30  color ///< get color only
31  };
32 
34 
35  virtual ~VideoSource_Kinect2();
36 
37  void SetMode(Kinect2Mode mode);
38 
39  /// Opens first device found, OpenDevice(0)
40  /// @return Returns -1 if no device is available and -2 if an error happened while opening
41  virtual int OpenDevice();
42 
43  /// Opens device by id. Use GetNumDevices() to determine the number of available Kinect2s
44  /// @return Returns -1 if no device is available and -2 if an error happened while opening
45  virtual int OpenDevice(int device);
46 
47  /// Close device, if no other kinect cameras are in use, context is deleted (i.e. close usb bus connection)
48  virtual int CloseDevice();
49 
50  /// Start grabbing
51  virtual int PreGrab();
52 
53  /// Stop grabbing
54  virtual int PostGrab();
55 
56  /// kinect 2 color image will be 1920x1080, RGB.
57  virtual int InitImage(BIAS::ImageBase &Image);
58 
59  /// kinect 2 depth image will be 512x424, float in Millimeters
60  virtual int InitDepthImage(BIAS::ImageBase &Image);
61 
62  /// kinect 2 ir image will be 512x424, single channel unsigned char
63  virtual int InitIrImage(BIAS::ImageBase &Image);
64 
65  /// all kinect 2 ir phase images in 512x242, nine channel float
66  virtual int InitIrFullImage(ImageBase &Image);
67 
68  /// grab single color image. Note: This image has already been jpeg compressed
69  virtual int GrabSingle(BIAS::Camera <unsigned char> &image);
70 
71  /// grab the raw ir data dump (very fast).
72  //virtual int GrabSingleRawIrData(Kinect2RawIrData &data);
73 
74  /// grab the raw ir data dump (very fast). It contains a 11 bit information for each of the
75  /// 512*424 pixels + 1 bit magic, so the complete data dump has the size of
76  /// 512*424*11/8 = 298496 bytes. This function expects this memory already allocated.
77  //virtual int GrabSingleRawIrData(unsigned char* data);
78 
79  /// grab the ir sum image computed from the phase images.
80  virtual int GrabSingleIR(BIAS::Camera <float> &image);
81 
82  /// grab all raw ir images (fast) as a 9 channel float image. The image contains 3 phase
83  /// images for each of the 3 frequencies.
84  virtual int GrabSingleIRFull(BIAS::Camera <float> &image);
85 
86  /// grab the depth image (slow). Use this function only if no high framerates are required, since
87  /// the conversion from ir to depth image is done on the cpu and requires a lot computation.
88  virtual int GrabSingleDepth(BIAS::Camera <float> &image);
89 
90  /// Return the number of available Kinect 2 devices
91  virtual int GetNumDevices();
92 
93  /// No standard capabilities here
94  virtual int GetCapabilities(VideoSourceCapabilities &caps);
95 
96  /// No standard capabilities here
97  virtual int GetCapabilities(const char *device, VideoSourceCapabilities &caps);
98 
99  virtual bool onNewFrame(libfreenect2::Frame::Type type, libfreenect2::Frame *frame);
100 
101  private:
102 
103  static libfreenect2::Freenect2 *freenect2_;
104  libfreenect2::Freenect2Device *dev_;
105  libfreenect2::PacketPipeline *pipeline_;
106 
107  static const int buffersize_ = 4;
108 
109  class imbuffer
110  {
111  public:
112  imbuffer() : curProd(0), curCons(0) {
113  sem_init(&sProd,true,buffersize_);
114  sem_init(&sCons,true,0);
115  for (int i=0;i<buffersize_;i++) buffer[i].Init(512,424,1);
116  }
117  Image<float> buffer[buffersize_];
118  sem_t sProd;
119  sem_t sCons;
120  int curProd, curCons;
121  };
122 
123  // unused, but for later
124  class colorbuffer
125  {
126  public:
127  colorbuffer() : curProd(0), curCons(0) {
128  sem_init(&sProd,true,buffersize_);
129  sem_init(&sCons,true,0);
130  for (int i=0;i<buffersize_;i++) buffer[i].Init(1920,1080,3);
131  }
132  Image<unsigned char> buffer[buffersize_];
133  sem_t sProd;
134  sem_t sCons;
135  int curProd, curCons;
136  };
137 
138  imbuffer depthbuffer_;
139  imbuffer irbuffer_;
140  colorbuffer colorbuffer_;
141 
142  Kinect2Mode grabMode_;
143 
144  int numDevices_;
145  };
146 
147 }
148 
149 #endif // VIDEOSOURCE_KINECT2_HH
Defines a common interface to different devices.
Use the Kinect2 class to grab images from the Microsoft Kinect2.
The image template class for specific storage types.
Definition: Image.hh:78
Checks for VideoSource capabilities.
This is the base class for images in BIAS.
Definition: ImageBase.hh:102