OpenNIImageProvider.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::Component
19  * @author David Gonzalez (david dot gonzalez at kit dot edu)
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 // VisionXCore
29 
30 // STL
31 #include <float.h>
32 
33 #include <map>
34 #include <string>
35 
36 // OpenNI
37 #include <OpenNI.h>
38 
39 namespace visionx
40 {
42  {
43 
44  public:
46  {
47  defineOptionalProperty<ImageDimension>(
48  "VideoMode", ImageDimension(320, 240), "Image resolution")
49  .setCaseInsensitive(true)
50  .map("320x240", ImageDimension(320, 240))
51  .map("640x480", ImageDimension(640, 480));
52 
53  defineOptionalProperty<ImageType>(
54  "ImageType", eColoredPointsScan, "Image type: RGB, Points, Points with RGB")
55  .setCaseInsensitive(true)
56  //.map("ePointsScan", ePointsScan)
57  .map("ColoredPointsScan", eColoredPointsScan);
58 
59  defineOptionalProperty<float>("FrameRate", 30.0f, "Frames per second")
60  .setMatchRegex("\\d+(.\\d*)?")
61  .setMin(15.0f)
62  .setMax(30.0f);
63 
64  // Camera URI
65  /*verify this lexical-rule*/
66  defineOptionalProperty<std::string>("DeviceURI", "ANY", "The URI from OpenNI API")
67  .setCaseInsensitive(true);
68  }
69  };
70 
71  /**
72  * OpenNI image provider captures 3D-points and/or color(s) images from a single device and
73  * supports the following image transmission formats:
74  *
75  * - RGB: 3, bytes per Pixel
76  * - PointsScan (X,Y,Z): 3, floats per Pixel
77  * - ColoredPointsScan, Geometrically registered (X,Y,Z,R,G,B): 3 floats + 3 bytes per Pixel
78  *
79  * \componentproperties
80  * \prop VisionX.OpenNIImageProvider.CameraURI: The particular camera URI tha wanst to be addressed.
81  If not set the first device on the USBus will be opened .
82  * \prop VisionX.OpenNIImageProvider.VideoMode: Defines the camera
83  * resolution.
84  * - 320x240 (default)
85  * - 640x480
86  * \prop VisionX.OpenNIImageProvider.ImageType: Specifies how image type is composed.
87  * Possible values:
88  * - rgb
89  * - PointsScan
90  * - ColoredPointsScan
91  * \prop VisionX.OpenNIImageProvider.FrameRate: Capture frame rate as
92  * float (default: 30fps)
93  */
94 
96  {
97 
98  public:
99  /*NEEDS TO BE UPDATED WITH THE RGB->RGBA Changes
100  static CByteImage* Display(const visionx::ColoredPoint3D* pColoredPoint3DBufferBase,const int Width, const int Height,const bool DisplayColor)
101  {
102  if( (!pColoredPoint3DBufferBase) || (Width<1) || (Height<1) )
103  {
104  return NULL;
105  }
106 
107  CByteImage* pDisplayImage = new CByteImage(Width,Height,CByteImage::eRGB24);
108 
109  visionx::RGB* pRGB = (visionx::RGB*)(pDisplayImage->pixels);
110 
111  const visionx::ColoredPoint3D* pColoredPoint3DBuffer = pColoredPoint3DBufferBase;
112 
113  const visionx::ColoredPoint3D* const pEndColoredPoint3DBuffer = pColoredPoint3DBufferBase + (Width * Height);
114 
115  if(DisplayColor)
116  {
117  while(pColoredPoint3DBuffer < pEndColoredPoint3DBuffer)
118  {
119  *pRGB++ = pColoredPoint3DBuffer->m_Color;
120 
121  ++pColoredPoint3DBuffer;
122  }
123  }
124  else
125  {
126  float MinimalDepth = FLT_MAX;
127 
128  float MaximalDepth = -FLT_MAX;
129 
130  while(pColoredPoint3DBuffer<pEndColoredPoint3DBuffer)
131  {
132  if(pColoredPoint3DBuffer->m_Point.m_Z)
133  {
134 
135  if(pColoredPoint3DBuffer->m_Point.m_Z > MaximalDepth)
136  MaximalDepth = pColoredPoint3DBuffer->m_Point.m_Z;
137 
138 
139  if(pColoredPoint3DBuffer->m_Point.m_Z < MinimalDepth)
140  MinimalDepth = pColoredPoint3DBuffer->m_Point.m_Z;
141 
142  }
143 
144  ++pColoredPoint3DBuffer;
145  }
146 
147  const float DepthRange = MaximalDepth - MinimalDepth;
148 
149  if(DepthRange < FLT_EPSILON)
150  {
151  memset(pRGB,128,sizeof(visionx::RGB) * Width * Height);
152 
153  return pDisplayImage;
154  }
155 
156  const float ScalingFactor = 255.0f / DepthRange;
157 
158  pColoredPoint3DBuffer = pColoredPoint3DBufferBase;
159 
160  while(pColoredPoint3DBuffer<pEndColoredPoint3DBuffer)
161  {
162  if(pColoredPoint3DBuffer->m_Point.m_Z)
163  {
164 
165  pRGB->m_R = pRGB->m_G = pRGB->m_B = 255 - int(((pColoredPoint3DBuffer->m_Point.m_Z - MinimalDepth) * ScalingFactor) + 0.5f);
166 
167  }
168  else
169  {
170  pRGB->m_R = 255;
171 
172  pRGB->m_G = pRGB->m_B = 0;
173  }
174 
175  ++pRGB;
176 
177  ++pColoredPoint3DBuffer;
178  }
179  }
180 
181  return pDisplayImage;
182  }*/
183 
184  /**
185  * @see visionx::ImageProviderBase::onInitImageProvider()
186  */
187  virtual void onInitCapturingImageProvider();
188 
189  /**
190  * @see visionx::ImageProviderBase::onExitImageProvider()
191  */
192  virtual void onExitCapturingImageProvider();
193 
194  /**
195  * @see visionx::ImageProviderBase::onStartCapture()
196  */
197  virtual void onStartCapture(float frameRate);
198 
199  /**
200  * @see visionx::ImageProviderBase::onStopCapture()
201  */
202  virtual void onStopCapture();
203 
204  /**
205  * @see visionx::ImageProviderBase::capture()
206  */
207  virtual bool capture(void** ppImageBuffers);
208 
209  /*
210  void provideImages(CByteImage** images);
211 
212 
213  void provideImages(CFloatImage** images);
214  */
215 
216  /**
217  * @see armarx::Component::getDefaultName()
218  */
219  virtual std::string
221  {
222  return "OpenNIImageProvider";
223  }
224 
225  /**
226  * @see PropertyUser::createPropertyDefinitions()
227  */
230  {
233  }
234 
235 
236  protected:
237  /**
238  * Camera URI
239  */
240  std::string uri;
241 
242  /**
243  * Video dimension data
244  */
245  visionx::ImageDimension videoDimension;
246 
247  /**
248  * Capturing frame rate
249  */
250  float frameRate;
251 
252  /**
253  * Capturing image type
254  */
255  visionx::ImageType imageType;
256 
257  /**
258  * Captured images
259  */
260  visionx::ColoredPoint3D* coloredPoint3DBuffer;
261 
262  /**
263  * OpenNI Capture
264  */
265 
267  {
268  float nx;
269  float ny;
270  };
271 
273 
274  openni::Device deviceOpenNI;
275 
276  openni::VideoStream depthStreamOpenNI;
277 
278  openni::VideoStream colorStreamOpenNI;
279 
280  openni::VideoFrameRef frameOpenNI;
281 
282  static bool isBackEndInitialized;
283 
285 
286  bool StartDevice(const char* pDeviceURI, const int Width, const int Height, const int Fps);
287 
288  bool CreateDataStructures();
289 
291 
292  bool StopDevice();
293 
294  bool DestroyDataStructure();
295 
296  bool CaptureColoredPoint();
297 
298  bool DispatchDepthFrame();
299 
300  bool DispatchColorFrame();
301 
302  int EnsureFPS(const int FPS);
303  };
304 } // namespace visionx
visionx::OpenNIImageProvider::frameOpenNI
openni::VideoFrameRef frameOpenNI
Definition: OpenNIImageProvider.h:280
visionx::OpenNIImageProvider::onStartCapture
virtual void onStartCapture(float frameRate)
Definition: OpenNIImageProvider.cpp:118
visionx::OpenNIImageProvider::onInitCapturingImageProvider
virtual void onInitCapturingImageProvider()
Definition: OpenNIImageProvider.cpp:54
visionx::OpenNIImageProvider::isModuleInitialized
bool isModuleInitialized
Definition: OpenNIImageProvider.h:284
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::OpenNIImageProvider::frameRate
float frameRate
Capturing frame rate.
Definition: OpenNIImageProvider.h:250
visionx::OpenNIImageProvider::onStopCapture
virtual void onStopCapture()
Definition: OpenNIImageProvider.cpp:261
visionx::OpenNIPropertyDefinitions::OpenNIPropertyDefinitions
OpenNIPropertyDefinitions(std::string prefix)
Definition: OpenNIImageProvider.h:45
visionx::OpenNIImageProvider::colorStreamOpenNI
openni::VideoStream colorStreamOpenNI
Definition: OpenNIImageProvider.h:278
visionx::OpenNIImageProvider::InitializeNormalizedDepthCells
bool InitializeNormalizedDepthCells()
Definition: OpenNIImageProvider.cpp:590
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
visionx::OpenNIPropertyDefinitions
Definition: OpenNIImageProvider.h:41
visionx::OpenNIImageProvider::NormalizedDepthCell::nx
float nx
Definition: OpenNIImageProvider.h:268
visionx::OpenNIImageProvider::DispatchDepthFrame
bool DispatchDepthFrame()
Definition: OpenNIImageProvider.cpp:782
visionx::OpenNIImageProvider::CreateDataStructures
bool CreateDataStructures()
Definition: OpenNIImageProvider.cpp:566
visionx::OpenNIImageProvider::CaptureColoredPoint
bool CaptureColoredPoint()
Definition: OpenNIImageProvider.cpp:665
visionx::OpenNIImageProvider
OpenNI image provider captures 3D-points and/or color(s) images from a single device and supports the...
Definition: OpenNIImageProvider.h:95
visionx::OpenNIImageProvider::EnsureFPS
int EnsureFPS(const int FPS)
Definition: OpenNIImageProvider.cpp:845
visionx::OpenNIImageProvider::isBackEndInitialized
static bool isBackEndInitialized
Definition: OpenNIImageProvider.h:282
visionx::OpenNIImageProvider::NormalizedDepthCell
OpenNI Capture.
Definition: OpenNIImageProvider.h:266
visionx::OpenNIImageProvider::videoDimension
visionx::ImageDimension videoDimension
Video dimension data.
Definition: OpenNIImageProvider.h:245
visionx::OpenNIImageProvider::DestroyDataStructure
bool DestroyDataStructure()
Definition: OpenNIImageProvider.cpp:645
visionx::OpenNIImageProvider::getDefaultName
virtual std::string getDefaultName() const
Definition: OpenNIImageProvider.h:220
visionx::OpenNIImageProvider::coloredPoint3DBuffer
visionx::ColoredPoint3D * coloredPoint3DBuffer
Captured images.
Definition: OpenNIImageProvider.h:260
visionx::CapturingImageProvider::capture
virtual void capture()
Definition: CapturingImageProvider.cpp:109
visionx::CapturingImageProvider
The CapturingImageProvider provides a callback function to trigger the capturing of images with diffe...
Definition: CapturingImageProvider.h:52
visionx::OpenNIImageProvider::StartDevice
bool StartDevice(const char *pDeviceURI, const int Width, const int Height, const int Fps)
Definition: OpenNIImageProvider.cpp:356
visionx::OpenNIImageProvider::StopDevice
bool StopDevice()
Definition: OpenNIImageProvider.cpp:623
visionx::OpenNIImageProvider::onExitCapturingImageProvider
virtual void onExitCapturingImageProvider()
Definition: OpenNIImageProvider.cpp:108
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
CapturingImageProvider.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::OpenNIImageProvider::NormalizedDepthCell::ny
float ny
Definition: OpenNIImageProvider.h:269
visionx::OpenNIImageProvider::depthStreamOpenNI
openni::VideoStream depthStreamOpenNI
Definition: OpenNIImageProvider.h:276
armarx::ComponentPropertyDefinitions::ComponentPropertyDefinitions
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition: Component.cpp:35
visionx::OpenNIImageProvider::DispatchColorFrame
bool DispatchColorFrame()
Definition: OpenNIImageProvider.cpp:819
visionx::OpenNIImageProvider::imageType
visionx::ImageType imageType
Capturing image type.
Definition: OpenNIImageProvider.h:255
visionx::OpenNIImageProvider::createPropertyDefinitions
virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions()
Definition: OpenNIImageProvider.h:229
visionx::OpenNIImageProvider::deviceOpenNI
openni::Device deviceOpenNI
Definition: OpenNIImageProvider.h:274
visionx::OpenNIImageProvider::normalizedDepthCells
NormalizedDepthCell * normalizedDepthCells
Definition: OpenNIImageProvider.h:272
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
visionx::OpenNIImageProvider::uri
std::string uri
Camera URI.
Definition: OpenNIImageProvider.h:240