openni_device_kinect.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011 2011 Willow Garage, Inc.
5  * Suat Gedikli <gedikli@willowgarage.com>
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
39 #include <iostream>
40 #include <sstream>
41 #include <boost/thread/mutex.hpp>
42 
43 using namespace boost;
44 
45 namespace openni_wrapper
46 {
47 
48 DeviceKinect::DeviceKinect (xn::Context& context, const xn::NodeInfo& device_node, const xn::NodeInfo& image_node, const xn::NodeInfo& depth_node, const xn::NodeInfo& ir_node) throw (OpenNIException)
49 : OpenNIDevice (context, device_node, image_node, depth_node, ir_node)
50 , debayering_method_ (ImageBayerGRBG::EdgeAwareWeighted)
51 {
52  // setup stream modes
53  enumAvailableModes ();
54  setDepthOutputMode (getDefaultDepthMode ());
55  setImageOutputMode (getDefaultImageMode ());
56  setIROutputMode (getDefaultIRMode ());
57 
58  // device specific initialization
59  XnStatus status;
60 
61  unique_lock<mutex> image_lock(image_mutex_);
62  // set kinect specific format. Thus input = uncompressed Bayer, output = grayscale = bypass = bayer
63  status = image_generator_.SetIntProperty ("InputFormat", 6);
64  if (status != XN_STATUS_OK)
65  THROW_OPENNI_EXCEPTION ("Error setting the image input format to Uncompressed 8-bit BAYER. Reason: %s", xnGetStatusString (status));
66 
67  // Grayscale: bypass debayering -> gives us bayer pattern!
68  status = image_generator_.SetPixelFormat (XN_PIXEL_FORMAT_GRAYSCALE_8_BIT);
69  if (status != XN_STATUS_OK)
70  THROW_OPENNI_EXCEPTION ("Failed to set image pixel format to 8bit-grayscale. Reason: %s", xnGetStatusString (status));
71  image_lock.unlock ();
72 
73  lock_guard<mutex> depth_lock(depth_mutex_);
74  // RegistrationType should be 2 (software) for Kinect, 1 (hardware) for PS
75  status = depth_generator_.SetIntProperty ("RegistrationType", 2);
76  if (status != XN_STATUS_OK)
77  THROW_OPENNI_EXCEPTION ("Error setting the registration type. Reason: %s", xnGetStatusString (status));
78 }
79 
80 DeviceKinect::~DeviceKinect () throw ()
81 {
82  depth_mutex_.lock ();
83  depth_generator_.UnregisterFromNewDataAvailable (depth_callback_handle_);
84  depth_mutex_.unlock ();
85 
86  image_mutex_.lock ();
87  image_generator_.UnregisterFromNewDataAvailable (image_callback_handle_);
88  image_mutex_.unlock ();
89 }
90 
91 bool DeviceKinect::isImageResizeSupported (unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const throw ()
92 {
93  return ImageBayerGRBG::resizingSupported (input_width, input_height, output_width, output_height);
94 }
95 
96 void DeviceKinect::enumAvailableModes () throw (OpenNIException)
97 {
98  XnMapOutputMode output_mode;
99  available_image_modes_.clear();
100  available_depth_modes_.clear();
101 
102  output_mode.nFPS = 30;
103  output_mode.nXRes = XN_VGA_X_RES;
104  output_mode.nYRes = XN_VGA_Y_RES;
105  available_image_modes_.push_back (output_mode);
106  available_depth_modes_.push_back (output_mode);
107 
108  output_mode.nFPS = 15;
109  output_mode.nXRes = XN_SXGA_X_RES;
110  output_mode.nYRes = XN_SXGA_Y_RES;
111  available_image_modes_.push_back (output_mode);
112 }
113 
114 boost::shared_ptr<Image> DeviceKinect::getCurrentImage (boost::shared_ptr<xn::ImageMetaData> image_data) const throw ()
115 {
116  return boost::shared_ptr<Image> (new ImageBayerGRBG (image_data, debayering_method_));
117 }
118 
119 void DeviceKinect::setSynchronization (bool on_off) throw (OpenNIException)
120 {
121  if (on_off)
122  THROW_OPENNI_EXCEPTION ("Microsoft Kinect does not support Hardware synchronization.");
123 }
124 
125 bool DeviceKinect::isSynchronized () const throw (OpenNIException)
126 {
127  return false;
128 }
129 
130 bool DeviceKinect::isSynchronizationSupported () const throw ()
131 {
132  return false;
133 }
134 
135 bool DeviceKinect::isDepthCropped () const throw (OpenNIException)
136 {
137  return false;
138 }
139 
140 void DeviceKinect::setDepthCropping (unsigned x, unsigned y, unsigned width, unsigned height) throw (OpenNIException)
141 {
142  if (width != 0 && height != 0)
143  THROW_OPENNI_EXCEPTION ("Microsoft Kinect does not support cropping for the depth stream.");
144 }
145 
146 bool DeviceKinect::isDepthCroppingSupported () const throw ()
147 {
148  return false;
149 }
150 
151 } //namespace
#define THROW_OPENNI_EXCEPTION(format,...)
General exception class.
string status
Definition: test_launch.py:51
This class provides methods to fill a RGB or Grayscale image buffer from underlying Bayer pattern ima...
Class representing an astract device for Primesense or MS Kinect devices.
Definition: openni_device.h:66


openni_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu
autogenerated on Mon Jun 10 2019 14:15:53