Depth Buffer Empty in C++
Describe the bug I am trying to set up the new Kinect DK for a project. When using the Kinect Viewer 1.2.0 I can see images of all three cameras (color, IR, Depth).
When trying to read the images in C++ and displaying them, I only get data for the color camera.
What I gathered so far is that the buffers for the depth, and ir image remain empty when calling k4a_image_get_buffer.
The log file seems to indicate that there is something wrong as shown with the Dropping depth capture as TS is too large TS messages.
To Reproduce I use the following code sample for testing, It displays the color image, but the depth image remains black.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <opencv2/core/core.hpp>
#include <k4a/k4a.h>
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <cstdint>
#include <iostream>
#include <chrono>
#include <thread>
#include <Windows.h>
using namespace cv;
using namespace std;
int main()
{
// 1.1: Start by counting the number of connected devices
uint32_t device_count = k4a_device_get_installed_count();
if (device_count == 0)
{
printf("No K4A devices found\n");
return 0;
}
else
{
printf("Found %d connected devices:\n", device_count);
}
// 1.2: Define the Exit block
int returnCode = 1;
// 1.3 Initialize the the device and capture attributes
k4a_device_t device = NULL;
k4a_capture_t capture = NULL;
const int32_t TIMEOUT_IN_MS = 1000;
// 1.4: Initialize the frame count
int totalFrame = 100;
int captureFrameCount = totalFrame;
printf("Capturing %d frames\n", captureFrameCount);
// 2: Set the configuration of device, you can also set it after open the device but before starting the camera
k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
config.color_format = K4A_IMAGE_FORMAT_COLOR_BGRA32; // <==== For Color image
config.color_resolution = K4A_COLOR_RESOLUTION_720P;
config.depth_mode = K4A_DEPTH_MODE_NFOV_UNBINNED; // <==== For Depth image
config.camera_fps = K4A_FRAMES_PER_SECOND_30;
config.synchronized_images_only = true;
// 3: Open the device
if (K4A_RESULT_SUCCEEDED != k4a_device_open(K4A_DEVICE_DEFAULT, &device))
{
printf("Failed to open device\n");
goto Exit;
}
// 4: Start the camera
if (K4A_RESULT_SUCCEEDED != k4a_device_start_cameras(device, &config))
{
printf("Failed to start device\n");
goto Exit;
}
// 5: Start to receive the captures and framess
while (captureFrameCount-- > 0)
{
// Get a depth frame
switch (k4a_device_get_capture(device, &capture, TIMEOUT_IN_MS))
{
case K4A_WAIT_RESULT_SUCCEEDED:
break;
case K4A_WAIT_RESULT_TIMEOUT:
printf("Timed out waiting for a capture\n");
continue;
break;
case K4A_WAIT_RESULT_FAILED:
printf("Failed to read a capture\n");
goto Exit;
}
printf("Capture %4d\n", captureFrameCount);
// 5.1 Probe for a color image
k4a_image_t image = k4a_capture_get_color_image(capture);
if (image)
{
// 5.2 Get the sizes of color image
int width = k4a_image_get_width_pixels(image);
int height = k4a_image_get_height_pixels(image);
int strides = k4a_image_get_stride_bytes(image);
// 5.3 Store the image using opencv Mat
uint8_t* color_image_data = k4a_image_get_buffer(image);
const cv::Mat color_image = cv::Mat(height, width, CV_8UC4, (void*)color_image_data, Mat::AUTO_STEP);
// 5.5 Display the images
namedWindow("foobar", WINDOW_AUTOSIZE);
imshow("foobar", color_image);
waitKey(1);
// 5.6 release the image
k4a_image_release(image);
}
else
{
printf(" | Color None ");
}
// 6.1 Probe for a depth16 image
k4a_image_t image_depth = k4a_capture_get_depth_image(capture);
//k4a_image_t image_depth = k4a_capture_get_ir_image(capture);
if (image != NULL)
{
// 6.2 Get the sizes of depth image
int width = k4a_image_get_width_pixels(image_depth);
int height = k4a_image_get_height_pixels(image_depth);
int strides = k4a_image_get_stride_bytes(image_depth);
// 6.3 Store the image using opencv Mat
uint8_t* depth_image_data = k4a_image_get_buffer(image_depth);
const cv::Mat depth_image = cv::Mat(height, width, CV_16U, (void*)depth_image_data, Mat::AUTO_STEP);
// 6.5 Display the images
namedWindow("depthbar", WINDOW_AUTOSIZE);
imshow("depthbar", depth_image);
waitKey(1);
// 6.6 release the image
k4a_image_release(image_depth);
}
else
{
printf(" | Depth16 None\n");
}
// release capture
k4a_capture_release(capture);
}
returnCode = 0;
Exit:
if (device != NULL)
{
k4a_device_stop_cameras(device);
k4a_device_close(device);
}
return returnCode;
}
Expected behavior It displays Logs
New logging session started
[2019-09-24 11:30:37.422] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\usbcommand\usbcommand.c (285): find_libusb_device(). Container ID found: {c14b54fe-74f0-4826-d680-e06f98e5425a}
[2019-09-24 11:30:37.423] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\usbcommand\usbcommand.c (185): populate_serialnumber(). Serial Number found 000434392112
[2019-09-24 11:30:37.475] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\usbcommand\usbcommand.c (285): find_libusb_device(). Container ID found: {c14b54fe-74f0-4826-d680-e06f98e5425a}
[2019-09-24 11:30:37.476] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\usbcommand\usbcommand.c (185): populate_serialnumber(). Serial Number found 000434392112
[2019-09-24 11:30:37.504] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (278): ******************** Device Info ********************
[2019-09-24 11:30:37.504] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (279): K4A SDK version: 1.2.0
[2019-09-24 11:30:37.529] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (285): Serial Number: 000434392112
[2019-09-24 11:30:37.529] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (289): RGB Sensor Version: 1.6.102
[2019-09-24 11:30:37.529] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (292): Depth Sensor Version:1.6.75
[2019-09-24 11:30:37.529] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (295): Mic Array Version: 1.6.14
[2019-09-24 11:30:37.529] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (298): Sensor Config: 6109.7
[2019-09-24 11:30:37.529] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (301): Build type: Release
[2019-09-24 11:30:37.529] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (306): Signature type: MSFT
[2019-09-24 11:30:37.529] [critical] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\depth\depth.c (312): ****************************************************
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (864): k4a_device_start_cameras(). Starting camera's with the following config.
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (865): k4a_device_start_cameras(). color_format:3
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (866): k4a_device_start_cameras(). color_resolution:1
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (867): k4a_device_start_cameras(). depth_mode:2
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (868): k4a_device_start_cameras(). camera_fps:2
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (869): k4a_device_start_cameras(). synchronized_images_only:1
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (870): k4a_device_start_cameras(). depth_delay_off_color_usec:0
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (871): k4a_device_start_cameras(). wired_sync_mode:0
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (872): k4a_device_start_cameras(). subordinate_delay_off_master_usec:0
[2019-09-24 11:30:37.649] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (873): k4a_device_start_cameras(). disable_streaming_indicator:0
[2019-09-24 11:30:37.668] [info] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\deloader\deloader.c (32): verify_plugin(). Loaded K4A Plugin with version: 2.0.0
[2019-09-24 11:30:37.916] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (266): capturesync_add_capture(). Dropping depth capture as TS is too large TS: 117417733
[2019-09-24 11:30:37.947] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (266): capturesync_add_capture(). Dropping depth capture as TS is too large TS: 117451066
[2019-09-24 11:30:37.964] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (912): k4a_device_start_cameras(). k4a_device_start_cameras started
[2019-09-24 11:30:37.977] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (266): capturesync_add_capture(). Dropping depth capture as TS is too large TS: 117484400
[2019-09-24 11:30:38.010] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (266): capturesync_add_capture(). Dropping depth capture as TS is too large TS: 117517733
[2019-09-24 11:30:38.044] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (266): capturesync_add_capture(). Dropping depth capture as TS is too large TS: 117551066
[2019-09-24 11:30:38.077] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (266): capturesync_add_capture(). Dropping depth capture as TS is too large TS: 117584400
[2019-09-24 11:30:38.110] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (266): capturesync_add_capture(). Dropping depth capture as TS is too large TS: 117617733
[2019-09-24 11:30:38.143] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (266): capturesync_add_capture(). Dropping depth capture as TS is too large TS: 117651066
[2019-09-24 11:30:38.177] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (266): capturesync_add_capture(). Dropping depth capture as TS is too large TS: 117684400
[2019-09-24 11:30:38.496] [info] [t=4864] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (87): drop_sample(). capturesync_drop, Dropping sample TS: 17733 type:Depth
[2019-09-24 11:30:38.497] [info] [t=4864] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (87): drop_sample(). capturesync_drop, Dropping sample TS: 51066 type:Depth
[2019-09-24 11:30:38.497] [info] [t=4864] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (87): drop_sample(). capturesync_drop, Dropping sample TS: 84400 type:Depth
[2019-09-24 11:30:39.949] [warning] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (170): queue_pop(). Queue_capture: Dropped oldest 36 captures from queue
[2019-09-24 11:30:40.024] [warning] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (170): queue_pop(). Queue_capture: Dropped oldest 2 captures from queue
[2019-09-24 11:30:40.305] [warning] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (170): queue_pop(). Queue_capture: Dropped oldest 1 captures from queue
[2019-09-24 11:30:40.446] [warning] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (170): queue_pop(). Queue_capture: Dropped oldest 1 captures from queue
[2019-09-24 11:30:40.525] [warning] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (170): queue_pop(). Queue_capture: Dropped oldest 1 captures from queue
[2019-09-24 11:30:40.548] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\dewrapper\dewrapper.c (278): depth_engine_thread(). Depth image processing is too slow at 41ms (this may be transient).
[2019-09-24 11:30:40.587] [warning] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (170): queue_pop(). Queue_capture: Dropped oldest 1 captures from queue
[2019-09-24 11:30:40.821] [warning] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (170): queue_pop(). Queue_capture: Dropped oldest 1 captures from queue
[2019-09-24 11:30:43.384] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (927): k4a_device_stop_cameras(). k4a_device_stop_cameras stopping
[2019-09-24 11:30:43.432] [warning] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (278): queue_disable(). Waiting for blocking call to complete
[2019-09-24 11:30:43.432] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (300): queue_error(). Error detected, shutting down queue and notifying consumers
[2019-09-24 11:30:43.432] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (300): queue_error(). Error detected, shutting down queue and notifying consumers
[2019-09-24 11:30:43.432] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\queue\queue.c (300): queue_error(). Error detected, shutting down queue and notifying consumers
[2019-09-24 11:30:43.432] [warning] [t=19148] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\capturesync\capturesync.c (204): capturesync_add_capture(). Capture Error Detected, Depth
[2019-09-24 11:30:43.914] [info] [t=11916] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\sdk\k4a.c (948): k4a_device_stop_cameras(). k4a_device_stop_cameras stopped
Desktop (please complete the following information):
- OS with Version: Windows 10 1903
- SDK Version: 1.2.0
You are using synchronized_images_only = TRUE so you are getting valid depth frames, they just don't contain the data you expect. If you run k4aviewer (installed with the MSI) do you get the same result?
Hi lunactic, I have same issues. I think we should use Microsoft's image conversion method.
I am not sure why you have the error in the log, may need we-b to help on that. But I just took your sample code and it runs fine on my machine. By the way, the depth image is not empty in my case, it is just dark, because the cv::mat is 16bit, the depth value is in mm, which means, the image intensity of the depth image will not be "bright" when you render the 16bit grayscale.
Sorry for the late response we were trying more things. As reported, this is indeed not a bug, but was simply due to us misunderstanding how the values in the depth buffer look like.
Thanks for your replies.