33class error :
public std::runtime_error
36 using runtime_error::runtime_error;
51template<
typename output_type,
typename input_type> output_type clamp_cast(input_type input)
53 static_assert(std::is_arithmetic<input_type>::value,
"clamp_cast only supports arithmetic types");
54 static_assert(std::is_arithmetic<output_type>::value,
"clamp_cast only supports arithmetic types");
55 const input_type min_value = std::is_signed<input_type>() ?
56 static_cast<input_type
>(std::numeric_limits<output_type>::min()) :
59 input_type max_value =
static_cast<input_type
>(std::numeric_limits<output_type>::max());
64 max_value = std::numeric_limits<input_type>::max();
66 input = std::min(input, max_value);
67 input = std::max(input, min_value);
68 return static_cast<output_type
>(input);
92 image(
const image &other) noexcept : m_handle(other.m_handle)
94 if (m_handle !=
nullptr)
96 k4a_image_reference(m_handle);
104 other.m_handle =
nullptr;
119 m_handle = other.m_handle;
120 if (m_handle !=
nullptr)
122 k4a_image_reference(m_handle);
135 m_handle = other.m_handle;
136 other.m_handle =
nullptr;
153 return m_handle == other.m_handle;
160 return m_handle ==
nullptr;
167 return m_handle != other.m_handle;
174 return m_handle !=
nullptr;
179 explicit operator bool() const noexcept
188 return m_handle !=
nullptr;
207 if (m_handle !=
nullptr)
209 k4a_image_release(m_handle);
222 k4a_result_t result = k4a_image_create(format, width_pixels, height_pixels, stride_bytes, &
handle);
225 throw error(
"Failed to create image!");
242 void *buffer_release_cb_context)
245 k4a_result_t result = k4a_image_create_from_buffer(format,
252 buffer_release_cb_context,
256 throw error(
"Failed to create image from buffer");
267 return k4a_image_get_buffer(m_handle);
276 return k4a_image_get_buffer(m_handle);
285 return k4a_image_get_size(m_handle);
294 return k4a_image_get_format(m_handle);
303 return k4a_image_get_width_pixels(m_handle);
312 return k4a_image_get_height_pixels(m_handle);
321 return k4a_image_get_stride_bytes(m_handle);
330 return std::chrono::microseconds(k4a_image_get_device_timestamp_usec(m_handle));
339 return std::chrono::nanoseconds(k4a_image_get_system_timestamp_nsec(m_handle));
350 return std::chrono::microseconds(k4a_image_get_exposure_usec(m_handle));
361 return k4a_image_get_white_balance(m_handle);
372 return k4a_image_get_iso_speed(m_handle);
381 k4a_image_set_device_timestamp_usec(m_handle, internal::clamp_cast<uint64_t>(timestamp.count()));
392 k4a_image_set_exposure_usec(m_handle, internal::clamp_cast<uint64_t>(exposure.count()));
403 k4a_image_set_white_balance(m_handle, white_balance);
414 k4a_image_set_iso_speed(m_handle, iso_speed);
440 if (m_handle !=
nullptr)
442 k4a_capture_reference(m_handle);
450 other.m_handle =
nullptr;
466 m_handle = other.m_handle;
467 if (m_handle !=
nullptr)
469 k4a_capture_reference(m_handle);
482 m_handle = other.m_handle;
483 other.m_handle =
nullptr;
500 return m_handle == other.m_handle;
507 return m_handle ==
nullptr;
514 return m_handle != other.m_handle;
521 return m_handle !=
nullptr;
526 explicit operator bool() const noexcept
535 return m_handle !=
nullptr;
554 if (m_handle !=
nullptr)
556 k4a_capture_release(m_handle);
567 return image(k4a_capture_get_color_image(m_handle));
576 return image(k4a_capture_get_depth_image(m_handle));
585 return image(k4a_capture_get_ir_image(m_handle));
594 k4a_capture_set_color_image(m_handle, color_image.handle());
603 k4a_capture_set_depth_image(m_handle, depth_image.handle());
612 k4a_capture_set_ir_image(m_handle, ir_image.handle());
623 k4a_capture_set_temperature_c(m_handle, temperature_c);
634 return k4a_capture_get_temperature_c(m_handle);
648 throw error(
"Failed to create capture!");
679 throw error(
"Calibration contained invalid transformation parameters!");
681 return target_point3d;
700 this, &source_point2d, source_depth, source_camera, target_camera, target_point3d, &valid);
704 throw error(
"Calibration contained invalid transformation parameters!");
706 return static_cast<bool>(valid);
727 throw error(
"Calibration contained invalid transformation parameters!");
729 return static_cast<bool>(valid);
748 this, &source_point2d, source_depth, source_camera, target_camera, target_point2d, &valid);
752 throw error(
"Calibration contained invalid transformation parameters!");
754 return static_cast<bool>(valid);
765 const image &depth_image,
774 throw error(
"Calibration contained invalid transformation parameters!");
776 return static_cast<bool>(valid);
785 size_t raw_calibration_size,
790 k4a_result_t result = k4a_calibration_get_from_raw(raw_calibration,
791 raw_calibration_size,
793 target_color_resolution,
798 throw error(
"Failed to load calibration from raw calibration blob!");
809 size_t raw_calibration_size,
813 return get_from_raw(
reinterpret_cast<char *
>(raw_calibration),
814 raw_calibration_size,
816 target_color_resolution);
828 return get_from_raw(
reinterpret_cast<char *
>(raw_calibration.data()),
829 raw_calibration.size(),
831 target_color_resolution);
866 m_handle(other.m_handle),
867 m_color_resolution(other.m_color_resolution),
868 m_depth_resolution(other.m_depth_resolution)
870 other.m_handle =
nullptr;
887 m_handle = other.m_handle;
888 m_color_resolution = other.m_color_resolution;
889 m_depth_resolution = other.m_depth_resolution;
890 other.m_handle =
nullptr;
910 if (m_handle !=
nullptr)
912 k4a_transformation_destroy(m_handle);
925 k4a_result_t result = k4a_transformation_depth_image_to_color_camera(m_handle,
927 transformed_depth_image->
handle());
930 throw error(
"Failed to convert depth map to color camera geometry!");
943 m_color_resolution.width,
944 m_color_resolution.height,
945 m_color_resolution.width *
946 static_cast<int32_t
>(
sizeof(uint16_t)));
948 return transformed_depth_image;
958 const image &custom_image,
959 image *transformed_depth_image,
960 image *transformed_custom_image,
962 uint32_t invalid_custom_value)
const
964 k4a_result_t result = k4a_transformation_depth_image_to_color_camera_custom(m_handle,
967 transformed_depth_image->
handle(),
968 transformed_custom_image->
handle(),
970 invalid_custom_value);
973 throw error(
"Failed to convert depth map and custom image to color camera geometry!");
983 std::pair<image, image>
985 const image &custom_image,
987 uint32_t invalid_custom_value)
const
990 m_color_resolution.width,
991 m_color_resolution.height,
992 m_color_resolution.width *
993 static_cast<int32_t
>(
sizeof(uint16_t)));
994 int32_t bytes_per_pixel;
998 bytes_per_pixel =
static_cast<int32_t
>(
sizeof(int8_t));
1001 bytes_per_pixel =
static_cast<int32_t
>(
sizeof(int16_t));
1004 throw error(
"Failed to support this format of custom image!");
1007 m_color_resolution.width,
1008 m_color_resolution.height,
1009 m_color_resolution.width * bytes_per_pixel);
1012 &transformed_depth_image,
1013 &transformed_custom_image,
1015 invalid_custom_value);
1016 return { std::move(transformed_depth_image), std::move(transformed_custom_image) };
1026 const image &color_image,
1027 image *transformed_color_image)
const
1029 k4a_result_t result = k4a_transformation_color_image_to_depth_camera(m_handle,
1032 transformed_color_image->
handle());
1035 throw error(
"Failed to convert color image to depth camera geometry!");
1048 m_depth_resolution.width,
1049 m_depth_resolution.height,
1050 m_depth_resolution.width * 4 *
1051 static_cast<int32_t
>(
sizeof(uint8_t)));
1053 return transformed_color_image;
1065 k4a_transformation_depth_image_to_point_cloud(m_handle, depth_image.
handle(), camera, xyz_image->
handle());
1068 throw error(
"Failed to transform depth image to point cloud!");
1083 depth_image.
get_width_pixels() * 3 *
static_cast<int32_t
>(
sizeof(int16_t)));
1095 resolution m_color_resolution;
1096 resolution m_depth_resolution;
1118 dev.m_handle =
nullptr;
1128 device &operator=(
const device &) =
delete;
1137 m_handle = dev.m_handle;
1138 dev.m_handle =
nullptr;
1145 explicit operator bool() const noexcept
1154 return m_handle !=
nullptr;
1173 if (m_handle !=
nullptr)
1175 k4a_device_close(m_handle);
1188 int32_t timeout_ms = internal::clamp_cast<int32_t>(timeout.count());
1189 k4a_wait_result_t result = k4a_device_get_capture(m_handle, &capture_handle, timeout_ms);
1192 throw error(
"Failed to get capture from device!");
1199 *cap =
capture(capture_handle);
1220 int32_t timeout_ms = internal::clamp_cast<int32_t>(timeout.count());
1221 k4a_wait_result_t result = k4a_device_get_imu_sample(m_handle, imu_sample, timeout_ms);
1224 throw error(
"Failed to get IMU sample from device!");
1251 k4a_result_t result = k4a_device_start_cameras(m_handle, configuration);
1254 throw error(
"Failed to start cameras!");
1264 k4a_device_stop_cameras(m_handle);
1277 throw error(
"Failed to start IMU!");
1287 k4a_device_stop_imu(m_handle);
1297 std::string serialnum;
1303 serialnum.resize(buffer);
1304 result = k4a_device_get_serialnum(m_handle, &serialnum[0], &buffer);
1310 serialnum.resize(buffer - 1);
1316 throw error(
"Failed to read device serial number!");
1329 k4a_result_t result = k4a_device_get_color_control(m_handle, command, mode, value);
1332 throw error(
"Failed to read color control!");
1343 k4a_result_t result = k4a_device_set_color_control(m_handle, command, mode, value);
1346 throw error(
"Failed to set color control!");
1364 result = k4a_device_get_raw_calibration(m_handle, &
calibration[0], &buffer);
1369 throw error(
"Failed to read raw device calibration!");
1383 k4a_result_t result = k4a_device_get_calibration(m_handle, depth_mode, color_resolution, &calib);
1387 throw error(
"Failed to read device calibration!");
1402 bool sync_in_jack_connected, sync_out_jack_connected;
1403 k4a_result_t result = k4a_device_get_sync_jack(m_handle, &sync_in_jack_connected, &sync_out_jack_connected);
1407 throw error(
"Failed to read sync jack status!");
1409 return sync_in_jack_connected;
1422 bool sync_in_jack_connected, sync_out_jack_connected;
1423 k4a_result_t result = k4a_device_get_sync_jack(m_handle, &sync_in_jack_connected, &sync_out_jack_connected);
1427 throw error(
"Failed to read sync jack status!");
1429 return sync_out_jack_connected;
1440 k4a_result_t result = k4a_device_get_version(m_handle, &version);
1444 throw error(
"Failed to read device firmware information!");
1461 throw error(
"Failed to open device!");
1472 return k4a_device_get_installed_count();
bool operator!=(std::nullptr_t) const noexcept
bool operator!=(const capture &other) const noexcept
image get_depth_image() const noexcept
void set_temperature_c(float temperature_c) noexcept
float get_temperature_c() const noexcept
capture & operator=(const capture &other) noexcept
k4a_capture_t handle() const noexcept
void set_depth_image(const image &depth_image) noexcept
bool operator==(std::nullptr_t) const noexcept
capture(const capture &other) noexcept
capture(k4a_capture_t handle=nullptr) noexcept
capture & operator=(capture &&other) noexcept
image get_color_image() const noexcept
capture(capture &&other) noexcept
image get_ir_image() const noexcept
bool operator==(const capture &other) const noexcept
bool is_valid() const noexcept
void set_ir_image(const image &ir_image) noexcept
void set_color_image(const image &color_image) noexcept
capture & operator=(std::nullptr_t) noexcept
static device open(uint32_t index)
device(k4a_device_t handle=nullptr) noexcept
bool get_capture(capture *cap)
device & operator=(device &&dev) noexcept
std::string get_serialnum() const
void get_color_control(k4a_color_control_command_t command, k4a_color_control_mode_t *mode, int32_t *value) const
void start_cameras(const k4a_device_configuration_t *configuration)
void stop_cameras() noexcept
k4a_hardware_version_t get_version() const
bool get_capture(capture *cap, std::chrono::milliseconds timeout)
bool get_imu_sample(k4a_imu_sample_t *imu_sample)
bool get_imu_sample(k4a_imu_sample_t *imu_sample, std::chrono::milliseconds timeout)
k4a_device_t handle() const noexcept
std::vector< uint8_t > get_raw_calibration() const
device(device &&dev) noexcept
static uint32_t get_installed_count() noexcept
calibration get_calibration(k4a_depth_mode_t depth_mode, k4a_color_resolution_t color_resolution) const
bool is_sync_in_connected() const
bool is_sync_out_connected() const
bool is_valid() const noexcept
void set_color_control(k4a_color_control_command_t command, k4a_color_control_mode_t mode, int32_t value)
std::chrono::microseconds get_exposure() const noexcept
uint8_t * get_buffer() noexcept
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)
std::chrono::microseconds get_device_timestamp() const noexcept
int get_width_pixels() const noexcept
const uint8_t * get_buffer() const noexcept
void set_white_balance(uint32_t white_balance) noexcept
int get_stride_bytes() const noexcept
uint32_t get_iso_speed() const noexcept
size_t get_size() const noexcept
image & operator=(std::nullptr_t) noexcept
bool operator!=(std::nullptr_t) const noexcept
static image create(k4a_image_format_t format, int width_pixels, int height_pixels, int stride_bytes)
int get_height_pixels() const noexcept
void set_timestamp(std::chrono::microseconds timestamp) noexcept
k4a_image_t handle() const noexcept
image & operator=(image &&other) noexcept
bool is_valid() const noexcept
uint32_t get_white_balance() const noexcept
bool operator!=(const image &other) const noexcept
std::chrono::nanoseconds get_system_timestamp() const noexcept
bool operator==(std::nullptr_t) const noexcept
k4a_image_format_t get_format() const noexcept
void set_iso_speed(uint32_t iso_speed) noexcept
image(k4a_image_t handle=nullptr) noexcept
image(image &&other) noexcept
image & operator=(const image &other) noexcept
void set_exposure_time(std::chrono::microseconds exposure) noexcept
bool operator==(const image &other) const noexcept
image(const image &other) noexcept
#define K4A_WAIT_INFINITE
k4a_transformation_interpolation_type_t
k4a_color_control_command_t
@ K4A_WAIT_RESULT_TIMEOUT
@ K4A_BUFFER_RESULT_TOO_SMALL
@ K4A_BUFFER_RESULT_SUCCEEDED
@ K4A_IMAGE_FORMAT_CUSTOM
@ K4A_IMAGE_FORMAT_CUSTOM8
@ K4A_IMAGE_FORMAT_DEPTH16
@ K4A_IMAGE_FORMAT_CUSTOM16
@ K4A_IMAGE_FORMAT_COLOR_BGRA32
K4A_EXPORT k4a_result_t k4a_calibration_2d_to_2d(const k4a_calibration_t *calibration, const k4a_float2_t *source_point2d, const float source_depth_mm, const k4a_calibration_type_t source_camera, const k4a_calibration_type_t target_camera, k4a_float2_t *target_point2d, int *valid)
K4A_EXPORT k4a_result_t k4a_calibration_3d_to_2d(const k4a_calibration_t *calibration, const k4a_float3_t *source_point3d_mm, const k4a_calibration_type_t source_camera, const k4a_calibration_type_t target_camera, k4a_float2_t *target_point2d, int *valid)
K4A_EXPORT k4a_result_t k4a_calibration_3d_to_3d(const k4a_calibration_t *calibration, const k4a_float3_t *source_point3d_mm, const k4a_calibration_type_t source_camera, const k4a_calibration_type_t target_camera, k4a_float3_t *target_point3d_mm)
K4A_EXPORT k4a_result_t k4a_calibration_color_2d_to_depth_2d(const k4a_calibration_t *calibration, const k4a_float2_t *source_point2d, const k4a_image_t depth_image, k4a_float2_t *target_point2d, int *valid)
K4A_EXPORT k4a_result_t k4a_calibration_2d_to_3d(const k4a_calibration_t *calibration, const k4a_float2_t *source_point2d, const float source_depth_mm, const k4a_calibration_type_t source_camera, const k4a_calibration_type_t target_camera, k4a_float3_t *target_point3d_mm, int *valid)
void() k4a_memory_destroy_cb_t(void *buffer, void *context)
k4a_calibration_camera_t color_camera_calibration
k4a_calibration_camera_t depth_camera_calibration
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)
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)
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
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
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
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)
bool convert_color_2d_to_depth_2d(const k4a_float2_t &source_point2d, const image &depth_image, k4a_float2_t *target_point2d) const
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