Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_OpenNI.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_OPENNI_HH_
26 #define VIDEOSOURCE_OPENNI_HH_
27 #ifndef _WIN32
28  #if __cplusplus > 199711L
29  #ifndef linux
30  #define linux 1
31  #endif
32  #ifndef __linux
33  #define __linux
34  #endif
35  #endif
36 #endif
37 
38 #include <XnCppWrapper.h>
39 #include <VideoSource/VideoSource_Kinect_Base.hh>
40 #include <Image/Camera.hh>
41 #include <pthread.h>
42 
43 namespace BIAS {
44 
45  /**
46  * Use OpenNI just like the ToF Cameras. There is no parameters to control the camera yet.
47  * Functionally might be added later depending on evolution of OpenNI
48  * @author fkellner 02/11
49  */
50  class BIASVideoSource_EXPORT VideoSource_OpenNI : public VideoSource_Kinect_Base {
51  public:
52 
54  virtual ~VideoSource_OpenNI();
55 
56  /// Opens first device found, OpenDevice(0)
57  virtual int OpenDevice();
58 
59  /// Opens device by id. On first call inits the context and looks for available cameras (i.e. scan bus)
60  virtual int OpenDevice(int device);
61 
62  /// Close device, if no other kinect cameras are in use, context is deleted (i.e. close usb bus connection)
63  virtual int CloseDevice();
64 
65  /// set appropriate operation mode for kinect and start grabbing thread (see callbacks in VideoSource_OpenNI_Callback)
66  virtual int PreGrab();
67  /// stop grabbing thread
68  virtual int PostGrab();
69 
70  /// kinect color image will be 640x480, RGB.
71  virtual int InitImage(BIAS::ImageBase &Image);
72  /// kinect depth image will be 640x480, float in Millimeters
73  virtual int InitDepthImage(BIAS::ImageBase &Image);
74 
75  /// grab single color image
76  virtual int GrabSingle(BIAS::Camera <unsigned char> &image);
77  /// depth according to formula by Stephane Magnenat, see http://openkinect.org/wiki/Imaging_Information, todo: needs validation!
78  /// Note that the depth image is shifted by 3 pixels in order to fit the IR image.
79  virtual int GrabSingleDepth(BIAS::Camera <float> &image);
80 
81  virtual int SetVideoModeColor();
82  virtual int SetVideoModeIR();
83 
84  virtual int SetDepthCaptureMode(CaptureMode mode);
85 
86  virtual int SetSwitchVideoModeAfterGrab();
87 
88  virtual int SetTilt(double degrees);
89  virtual int SetLed(int mode);
90  virtual int GetAccelerometer(double &tilt, double &x, double &y, double &z);
91 
92  virtual int GetNumDevices() {
93  return numDevices_;
94  }
95 
96  /// get if generating color (=true)
97  virtual bool GetColorGeneratorMode() {
98  return colorMode_;
99  }
100 
101  virtual inline xn::Context* GetOpenNIContext() { return &context_; }
102 
103  virtual int GetCapabilities(BIAS::VideoSourceCapabilities &caps);
104  virtual int GetCapabilities(const char *device, BIAS::VideoSourceCapabilities &caps);
105 
106  virtual int CreateDepthLookupTable(double px, double py, double fx, double fy);
107 
108  virtual int StartDepthStream();
109 
110  virtual int StopDepthStream();
111 
112  virtual int ToggleDepthStream();
113 
114  private:
115 
116  struct ONIDeviceNode {
117  ONIDeviceNode(const xn::NodeInfo& device, const xn::NodeInfo& image, const xn::NodeInfo& ir, const xn::NodeInfo& depth)
118  : deviceInfo(device), imageInfo(image), irInfo(ir), depthInfo(depth)
119  {
120  }
121  xn::NodeInfo deviceInfo;
122  xn::NodeInfo imageInfo;
123  xn::NodeInfo irInfo;
124  xn::NodeInfo depthInfo;
125  };
126 
127  xn::DepthGenerator depthGenerator_;
128  xn::UserGenerator userGenerator_;
129  xn::ImageGenerator colorGenerator_;
130  xn::IRGenerator irGenerator_;
131 
132  xn::DepthMetaData depthMD_;
133  xn::ImageMetaData colorMD_;
134  xn::IRMetaData irMD_;
135 
136  XnStatus rc_;
137  xn::EnumerationErrors errors_;
138 
139  CaptureMode capMode_;
140  bool colorMode_;
141 
142  Image<float> *depthLookupTable_;
143  static int numInstances_;
144  static int numDevices_;
145 
146 
147  static xn::Context context_;
148  static std::vector<ONIDeviceNode> devices_;
149 
150  static xn::NodeInfoList device_nodes_;
151  static xn::NodeInfoList depth_nodes_;
152  static xn::NodeInfoList image_nodes_;
153  static xn::NodeInfoList ir_nodes_;
154 
155  void CreateLookupTable_(double px = 328.82739, double py = 252.6967098, double fx = 585.9303337, double fy = 585.548233470985);
156  void DeleteLookupTable_();
157 
158  static bool CheckError_(XnStatus rc, std::string what);
159 
160  static void ScanBus();
161 
162  bool contextInited_;
163 
164  int deviceId_;
165 
166  bool switchModeEveryFrame_;
167 
168  static pthread_mutex_t *exclusive_mutex;
169  };
170 
171 }
172 
173 #endif /* VIDEOSOURCE_OPENNI_HH_ */
int BIASVideoSource_EXPORT ScanBus(std::stringstream &ofs)
support function to get the number of cameras and IDs of all cameras on one bus
Use OpenNI just like the ToF Cameras.
BIAS::Image< float > * depthLookupTable_
virtual xn::Context * GetOpenNIContext()
The image template class for specific storage types.
Definition: Image.hh:78
Checks for VideoSource capabilities.
virtual bool GetColorGeneratorMode()
get if generating color (=true)
Use Kinect just like the ToF Cameras.
This is the base class for images in BIAS.
Definition: ImageBase.hh:102