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
43namespace
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
75namespace visionx
76{
77
83
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
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
211
212 void
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 =
281 convertEigenMatToVisionX(Matrix3f::Identity());
282
283
284 return calibration;
285 }
286
287
288} // namespace visionx
#define float
Definition 16_Level.h:22
Eigen::Matrix< T, 3, 3 > Matrix
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
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.
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
The Variant class is described here: Variants.
Definition Variant.h:224
Measures the passed time between the construction or calling reset() and stop().
Definition StopWatch.h:42
bool capture(void **pp_image_buffers) override
Main capturing function.
static std::pair< int, int > GetDepthDimensions(const k4a_depth_mode_t depth_mode)
void onStartCapture(float frames_per_second) override
This is called when the image provider capturing has been started.
static std::pair< int, int > GetColorDimensions(const k4a_color_resolution_t resolution)
virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
static std::string VersionToString(const k4a_version_t &version)
bool getImagesAreUndistorted(const Ice::Current &current) override
void onStopCapture() override
This is called when the image provider capturing has been stopped.
void onExitCapturingImageProvider() override
This is called when the Component::onExitComponent() setup is called.
std::string getReferenceFrame(const Ice::Current &current) override
StereoCalibration getStereoCalibration(const Ice::Current &current) override
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
virtual std::string getDefaultName() const override
ImageFormatInfo getImageFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the entire image format info struct via Ice.
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
void setNumberImages(int numberImages)
Sets the number of images on each capture.
#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...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
visionx::types::Mat convertEigenMatToVisionX(Eigen::MatrixXf m)
ArmarX headers.