AzureKinectIRImageProvider.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package VisionX::ArmarXObjects::AzureKinectIRImageProvider
17  * @author Mirko Wächter
18  * @author Christian R. G. Dreher <c.dreher@kit.edu>
19  * @date 2019
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 
26 
27 
28 // IVT
29 #include <Calibration/Calibration.h>
30 #include <Image/ImageProcessor.h>
31 
32 // ArmarX
37 
38 // VisionX
42 
43 namespace
44 {
45  /**
46  * @brief Converts a k4a image to an IVT image.
47  * @param color_image
48  * @param result
49  */
50  void
51  k4aToIvtImage(const k4a::image& color_image, ::CByteImage& result)
52  {
53  auto cw = static_cast<unsigned int>(color_image.get_width_pixels());
54  auto ch = static_cast<unsigned int>(color_image.get_height_pixels());
55  ARMARX_CHECK_EQUAL(static_cast<unsigned int>(result.width), cw);
56  ARMARX_CHECK_EQUAL(static_cast<unsigned int>(result.height), ch);
57  auto color_buffer = reinterpret_cast<const unsigned char*>(color_image.get_buffer());
58  auto rgb_buffer_ivt = result.pixels;
59 
60  int index_ivt = 0;
61  int index_k4a = 0;
62  for (unsigned int y = 0; y < ch; ++y)
63  {
64  for (unsigned int x = 0; x < cw; ++x)
65  {
66  rgb_buffer_ivt[index_ivt] =
67  float(color_buffer[index_k4a] + color_buffer[index_k4a + 1]) / 2.f;
68  index_ivt += 1;
69  index_k4a += 2;
70  }
71  }
72  }
73 } // namespace
74 
75 namespace visionx
76 {
77 
79  std::string prefix) :
81  {
82  }
83 
86  {
89  return defs;
90  }
91 
92  void
93  AzureKinectIRImageProvider::onStartCapture(float /* framesPerSecond */)
94  {
95  // Check for devices.
96  const uint32_t DEVICE_COUNT = k4a::device::get_installed_count();
97  if (DEVICE_COUNT == 0)
98  {
99  getArmarXManager()->asyncShutdown();
100  throw armarx::LocalException("No Azure Kinect devices detected!");
101  }
102 
103  device = k4a::device::open(K4A_DEVICE_DEFAULT);
104  k4aCalibration = device.get_calibration(config.depth_mode, config.color_resolution);
105 
106  transformation = k4a::transformation(k4aCalibration);
107 
108  device.start_cameras(&config);
109 
110  setMetaInfo("serialNumber", new armarx::Variant(device.get_serialnum()));
111  setMetaInfo("rgbVersion", new armarx::Variant(VersionToString(device.get_version().rgb)));
112  setMetaInfo("depthVersion",
113  new armarx::Variant(VersionToString(device.get_version().depth)));
114  setMetaInfo("depthSensorVersion",
115  new armarx::Variant(VersionToString(device.get_version().depth_sensor)));
116  setMetaInfo("audioVersion",
117  new armarx::Variant(VersionToString(device.get_version().audio)));
118  }
119 
120  bool
121  AzureKinectIRImageProvider::capture(void** pp_image_buffers)
122  {
124 
125  k4a::capture capture;
126  const std::chrono::milliseconds TIMEOUT{1000};
127 
128  bool status;
129  try
130  {
131  status = device.get_capture(&capture, TIMEOUT);
132  }
133  catch (const std::exception&)
134  {
135  ARMARX_WARNING << "Failed to get capture from device. Restarting camera.";
136  StopWatch sw;
137  device.stop_cameras();
138  device.start_cameras(&config);
139  ARMARX_INFO << "Restarting took " << sw.stop() << ".";
140  return false;
141  }
142 
143  if (status)
144  {
145  setMetaInfo("temperature", new armarx::Variant(capture.get_temperature_c()));
146 
147  const k4a::image irImage = capture.get_ir_image();
148  CByteImage buffer(depthDim.first, depthDim.second, CByteImage::eGrayScale);
149  k4aToIvtImage(irImage, buffer);
150 
151  std::memcpy(pp_image_buffers[0], buffer.pixels, 1024 * 1024);
152  return true;
153  }
154  return false;
155  }
156 
157  std::string
159  {
160  return "AzureKinectIRImageProvider";
161  }
162 
163  void
165  {
166  config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
167  auto fps = getProperty<float>("framerate").getValue();
168  if (fps == 5.0f)
169  {
170  config.camera_fps = K4A_FRAMES_PER_SECOND_5;
171  }
172  else if (fps == 15.0f)
173  {
174  config.camera_fps = K4A_FRAMES_PER_SECOND_15;
175  }
176  else if (fps == 30.0f)
177  {
178  config.camera_fps = K4A_FRAMES_PER_SECOND_30;
179  }
180  else
181  {
182  throw armarx::LocalException("Invalid framerate: ")
183  << fps << " - Only framerates 5, 15 and 30 are "
184  << "supported by Azure Kinect.";
185  }
186  frameRate = fps;
187  config.depth_mode = K4A_DEPTH_MODE_PASSIVE_IR;
188  config.color_format = K4A_IMAGE_FORMAT_COLOR_BGRA32;
189  config.color_resolution = K4A_COLOR_RESOLUTION_1080P;
190 
191  // This means that we'll only get captures that have both color and depth images, so we don't
192  // need to check if the capture contains a particular type of image.
193  config.synchronized_images_only = true;
194 
195  setNumberImages(1);
196  depthDim = GetDepthDimensions(config.depth_mode);
197  auto color_dim = GetColorDimensions(config.color_resolution);
198  ARMARX_INFO << "Depth image size: " << depthDim.first << "x" << depthDim.second;
199  ARMARX_INFO << "Color image size: " << color_dim.first << "x" << color_dim.second;
200 
201  setImageFormat(visionx::ImageDimension(depthDim.first, depthDim.second),
202  visionx::eGrayScale,
203  visionx::eBayerPatternGr);
204  resultIRImage.reset(visionx::tools::createByteImage(getImageFormat(), visionx::eRgb));
205  }
206 
207  void
209  {
210  }
211 
212  void
214  {
215  }
216 
217  std::string
219  {
220  return std::string();
221  }
222 
223  bool
225  {
226  return false;
227  }
228 
229  StereoCalibration
231  {
232  using namespace Eigen;
233  using Matrix3fRowMajor = Matrix<float, 3, 3, StorageOptions::RowMajor>;
235 
236  const auto convert_calibration =
237  [](const k4a_calibration_camera_t& k4a_calib, float scale = 1.)
238  {
239  MonocularCalibration calibration;
240 
241  const k4a_calibration_intrinsic_parameters_t& params = k4a_calib.intrinsics.parameters;
242  calibration.cameraParam.principalPoint = {params.param.cx * scale,
243  params.param.cy * scale};
244  calibration.cameraParam.focalLength = {params.param.fx * scale,
245  params.param.fy * scale};
246  // TODO: Figure out convertions. IVT (Calibration.h) expects 4 parameters:
247  // - The first radial lens distortion parameter.
248  // - The second radial lens distortion parameter.
249  // - The first tangential lens distortion parameter.
250  // - The second tangential lens distortion parameter.
251  // However, the Kinect offers k1-k6 radial distortion coefficients and 2 p1-p2
252  // tangential distortion parameters, which means that k3-k6 are unused.
253  // It is even unclear whether this is correct now, as previously it was a vector of all
254  // 6 k-params, which resulted in failed assertions in TypeMapping.cpp in the function
255  // CStereoCalibration* visionx::tools::convert(const visionx::StereoCalibration& stereoCalibration)
256  // lin 314 at time of this commit.
257  // See: https://microsoft.github.io/Azure-Kinect-Sensor-SDK/master/structk4a__calibration__intrinsic__parameters__t_1_1__param.html
258  calibration.cameraParam.distortion = {
259  params.param.k1,
260  params.param.k2,
261  params.param.p1,
262  params.param.p2,
263  };
264  calibration.cameraParam.width = k4a_calib.resolution_width;
265  calibration.cameraParam.height = k4a_calib.resolution_height;
266  const Matrix3fRowMajor ROTATION =
267  Map<const Matrix3fRowMajor>{k4a_calib.extrinsics.rotation};
268  calibration.cameraParam.rotation = convertEigenMatToVisionX(ROTATION);
269  calibration.cameraParam.translation = {k4a_calib.extrinsics.translation,
270  k4a_calib.extrinsics.translation + 3};
271 
272  return calibration;
273  };
274 
275  StereoCalibration calibration;
276 
277  calibration.calibrationLeft = calibration.calibrationRight =
278  convert_calibration(k4aCalibration.color_camera_calibration);
279 
280  calibration.rectificationHomographyLeft = calibration.rectificationHomographyRight =
282 
283 
284  return calibration;
285  }
286 
287 
288 } // namespace visionx
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
Eigen
Definition: Elements.h:32
armarx::ManagedIceObject::setMetaInfo
void setMetaInfo(const std::string &id, const VariantBasePtr &value)
Allows to set meta information that can be queried live via Ice interface on the ArmarXManager.
Definition: ManagedIceObject.cpp:795
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::CapturingImageProvider::frameRate
float frameRate
Required frame rate.
Definition: CapturingImageProvider.h:201
StopWatch.h
visionx::AzureKinectIRImageProvider::getReferenceFrame
std::string getReferenceFrame(const Ice::Current &current) override
Definition: AzureKinectIRImageProvider.cpp:218
visionx::AzureKinectIRImageProvider::onStopCapture
void onStopCapture() override
This is called when the image provider capturing has been stopped.
Definition: AzureKinectIRImageProvider.cpp:213
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:360
trace.h
AzureKinectIRImageProvider.h
visionx::AzureKinectIRImageProviderPropertyDefinitions::AzureKinectIRImageProviderPropertyDefinitions
AzureKinectIRImageProviderPropertyDefinitions(std::string prefix)
Definition: AzureKinectIRImageProvider.cpp:78
visionx::AzureKinectIRImageProvider::getDefaultName
virtual std::string getDefaultName() const override
Definition: AzureKinectIRImageProvider.cpp:158
visionx::AzureKinectIRImageProvider::GetColorDimensions
static std::pair< int, int > GetColorDimensions(const k4a_color_resolution_t resolution)
Definition: AzureKinectIRImageProvider.h:113
visionx::ImageProvider::setImageFormat
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
Definition: ImageProvider.cpp:305
visionx::ImageProvider::getImageFormat
ImageFormatInfo getImageFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the entire image format info struct via Ice.
Definition: ImageProvider.cpp:76
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
visionx::AzureKinectIRImageProvider::getImagesAreUndistorted
bool getImagesAreUndistorted(const Ice::Current &current) override
Definition: AzureKinectIRImageProvider.cpp:224
armarx::status
status
Definition: FiniteStateMachine.h:244
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
visionx::AzureKinectIRImageProvider::getStereoCalibration
StereoCalibration getStereoCalibration(const Ice::Current &current) override
Definition: AzureKinectIRImageProvider.cpp:230
visionx::AzureKinectIRImageProvider::onExitCapturingImageProvider
void onExitCapturingImageProvider() override
This is called when the Component::onExitComponent() setup is called.
Definition: AzureKinectIRImageProvider.cpp:208
visionx::AzureKinectIRImageProvider::createPropertyDefinitions
virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: AzureKinectIRImageProvider.cpp:85
visionx::CapturingPointCloudProviderPropertyDefinitions
Definition: CapturingPointCloudProvider.h:39
visionx::CapturingImageProvider::capture
virtual void capture()
Definition: CapturingImageProvider.cpp:109
visionx::AzureKinectIRImageProvider::VersionToString
static std::string VersionToString(const k4a_version_t &version)
Definition: AzureKinectIRImageProvider.h:159
visionx::AzureKinectIRImageProvider::onInitCapturingImageProvider
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
Definition: AzureKinectIRImageProvider.cpp:164
visionx::ImageProvider::setNumberImages
void setNumberImages(int numberImages)
Sets the number of images on each capture.
Definition: ImageProvider.cpp:344
visionx::AzureKinectIRImageProviderPropertyDefinitions
Definition: AzureKinectIRImageProvider.h:53
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
TimeUtil.h
float
#define float
Definition: 16_Level.h:22
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
visionx::AzureKinectIRImageProvider::onStartCapture
void onStartCapture(float frames_per_second) override
This is called when the image provider capturing has been started.
Definition: AzureKinectIRImageProvider.cpp:93
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::tools::convertEigenMatToVisionX
visionx::types::Mat convertEigenMatToVisionX(Eigen::MatrixXf m)
Definition: TypeMapping.cpp:686
ImageUtil.h
Eigen::Matrix
Definition: EigenForwardDeclarations.h:27
armarx::core::time::StopWatch
Measures the passed time between the construction or calling reset() and stop().
Definition: StopWatch.h:41
TypeMapping.h
visionx::AzureKinectIRImageProvider::GetDepthDimensions
static std::pair< int, int > GetDepthDimensions(const k4a_depth_mode_t depth_mode)
Definition: AzureKinectIRImageProvider.h:138
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
helper.h
Variant.h
scene3D::ROTATION
@ ROTATION
Definition: ManipulatorMode.h:36