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 <string>
32 #include <map>
33 #include <float.h>
34 
35 // OpenNI
36 #include <OpenNI.h>
37 
38 namespace visionx
39 {
42  {
43 
44  public:
45 
48  {
49  defineOptionalProperty<ImageDimension>("VideoMode", ImageDimension(320, 240), "Image resolution")
50  .setCaseInsensitive(true)
51  .map("320x240", ImageDimension(320, 240))
52  .map("640x480", ImageDimension(640, 480));
53 
54  defineOptionalProperty<ImageType>("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").setCaseInsensitive(true);
67 
68  }
69  };
70 
71 
72  /**
73  * OpenNI image provider captures 3D-points and/or color(s) images from a single device and
74  * supports the following image transmission formats:
75  *
76  * - RGB: 3, bytes per Pixel
77  * - PointsScan (X,Y,Z): 3, floats per Pixel
78  * - ColoredPointsScan, Geometrically registered (X,Y,Z,R,G,B): 3 floats + 3 bytes per Pixel
79  *
80  * \componentproperties
81  * \prop VisionX.OpenNIImageProvider.CameraURI: The particular camera URI tha wanst to be addressed.
82  If not set the first device on the USBus will be opened .
83  * \prop VisionX.OpenNIImageProvider.VideoMode: Defines the camera
84  * resolution.
85  * - 320x240 (default)
86  * - 640x480
87  * \prop VisionX.OpenNIImageProvider.ImageType: Specifies how image type is composed.
88  * Possible values:
89  * - rgb
90  * - PointsScan
91  * - ColoredPointsScan
92  * \prop VisionX.OpenNIImageProvider.FrameRate: Capture frame rate as
93  * float (default: 30fps)
94  */
95 
97  virtual public visionx::CapturingImageProvider
98  {
99 
100  public:
101 
102  /*NEEDS TO BE UPDATED WITH THE RGB->RGBA Changes
103  static CByteImage* Display(const visionx::ColoredPoint3D* pColoredPoint3DBufferBase,const int Width, const int Height,const bool DisplayColor)
104  {
105  if( (!pColoredPoint3DBufferBase) || (Width<1) || (Height<1) )
106  {
107  return NULL;
108  }
109 
110  CByteImage* pDisplayImage = new CByteImage(Width,Height,CByteImage::eRGB24);
111 
112  visionx::RGB* pRGB = (visionx::RGB*)(pDisplayImage->pixels);
113 
114  const visionx::ColoredPoint3D* pColoredPoint3DBuffer = pColoredPoint3DBufferBase;
115 
116  const visionx::ColoredPoint3D* const pEndColoredPoint3DBuffer = pColoredPoint3DBufferBase + (Width * Height);
117 
118  if(DisplayColor)
119  {
120  while(pColoredPoint3DBuffer < pEndColoredPoint3DBuffer)
121  {
122  *pRGB++ = pColoredPoint3DBuffer->m_Color;
123 
124  ++pColoredPoint3DBuffer;
125  }
126  }
127  else
128  {
129  float MinimalDepth = FLT_MAX;
130 
131  float MaximalDepth = -FLT_MAX;
132 
133  while(pColoredPoint3DBuffer<pEndColoredPoint3DBuffer)
134  {
135  if(pColoredPoint3DBuffer->m_Point.m_Z)
136  {
137 
138  if(pColoredPoint3DBuffer->m_Point.m_Z > MaximalDepth)
139  MaximalDepth = pColoredPoint3DBuffer->m_Point.m_Z;
140 
141 
142  if(pColoredPoint3DBuffer->m_Point.m_Z < MinimalDepth)
143  MinimalDepth = pColoredPoint3DBuffer->m_Point.m_Z;
144 
145  }
146 
147  ++pColoredPoint3DBuffer;
148  }
149 
150  const float DepthRange = MaximalDepth - MinimalDepth;
151 
152  if(DepthRange < FLT_EPSILON)
153  {
154  memset(pRGB,128,sizeof(visionx::RGB) * Width * Height);
155 
156  return pDisplayImage;
157  }
158 
159  const float ScalingFactor = 255.0f / DepthRange;
160 
161  pColoredPoint3DBuffer = pColoredPoint3DBufferBase;
162 
163  while(pColoredPoint3DBuffer<pEndColoredPoint3DBuffer)
164  {
165  if(pColoredPoint3DBuffer->m_Point.m_Z)
166  {
167 
168  pRGB->m_R = pRGB->m_G = pRGB->m_B = 255 - int(((pColoredPoint3DBuffer->m_Point.m_Z - MinimalDepth) * ScalingFactor) + 0.5f);
169 
170  }
171  else
172  {
173  pRGB->m_R = 255;
174 
175  pRGB->m_G = pRGB->m_B = 0;
176  }
177 
178  ++pRGB;
179 
180  ++pColoredPoint3DBuffer;
181  }
182  }
183 
184  return pDisplayImage;
185  }*/
186 
187  /**
188  * @see visionx::ImageProviderBase::onInitImageProvider()
189  */
190  virtual void onInitCapturingImageProvider();
191 
192  /**
193  * @see visionx::ImageProviderBase::onExitImageProvider()
194  */
195  virtual void onExitCapturingImageProvider();
196 
197  /**
198  * @see visionx::ImageProviderBase::onStartCapture()
199  */
200  virtual void onStartCapture(float frameRate);
201 
202  /**
203  * @see visionx::ImageProviderBase::onStopCapture()
204  */
205  virtual void onStopCapture();
206 
207  /**
208  * @see visionx::ImageProviderBase::capture()
209  */
210  virtual bool capture(void** ppImageBuffers);
211 
212 
213  /*
214  void provideImages(CByteImage** images);
215 
216 
217  void provideImages(CFloatImage** images);
218  */
219 
220  /**
221  * @see armarx::Component::getDefaultName()
222  */
223  virtual std::string getDefaultName() const
224  {
225  return "OpenNIImageProvider";
226  }
227 
228  /**
229  * @see PropertyUser::createPropertyDefinitions()
230  */
232  {
234  }
235 
236 
237  protected:
238 
239  /**
240  * Camera URI
241  */
242  std::string uri;
243 
244  /**
245  * Video dimension data
246  */
247  visionx::ImageDimension videoDimension;
248 
249  /**
250  * Capturing frame rate
251  */
252  float frameRate;
253 
254  /**
255  * Capturing image type
256  */
257  visionx::ImageType imageType;
258 
259  /**
260  * Captured images
261  */
262  visionx::ColoredPoint3D* coloredPoint3DBuffer;
263 
264  /**
265  * OpenNI Capture
266  */
267 
269  {
270  float nx;
271  float ny;
272  };
273 
275 
276  openni::Device deviceOpenNI;
277 
278  openni::VideoStream depthStreamOpenNI;
279 
280  openni::VideoStream colorStreamOpenNI;
281 
282  openni::VideoFrameRef frameOpenNI;
283 
284  static bool isBackEndInitialized;
285 
287 
288  bool StartDevice(const char* pDeviceURI, const int Width, const int Height, const int Fps);
289 
290  bool CreateDataStructures();
291 
293 
294  bool StopDevice();
295 
296  bool DestroyDataStructure();
297 
298  bool CaptureColoredPoint();
299 
300  bool DispatchDepthFrame();
301 
302  bool DispatchColorFrame();
303 
304  int EnsureFPS(const int FPS);
305  };
306 }
307 
308 
visionx::OpenNIImageProvider::frameOpenNI
openni::VideoFrameRef frameOpenNI
Definition: OpenNIImageProvider.h:282
visionx::OpenNIImageProvider::onStartCapture
virtual void onStartCapture(float frameRate)
Definition: OpenNIImageProvider.cpp:114
visionx::OpenNIImageProvider::onInitCapturingImageProvider
virtual void onInitCapturingImageProvider()
Definition: OpenNIImageProvider.cpp:53
visionx::OpenNIImageProvider::isModuleInitialized
bool isModuleInitialized
Definition: OpenNIImageProvider.h:286
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::OpenNIImageProvider::frameRate
float frameRate
Capturing frame rate.
Definition: OpenNIImageProvider.h:252
visionx::OpenNIImageProvider::onStopCapture
virtual void onStopCapture()
Definition: OpenNIImageProvider.cpp:249
visionx::OpenNIPropertyDefinitions::OpenNIPropertyDefinitions
OpenNIPropertyDefinitions(std::string prefix)
Definition: OpenNIImageProvider.h:46
visionx::OpenNIImageProvider::colorStreamOpenNI
openni::VideoStream colorStreamOpenNI
Definition: OpenNIImageProvider.h:280
visionx::OpenNIImageProvider::InitializeNormalizedDepthCells
bool InitializeNormalizedDepthCells()
Definition: OpenNIImageProvider.cpp:537
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
visionx::OpenNIPropertyDefinitions
Definition: OpenNIImageProvider.h:40
visionx::OpenNIImageProvider::NormalizedDepthCell::nx
float nx
Definition: OpenNIImageProvider.h:270
visionx::OpenNIImageProvider::DispatchDepthFrame
bool DispatchDepthFrame()
Definition: OpenNIImageProvider.cpp:723
visionx::OpenNIImageProvider::CreateDataStructures
bool CreateDataStructures()
Definition: OpenNIImageProvider.cpp:514
visionx::OpenNIImageProvider::CaptureColoredPoint
bool CaptureColoredPoint()
Definition: OpenNIImageProvider.cpp:609
visionx::OpenNIImageProvider
OpenNI image provider captures 3D-points and/or color(s) images from a single device and supports the...
Definition: OpenNIImageProvider.h:96
visionx::OpenNIImageProvider::EnsureFPS
int EnsureFPS(const int FPS)
Definition: OpenNIImageProvider.cpp:783
visionx::OpenNIImageProvider::isBackEndInitialized
static bool isBackEndInitialized
Definition: OpenNIImageProvider.h:284
visionx::OpenNIImageProvider::NormalizedDepthCell
OpenNI Capture.
Definition: OpenNIImageProvider.h:268
visionx::OpenNIImageProvider::videoDimension
visionx::ImageDimension videoDimension
Video dimension data.
Definition: OpenNIImageProvider.h:247
visionx::OpenNIImageProvider::DestroyDataStructure
bool DestroyDataStructure()
Definition: OpenNIImageProvider.cpp:590
visionx::OpenNIImageProvider::getDefaultName
virtual std::string getDefaultName() const
Definition: OpenNIImageProvider.h:223
visionx::OpenNIImageProvider::coloredPoint3DBuffer
visionx::ColoredPoint3D * coloredPoint3DBuffer
Captured images.
Definition: OpenNIImageProvider.h:262
visionx::CapturingImageProvider::capture
virtual void capture()
Definition: CapturingImageProvider.cpp:106
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:343
visionx::OpenNIImageProvider::StopDevice
bool StopDevice()
Definition: OpenNIImageProvider.cpp:569
visionx::OpenNIImageProvider::onExitCapturingImageProvider
virtual void onExitCapturingImageProvider()
Definition: OpenNIImageProvider.cpp:105
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
CapturingImageProvider.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::OpenNIImageProvider::NormalizedDepthCell::ny
float ny
Definition: OpenNIImageProvider.h:271
visionx::OpenNIImageProvider::depthStreamOpenNI
openni::VideoStream depthStreamOpenNI
Definition: OpenNIImageProvider.h:278
armarx::ComponentPropertyDefinitions::ComponentPropertyDefinitions
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition: Component.cpp:37
visionx::OpenNIImageProvider::DispatchColorFrame
bool DispatchColorFrame()
Definition: OpenNIImageProvider.cpp:759
visionx::OpenNIImageProvider::imageType
visionx::ImageType imageType
Capturing image type.
Definition: OpenNIImageProvider.h:257
visionx::OpenNIImageProvider::createPropertyDefinitions
virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions()
Definition: OpenNIImageProvider.h:231
visionx::OpenNIImageProvider::deviceOpenNI
openni::Device deviceOpenNI
Definition: OpenNIImageProvider.h:276
visionx::OpenNIImageProvider::normalizedDepthCells
NormalizedDepthCell * normalizedDepthCells
Definition: OpenNIImageProvider.h:274
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
visionx::OpenNIImageProvider::uri
std::string uri
Camera URI.
Definition: OpenNIImageProvider.h:242