Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_Kinect.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #ifndef VIDEOSOURCE_KINECT_HH_
26 #define VIDEOSOURCE_KINECT_HH_
27 
28 // freenect c++ interface header
29 #ifndef WIN32
30  #include <libfreenect.hpp>
31 #else
32  #include <libfreenect.h>
33 #endif
34 #include <VideoSource/VideoSource_Kinect_Base.hh>
35 #include <Image/Camera.hh>
36 
37 namespace BIAS {
38 #ifndef WIN32
39  class VideoSource_Kinect_Callback;
40 #endif
41  /**
42  * @brief Use Kinect with the libfreenect drivers just like the ToF Cameras.
43  * There is no parameters to control the camera yet.
44  * Functionally might be added later depending on evolution of libfreenect
45  * @note for usage in Linux mind, that you need write access to the USB port.
46  * This can be granted by creating a udev rules file, e.g. 51-kinect.rules
47  * with the following content:
48  # ATTR{product}=="Xbox NUI Motor"
49  SUBSYSTEM=="usb", ATTR{idVendor}=="045e", ATTR{idProduct}=="02b0", MODE="0666"
50 
51  # ATTR{product}=="Xbox NUI Audio"
52  SUBSYSTEM=="usb", ATTR{idVendor}=="045e", ATTR{idProduct}=="02ad", MODE="0666"
53 
54  # ATTR{product}=="Xbox NUI Camera"
55  SUBSYSTEM=="usb", ATTR{idVendor}=="045e", ATTR{idProduct}=="02ae", MODE="0666"
56  * \ingroup g_videosource
57  * @author fkellner 02/11
58  */
59  class BIASVideoSource_EXPORT VideoSource_Kinect : public VideoSource_Kinect_Base {
60  public:
61  /** \brief Contrructor */
63  /** \brief Destructor */
64  virtual ~VideoSource_Kinect();
65 
66  /** \brief Opens first device found, OpenDevice(0) */
67  virtual int OpenDevice();
68  /** \brief Opens device by id. On first call inits the context and looks for available cameras (i.e. scan bus) */
69  virtual int OpenDevice(int device);
70 
71  /** \brief Close device, if no other kinect cameras are in use, context is deleted (i.e. close usb bus connection) */
72  virtual int CloseDevice();
73 
74  /** \brief set appropriate operation mode for kinect and start grabbing thread
75  (see callbacks in VideoSource_Kinect_Callback) */
76  virtual int PreGrab();
77  /** \brief stop grabbing thread */
78  virtual int PostGrab();
79 
80  /** \brief kinect color image will be 640x480, RGB. */
81  virtual int InitImage(BIAS::ImageBase &Image);
82  /** \brief kinect depth image will be 640x480, float in Millimeters */
83  virtual int InitDepthImage(BIAS::ImageBase &Image);
84 
85  /** \brief grab single color image */
86  virtual int GrabSingle(BIAS::Camera <unsigned char> &image);
87  /** \brief depth according to formula by Stephane Magnenat,
88  see http://openkinect.org/wiki/Imaging_Information, todo: needs validation! */
89  virtual int GrabSingleDepth(BIAS::Camera <float> &image);
90 
91  virtual int CreateDepthLookupTable(double px=328.82739, double py=252.6967098, double fx=585.9303337, double fy=585.5482334);
92 
93  /**\brief Set the video mode to color*/
94  virtual int SetVideoModeColor();
95  /**\brief Set the video mode to infrared*/
96  virtual int SetVideoModeIR();
97 
98  /**\brief set the depth capture mode
99  * \param mode[in]: the capture mode */
100  virtual int SetDepthCaptureMode(CaptureMode mode);
101  /**\brief Set whether to switch between infrared and color after each grab */
102  virtual int SetSwitchVideoModeAfterGrab();
103 
104  /**\brief Set the tilt angle of the Kinect Motor
105  * \param degree[in]: the degrees to tilt*/
106  virtual int SetTilt(double degrees);
107  /**\brief Set the Led mode, e.g. blinking
108  * \param mode[in]: the mode of the led, see freenect_led_options*/
109  virtual int SetLed(int mode);
110 
111 
112  /**\brief Get the values of tilt and accelerometer
113  * \param tilt[out]: the tilt angle
114  * \param x[out]: x acceleration
115  * \param y[out]: y acceleration
116  * \param z[out]: z acceleration*/
117  virtual int GetAccelerometer(double &tilt, double &x, double &y, double &z);
118 
119  virtual int GetCapabilities(BIAS::VideoSourceCapabilities &caps);
120  virtual int GetCapabilities(const char *device, BIAS::VideoSourceCapabilities &caps);
121 
122  /** \brief Get the number of devices */
123  virtual int GetNumDevices() {return deviceCount_; }
124 
125  virtual int ToggleDepthStream();
126 
127 #ifdef WIN32
128  void DeleteDepthLookupTable();
129 #endif
130 
131  private:
132 #ifndef WIN32
133  /// freenect "context", quite similar to dc_bus in libdc1394 this wraps up all devices on the bus
134  static Freenect::Freenect *kinectManager_;
135  /// this instance's actual device
136  VideoSource_Kinect_Callback *kinectDevice_;
137 #else
138 
139 #endif
140  /// total number of devices found on bus
141  static int deviceCount_;
142  /// number of open devices. when this reaches zero, context is closed
143  static int openDevices_;
144  /// this instance's device id. First cam found has id=0, second has id=1 etc
145  int deviceId_;
146  };
147 
148 #ifndef WIN32
149  /**
150  * Implementing FreenectDevice Interface, this class is used *internally* by
151  * VideoSource_Kinect - do not use directly
152  * @author fkellner 02/11
153  */
154  class VideoSource_Kinect_Callback : public Freenect::FreenectDevice
155  {
156  public:
157  /**
158  * libfreenect constructor used by VideoSource_Kinect, do not use directly
159  * @param _ctx libfreenect context, i.e. opened usb bus
160  * @param _index index of camera on context/bus
161  * @author fkellner 02/11
162  */
163  VideoSource_Kinect_Callback(freenect_context *_ctx, int _index);
164 
165  /**
166  * libfreenect thread calls this when new color data is available
167  * @param video pointer to color bytes (3 per pixel), todo: convince libfreenect to deliver raw rggb data
168  * @param timestamp frame timestamp, todo: interpret, put as metadata
169  * @author fkellner 02/11
170  */
171  virtual void VideoCallback(void *video, uint32_t timestamp);
172 
173  /**
174  * libfreenect thread calls this when new depth data is available
175  * @param video pointer to depth bytes (2 per pixel)
176  * @param timestamp frame timestamp, todo: interpret, put as metadata
177  * @author fkellner 02/11
178  */
179  virtual void DepthCallback(void *depth, uint32_t timestamp);
180 
181  /**
182  * Callbacks store image data in double buffer, get latest
183  * @param image color image
184  * @author fkellner 02/11
185  */
186  virtual int GetLatestColor(Camera<unsigned char> &image);
187 
188  /**
189  * Callbacks store image data in double buffer, get latest
190  * Note that the depth image is shifted by 3 pixels in order to fit the IR image.
191  * @param image depth image
192  * @author fkellner 02/11
193  */
194  virtual int GetLatestDepth(Camera<float> &image);
195 
196  virtual int SetVideoMode(freenect_video_format format);
197  virtual int SetSwitchVideoModeAfterGrab(bool switchMode);
198  virtual void SetDepthMode(int mode);
199 
200  void CreateLookupTable(double px, double py, double fx, double fy);
201 
202  protected:
203  // double buffered cam data
204  std::vector< Camera<unsigned char> > colorbuffer_;
205  std::vector< Camera<float> > depthbuffer_;
206  // indices into buffers
209 
210  // todo: clean up this mess, we do not need so many mutexes (fkellner)
211  pthread_mutex_t *color_mutex;
212  pthread_mutex_t *depth_mutex;
213  pthread_cond_t *newColorAvail_;
214  pthread_cond_t *newDepthAvail_;
215  pthread_mutex_t *coloravail_mutex;
216  pthread_mutex_t *depthavail_mutex;
217 
218  // lookup table for depth bytes -> mm conversion,
219  // formula by Stephane Magnenat, see http://openkinect.org/wiki/Imaging_Information
220  float lookup_[2048];
221 
222  // lookup table for z-depth to ray conversion (calibrated using one specific kinect camera)
224 
225  // current color format
226  freenect_video_format format_;
227 
228  bool switch_;
229  int capMode_;
230  };
231 #endif
232 }
233 
234 #endif /* VIDEOSOURCE_KINECT_HH_ */
virtual int SetVideoMode(freenect_video_format format)
std::vector< Camera< float > > depthbuffer_
virtual int GetNumDevices()
Get the number of devices.
Implementing FreenectDevice Interface, this class is used internally by VideoSource_Kinect - do not u...
virtual void VideoCallback(void *video, uint32_t timestamp)
libfreenect thread calls this when new color data is available
virtual void DepthCallback(void *depth, uint32_t timestamp)
libfreenect thread calls this when new depth data is available
std::vector< Camera< unsigned char > > colorbuffer_
virtual int GetLatestDepth(Camera< float > &image)
Callbacks store image data in double buffer, get latest Note that the depth image is shifted by 3 pix...
virtual int GetLatestColor(Camera< unsigned char > &image)
Callbacks store image data in double buffer, get latest.
The image template class for specific storage types.
Definition: Image.hh:78
void CreateLookupTable(double px, double py, double fx, double fy)
VideoSource_Kinect_Callback(freenect_context *_ctx, int _index)
libfreenect constructor used by VideoSource_Kinect, do not use directly
Checks for VideoSource capabilities.
Use Kinect with the libfreenect drivers just like the ToF Cameras.
virtual int SetSwitchVideoModeAfterGrab(bool switchMode)
Use Kinect just like the ToF Cameras.
This is the base class for images in BIAS.
Definition: ImageBase.hh:102