This repository was archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 640
Expand file tree
/
Copy pathk4a.hpp
More file actions
1464 lines (1319 loc) · 46.2 KB
/
k4a.hpp
File metadata and controls
1464 lines (1319 loc) · 46.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** \file k4a.hpp
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
* Kinect For Azure SDK - C++ wrapper.
*/
#ifndef K4A_HPP
#define K4A_HPP
#include "k4a.h"
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string>
#include <vector>
namespace k4a
{
/**
* \defgroup cppsdk C++ Reference
*
* Functions part of the SDK.
*
* @{
*/
/** Exception type thrown when a K4A API call fails
*/
class error : public std::runtime_error
{
public:
using runtime_error::runtime_error;
};
// Helper functions not intended for use by client code
//
namespace internal
{
/// @cond FALSE
/** Casts an arithmetic value, clamping to the supported range of the type being cast to
*
* \returns input represented in output_type
*
* \remarks
* Clamps to 0 for unsigned types
*/
template<typename output_type, typename input_type> output_type clamp_cast(input_type input)
{
static_assert(std::is_arithmetic<input_type>::value, "clamp_cast only supports arithmetic types");
static_assert(std::is_arithmetic<output_type>::value, "clamp_cast only supports arithmetic types");
const input_type min_value = std::is_signed<input_type>() ?
static_cast<input_type>(std::numeric_limits<output_type>::min()) :
0;
input_type max_value = static_cast<input_type>(std::numeric_limits<output_type>::max());
if (max_value < 0)
{
// Output type is of greater or equal size to input type and we've overflowed.
//
max_value = std::numeric_limits<input_type>::max();
}
input = std::min(input, max_value);
input = std::max(input, min_value);
return static_cast<output_type>(input);
}
/// @endcond
} // namespace internal
/** \class image k4a.hpp <k4a/k4a.hpp>
* Wrapper for \ref k4a_image_t
*
* Wraps a handle for an image. Copying/moving is cheap, copies are shallow.
*
* \sa k4a_image_t
*/
class image
{
public:
/** Creates an image from a k4a_image_t.
* Takes ownership of the handle, i.e. assuming that handle has a refcount
* of 1, you should not call k4a_image_release on the handle after giving
* it to the image; the image will take care of that.
*/
image(k4a_image_t handle = nullptr) noexcept : m_handle(handle) {}
/** Creates a shallow copy of another image
*/
image(const image &other) noexcept : m_handle(other.m_handle)
{
if (m_handle != nullptr)
{
k4a_image_reference(m_handle);
}
}
/** Moves another image into a new image
*/
image(image &&other) noexcept : m_handle(other.m_handle)
{
other.m_handle = nullptr;
}
~image()
{
reset();
}
/** Sets image to a shallow copy of the other image
*/
image &operator=(const image &other) noexcept
{
if (this != &other)
{
reset();
m_handle = other.m_handle;
if (m_handle != nullptr)
{
k4a_image_reference(m_handle);
}
}
return *this;
}
/** Moves another image into this image; other is set to invalid
*/
image &operator=(image &&other) noexcept
{
if (this != &other)
{
reset();
m_handle = other.m_handle;
other.m_handle = nullptr;
}
return *this;
}
/** Invalidates this image
*/
image &operator=(std::nullptr_t) noexcept
{
reset();
return *this;
}
/** Returns true if two images refer to the same k4a_image_t, false otherwise
*/
bool operator==(const image &other) const noexcept
{
return m_handle == other.m_handle;
}
/** Returns false if the image is valid, true otherwise
*/
bool operator==(std::nullptr_t) const noexcept
{
return m_handle == nullptr;
}
/** Returns true if two images wrap different k4a_image_t instances, false otherwise
*/
bool operator!=(const image &other) const noexcept
{
return m_handle != other.m_handle;
}
/** Returns true if the image is valid, false otherwise
*/
bool operator!=(std::nullptr_t) const noexcept
{
return m_handle != nullptr;
}
/** Returns true if the image is valid, false otherwise
*/
explicit operator bool() const noexcept
{
return is_valid();
}
/** Returns true if the image is valid, false otherwise
*/
bool is_valid() const noexcept
{
return m_handle != nullptr;
}
/** Returns the underlying k4a_image_t handle
*
* Note that this function does not increment the reference count on the k4a_image_t.
* The caller is responsible for incrementing the reference count on
* the k4a_image_t if the caller needs the k4a_image_t to outlive this C++ object.
* Otherwise, the k4a_image_t will be destroyed by this C++ object.
*/
k4a_image_t handle() const noexcept
{
return m_handle;
}
/** Releases the underlying k4a_image_t; the image is set to invalid.
*/
void reset() noexcept
{
if (m_handle != nullptr)
{
k4a_image_release(m_handle);
m_handle = nullptr;
}
}
/** Create a blank image
* Throws error on failure
*
* \sa k4a_image_create
*/
static image create(k4a_image_format_t format, int width_pixels, int height_pixels, int stride_bytes)
{
k4a_image_t handle = nullptr;
k4a_result_t result = k4a_image_create(format, width_pixels, height_pixels, stride_bytes, &handle);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Failed to create image!");
}
return image(handle);
}
/** Create an image from a pre-allocated buffer
* Throws error on failure
*
* \sa k4a_image_create_from_buffer
*/
static image create_from_buffer(k4a_image_format_t format,
int width_pixels,
int height_pixels,
int stride_bytes,
uint8_t *buffer,
size_t buffer_size,
k4a_memory_destroy_cb_t *buffer_release_cb,
void *buffer_release_cb_context)
{
k4a_image_t handle = nullptr;
k4a_result_t result = k4a_image_create_from_buffer(format,
width_pixels,
height_pixels,
stride_bytes,
buffer,
buffer_size,
buffer_release_cb,
buffer_release_cb_context,
&handle);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Failed to create image from buffer");
}
return image(handle);
}
/** Get the image buffer
*
* \sa k4a_image_get_buffer
*/
template<typename T = uint8_t>
T *get_buffer() noexcept
{
return reinterpret_cast<T*>(k4a_image_get_buffer(m_handle));
}
/** Get the image buffer
*
* \sa k4a_image_get_buffer
*/
template<typename T = uint8_t>
const T *get_buffer() const noexcept
{
return reinterpret_cast<T*>(k4a_image_get_buffer(m_handle));
}
/** Get the image buffer size in bytes
*
* \sa k4a_image_get_size
*/
size_t get_size() const noexcept
{
return k4a_image_get_size(m_handle);
}
/** Get the image format of the image
*
* \sa k4a_image_get_format
*/
k4a_image_format_t get_format() const noexcept
{
return k4a_image_get_format(m_handle);
}
/** Get the image width in pixels
*
* \sa k4a_image_get_width_pixels
*/
int get_width_pixels() const noexcept
{
return k4a_image_get_width_pixels(m_handle);
}
/** Get the image height in pixels
*
* \sa k4a_image_get_height_pixels
*/
int get_height_pixels() const noexcept
{
return k4a_image_get_height_pixels(m_handle);
}
/** Get the image stride in bytes
*
* \sa k4a_image_get_stride_bytes
*/
int get_stride_bytes() const noexcept
{
return k4a_image_get_stride_bytes(m_handle);
}
/** Get the image's device timestamp in microseconds
*
* \sa k4a_image_get_device_timestamp_usec
*/
std::chrono::microseconds get_device_timestamp() const noexcept
{
return std::chrono::microseconds(k4a_image_get_device_timestamp_usec(m_handle));
}
/** Get the image's system timestamp in nanoseconds
*
* \sa k4a_image_get_system_timestamp_nsec
*/
std::chrono::nanoseconds get_system_timestamp() const noexcept
{
return std::chrono::nanoseconds(k4a_image_get_system_timestamp_nsec(m_handle));
}
/** Get the image exposure time in microseconds
*
* \sa k4a_image_get_exposure_usec
*/
std::chrono::microseconds get_exposure() const noexcept
{
return std::chrono::microseconds(k4a_image_get_exposure_usec(m_handle));
}
/** Get the image white balance in Kelvin (color images only)
*
* \sa k4a_image_get_white_balance
*/
uint32_t get_white_balance() const noexcept
{
return k4a_image_get_white_balance(m_handle);
}
/** Get the image's ISO speed (color images only)
*
* \sa k4a_image_get_white_balance
*/
uint32_t get_iso_speed() const noexcept
{
return k4a_image_get_iso_speed(m_handle);
}
/** Set the image's timestamp in microseconds
*
* \sa k4a_image_set_device_timestamp_usec
*/
void set_timestamp(std::chrono::microseconds timestamp) noexcept
{
k4a_image_set_device_timestamp_usec(m_handle, internal::clamp_cast<uint64_t>(timestamp.count()));
}
/** Set the image's exposure time in microseconds (color images only)
*
* \sa k4a_image_set_exposure_time_usec
*/
void set_exposure_time(std::chrono::microseconds exposure) noexcept
{
k4a_image_set_exposure_usec(m_handle, internal::clamp_cast<uint64_t>(exposure.count()));
}
/** Set the white balance of the image (color images only)
*
* \sa k4a_image_set_white_balance
*/
void set_white_balance(uint32_t white_balance) noexcept
{
k4a_image_set_white_balance(m_handle, white_balance);
}
/** Set the ISO speed of the image (color images only)
*
* \sa k4a_image_set_iso_speed
*/
void set_iso_speed(uint32_t iso_speed) noexcept
{
k4a_image_set_iso_speed(m_handle, iso_speed);
}
private:
k4a_image_t m_handle;
};
/** \class capture k4a.hpp <k4a/k4a.hpp>
* Wrapper for \ref k4a_capture_t
*
* Wraps a handle for a capture. Copying/moving is cheap, copies are shallow.
*/
class capture
{
public:
/** Creates a capture from a k4a_capture_t
* Takes ownership of the handle, i.e. assuming that handle has a refcount
* of 1, you should not call k4a_capture_release on the handle after giving
* it to the capture; the capture will take care of that.
*/
capture(k4a_capture_t handle = nullptr) noexcept : m_handle(handle) {}
/** Creates a shallow copy of another capture
*/
capture(const capture &other) noexcept : m_handle(other.m_handle)
{
if (m_handle != nullptr)
{
k4a_capture_reference(m_handle);
}
}
/** Moves another capture into a new capture
*/
capture(capture &&other) noexcept : m_handle(other.m_handle)
{
other.m_handle = nullptr;
}
~capture()
{
reset();
}
/** Sets capture to a shallow copy of the other image
*/
capture &operator=(const capture &other) noexcept
{
if (this != &other)
{
reset();
m_handle = other.m_handle;
if (m_handle != nullptr)
{
k4a_capture_reference(m_handle);
}
}
return *this;
}
/** Moves another capture into this capture; other is set to invalid
*/
capture &operator=(capture &&other) noexcept
{
if (this != &other)
{
reset();
m_handle = other.m_handle;
other.m_handle = nullptr;
}
return *this;
}
/** Invalidates this capture
*/
capture &operator=(std::nullptr_t) noexcept
{
reset();
return *this;
}
/** Returns true if two captures refer to the same k4a_capture_t, false otherwise
*/
bool operator==(const capture &other) const noexcept
{
return m_handle == other.m_handle;
}
/** Returns false if the capture is valid, true otherwise
*/
bool operator==(std::nullptr_t) const noexcept
{
return m_handle == nullptr;
}
/** Returns true if two captures wrap different k4a_capture_t instances, false otherwise
*/
bool operator!=(const capture &other) const noexcept
{
return m_handle != other.m_handle;
}
/** Returns true if the capture is valid, false otherwise
*/
bool operator!=(std::nullptr_t) const noexcept
{
return m_handle != nullptr;
}
/** Returns true if the capture is valid, false otherwise
*/
explicit operator bool() const noexcept
{
return is_valid();
}
/** Returns true if the capture is valid, false otherwise
*/
bool is_valid() const noexcept
{
return m_handle != nullptr;
}
/** Returns the underlying k4a_capture_t handle
*
* Note that this function does not increment the reference count on the k4a_capture_t.
* The caller is responsible for incrementing the reference count on
* the k4a_capture_t if the caller needs the k4a_capture_t to outlive this C++ object.
* Otherwise, the k4a_capture_t will be destroyed by this C++ object.
*/
k4a_capture_t handle() const noexcept
{
return m_handle;
}
/** Releases the underlying k4a_capture_t; the capture is set to invalid.
*/
void reset() noexcept
{
if (m_handle != nullptr)
{
k4a_capture_release(m_handle);
m_handle = nullptr;
}
}
/** Get the color image associated with the capture
*
* \sa k4a_capture_get_color_image
*/
image get_color_image() const noexcept
{
return image(k4a_capture_get_color_image(m_handle));
}
/** Get the depth image associated with the capture
*
* \sa k4a_capture_get_depth_image
*/
image get_depth_image() const noexcept
{
return image(k4a_capture_get_depth_image(m_handle));
}
/** Get the IR image associated with the capture
*
* \sa k4a_capture_get_ir_image
*/
image get_ir_image() const noexcept
{
return image(k4a_capture_get_ir_image(m_handle));
}
/** Set / add a color image to the capture
*
* \sa k4a_capture_set_color_image
*/
void set_color_image(const image &color_image) noexcept
{
k4a_capture_set_color_image(m_handle, color_image.handle());
}
/** Set / add a depth image to the capture
*
* \sa k4a_capture_set_depth_image
*/
void set_depth_image(const image &depth_image) noexcept
{
k4a_capture_set_depth_image(m_handle, depth_image.handle());
}
/** Set / add an IR image to the capture
*
* \sa k4a_capture_set_ir_image
*/
void set_ir_image(const image &ir_image) noexcept
{
k4a_capture_set_ir_image(m_handle, ir_image.handle());
}
/** Set the temperature associated with the capture in Celsius.
*
* \sa k4a_capture_set_temperature_c
*/
void set_temperature_c(float temperature_c) noexcept
{
k4a_capture_set_temperature_c(m_handle, temperature_c);
}
/** Get temperature (in Celsius) associated with the capture.
*
* \sa k4a_capture_get_temperature_c
*/
float get_temperature_c() const noexcept
{
return k4a_capture_get_temperature_c(m_handle);
}
/** Create an empty capture object.
* Throws error on failure.
*
* \sa k4a_capture_create
*/
static capture create()
{
k4a_capture_t handle = nullptr;
k4a_result_t result = k4a_capture_create(&handle);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Failed to create capture!");
}
return capture(handle);
}
private:
k4a_capture_t m_handle;
};
/** \class calibration k4a.hpp <k4a/k4a.hpp>
* Wrapper for \ref k4a_calibration_t
*
* Provides member functions for k4a_calibration_t.
*/
struct calibration : public k4a_calibration_t
{
/** Transform a 3d point of a source coordinate system into a 3d point of the target coordinate system.
* Throws error on failure.
*
* \sa k4a_calibration_3d_to_3d
*/
k4a_float3_t convert_3d_to_3d(const k4a_float3_t &source_point3d,
k4a_calibration_type_t source_camera,
k4a_calibration_type_t target_camera) const
{
k4a_float3_t target_point3d;
k4a_result_t result =
k4a_calibration_3d_to_3d(this, &source_point3d, source_camera, target_camera, &target_point3d);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Calibration contained invalid transformation parameters!");
}
return target_point3d;
}
/** Transform a 2d pixel coordinate with an associated depth value of the source camera into a 3d point of the
* target coordinate system.
* Returns false if the point is invalid in the target coordinate system (and therefore target_point3d should not be
* used)
* Throws error if calibration contains invalid data.
*
* \sa k4a_calibration_2d_to_3d
*/
bool convert_2d_to_3d(const k4a_float2_t &source_point2d,
float source_depth,
k4a_calibration_type_t source_camera,
k4a_calibration_type_t target_camera,
k4a_float3_t *target_point3d) const
{
int valid = 0;
k4a_result_t result = k4a_calibration_2d_to_3d(
this, &source_point2d, source_depth, source_camera, target_camera, target_point3d, &valid);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Calibration contained invalid transformation parameters!");
}
return static_cast<bool>(valid);
}
/** Transform a 3d point of a source coordinate system into a 2d pixel coordinate of the target camera.
* Returns false if the point is invalid in the target coordinate system (and therefore target_point2d should not be
* used)
* Throws error if calibration contains invalid data.
*
* \sa k4a_calibration_3d_to_2d
*/
bool convert_3d_to_2d(const k4a_float3_t &source_point3d,
k4a_calibration_type_t source_camera,
k4a_calibration_type_t target_camera,
k4a_float2_t *target_point2d) const
{
int valid = 0;
k4a_result_t result =
k4a_calibration_3d_to_2d(this, &source_point3d, source_camera, target_camera, target_point2d, &valid);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Calibration contained invalid transformation parameters!");
}
return static_cast<bool>(valid);
}
/** Transform a 2d pixel coordinate with an associated depth value of the source camera into a 2d pixel coordinate
* of the target camera
* Returns false if the point is invalid in the target coordinate system (and therefore target_point2d should not be
* used)
* Throws error if calibration contains invalid data.
*
* \sa k4a_calibration_2d_to_2d
*/
bool convert_2d_to_2d(const k4a_float2_t &source_point2d,
float source_depth,
k4a_calibration_type_t source_camera,
k4a_calibration_type_t target_camera,
k4a_float2_t *target_point2d) const
{
int valid = 0;
k4a_result_t result = k4a_calibration_2d_to_2d(
this, &source_point2d, source_depth, source_camera, target_camera, target_point2d, &valid);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Calibration contained invalid transformation parameters!");
}
return static_cast<bool>(valid);
}
/** Transform a 2D pixel coordinate from color camera into a 2D pixel coordinate of the depth camera. This function
* searches along an epipolar line in the depth image to find the corresponding depth pixel.
* Returns false if the point is invalid in the target coordinate system (and therefore target_point2d should not be
* used) Throws error if calibration contains invalid data.
*
* \sa k4a_calibration_color_2d_to_depth_2d
*/
bool convert_color_2d_to_depth_2d(const k4a_float2_t &source_point2d,
const image &depth_image,
k4a_float2_t *target_point2d) const
{
int valid = 0;
k4a_result_t result =
k4a_calibration_color_2d_to_depth_2d(this, &source_point2d, depth_image.handle(), target_point2d, &valid);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Calibration contained invalid transformation parameters!");
}
return static_cast<bool>(valid);
}
/** Get the camera calibration for a device from a raw calibration blob.
* Throws error on failure.
*
* \sa k4a_calibration_get_from_raw
*/
static calibration get_from_raw(char *raw_calibration,
size_t raw_calibration_size,
k4a_depth_mode_t target_depth_mode,
k4a_color_resolution_t target_color_resolution)
{
calibration calib;
k4a_result_t result = k4a_calibration_get_from_raw(raw_calibration,
raw_calibration_size,
target_depth_mode,
target_color_resolution,
&calib);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Failed to load calibration from raw calibration blob!");
}
return calib;
}
/** Get the camera calibration for a device from a raw calibration blob.
* Throws error on failure.
*
* \sa k4a_calibration_get_from_raw
*/
static calibration get_from_raw(uint8_t *raw_calibration,
size_t raw_calibration_size,
k4a_depth_mode_t target_depth_mode,
k4a_color_resolution_t target_color_resolution)
{
return get_from_raw(reinterpret_cast<char *>(raw_calibration),
raw_calibration_size,
target_depth_mode,
target_color_resolution);
}
/** Get the camera calibration for a device from a raw calibration blob.
* Throws error on failure.
*
* \sa k4a_calibration_get_from_raw
*/
static calibration get_from_raw(std::vector<uint8_t> &raw_calibration,
k4a_depth_mode_t target_depth_mode,
k4a_color_resolution_t target_color_resolution)
{
return get_from_raw(reinterpret_cast<char *>(raw_calibration.data()),
raw_calibration.size(),
target_depth_mode,
target_color_resolution);
}
};
/** \class transformation k4a.hpp <k4a/k4a.hpp>
* Wrapper for \ref k4a_transformation_t
*
* Wraps a handle for a transformation.
*/
class transformation
{
public:
/** Creates a transformation associated with calibration
*
* \sa k4a_transformation_create
*/
transformation(const k4a_calibration_t &calibration) noexcept :
m_handle(k4a_transformation_create(&calibration)),
m_color_resolution({ calibration.color_camera_calibration.resolution_width,
calibration.color_camera_calibration.resolution_height }),
m_depth_resolution({ calibration.depth_camera_calibration.resolution_width,
calibration.depth_camera_calibration.resolution_height })
{
}
/** Creates a transformation from a k4a_transformation_t
* Takes ownership of the handle, i.e. you should not call
* k4a_transformation_destroy on the handle after giving
* it to the transformation; the transformation will take care of that.
*/
transformation(k4a_transformation_t handle = nullptr) noexcept : m_handle(handle) {}
/** Moves another tranformation into a new transformation
*/
transformation(transformation &&other) noexcept :
m_handle(other.m_handle),
m_color_resolution(other.m_color_resolution),
m_depth_resolution(other.m_depth_resolution)
{
other.m_handle = nullptr;
}
transformation(const transformation &) = delete;
~transformation()
{
destroy();
}
/** Moves another image into this image; other is set to invalid
*/
transformation &operator=(transformation &&other) noexcept
{
if (this != &other)
{
destroy();
m_handle = other.m_handle;
m_color_resolution = other.m_color_resolution;
m_depth_resolution = other.m_depth_resolution;
other.m_handle = nullptr;
}
return *this;
}
/** Invalidates this transformation
*/
transformation &operator=(std::nullptr_t) noexcept
{
destroy();
return *this;
}
transformation &operator=(const transformation &) = delete;
/** Invalidates this transformation
*/
void destroy() noexcept
{
if (m_handle != nullptr)
{
k4a_transformation_destroy(m_handle);
m_handle = nullptr;
}
}
/** Transforms the depth map into the geometry of the color camera.
* Throws error on failure
*
* \sa k4a_transformation_depth_image_to_color_camera
* Transforms the output in to the existing caller provided \p transformed_depth_image.
*/
void depth_image_to_color_camera(const image &depth_image, image *transformed_depth_image) const
{
k4a_result_t result = k4a_transformation_depth_image_to_color_camera(m_handle,
depth_image.handle(),
transformed_depth_image->handle());
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Failed to convert depth map to color camera geometry!");
}
}
/** Transforms the depth map into the geometry of the color camera.
* Throws error on failure
*
* \sa k4a_transformation_depth_image_to_color_camera
* Creates a new image with the output.
*/
image depth_image_to_color_camera(const image &depth_image) const
{
image transformed_depth_image = image::create(K4A_IMAGE_FORMAT_DEPTH16,
m_color_resolution.width,
m_color_resolution.height,
m_color_resolution.width *
static_cast<int32_t>(sizeof(uint16_t)));
depth_image_to_color_camera(depth_image, &transformed_depth_image);
return transformed_depth_image;
}
/** Transforms depth map and a custom image into the geometry of the color camera.
* Throws error on failure
*
* \sa k4a_transformation_depth_image_to_color_camera_custom
* Transforms the output in to the existing caller provided \p transformed_depth_image \p transformed_custom_image.
*/
void depth_image_to_color_camera_custom(const image &depth_image,
const image &custom_image,
image *transformed_depth_image,
image *transformed_custom_image,
k4a_transformation_interpolation_type_t interpolation_type,
uint32_t invalid_custom_value) const
{
k4a_result_t result = k4a_transformation_depth_image_to_color_camera_custom(m_handle,
depth_image.handle(),
custom_image.handle(),
transformed_depth_image->handle(),
transformed_custom_image->handle(),
interpolation_type,
invalid_custom_value);
if (K4A_RESULT_SUCCEEDED != result)
{
throw error("Failed to convert depth map and custom image to color camera geometry!");
}
}
/** Transforms depth map and a custom image into the geometry of the color camera.
* Throws error on failure
*
* \sa k4a_transformation_depth_image_to_color_camera_custom
* Creates a new image with the output.
*/
std::pair<image, image>
depth_image_to_color_camera_custom(const image &depth_image,
const image &custom_image,
k4a_transformation_interpolation_type_t interpolation_type,
uint32_t invalid_custom_value) const
{
image transformed_depth_image = image::create(K4A_IMAGE_FORMAT_DEPTH16,
m_color_resolution.width,
m_color_resolution.height,
m_color_resolution.width *
static_cast<int32_t>(sizeof(uint16_t)));
int32_t bytes_per_pixel;
switch (custom_image.get_format())
{
case K4A_IMAGE_FORMAT_CUSTOM8:
bytes_per_pixel = static_cast<int32_t>(sizeof(int8_t));
break;
case K4A_IMAGE_FORMAT_CUSTOM16:
bytes_per_pixel = static_cast<int32_t>(sizeof(int16_t));
break;
default:
throw error("Failed to support this format of custom image!");
}
image transformed_custom_image = image::create(custom_image.get_format(),
m_color_resolution.width,
m_color_resolution.height,
m_color_resolution.width * bytes_per_pixel);
depth_image_to_color_camera_custom(depth_image,
custom_image,
&transformed_depth_image,
&transformed_custom_image,
interpolation_type,
invalid_custom_value);