Can't display image from kinect dk using OpenCV - blank image

588 Views Asked by At

My problem look to be simple. I'm trying to get image from Azure Kinect DK using SDK for C++ and then display it using OpenCV. Code is working, but in results of it I can see only gray image. So what is missing here?

    k4a_device_configuration_t config = K4A_DEVICE_CONFIG_INIT_DISABLE_ALL;
    config.camera_fps = K4A_FRAMES_PER_SECOND_30;
    config.color_format = K4A_IMAGE_FORMAT_COLOR_BGRA32; // <==== For Color image
    config.color_resolution = K4A_COLOR_RESOLUTION_2160P;
    config.depth_mode = K4A_DEPTH_MODE_NFOV_UNBINNED; // <==== For Depth image
k4a_image_t colorImage = k4a_capture_get_color_image(capture); // get image metadata
        if (colorImage != NULL)
        {
            printf("Capture");

            // you can check the format with this function
            k4a_image_format_t format = k4a_image_get_format(colorImage); // K4A_IMAGE_FORMAT_COLOR_BGRA32 

            // get raw buffer
            uint8_t* buffer = k4a_image_get_buffer(colorImage);

            // convert the raw buffer to cv::Mat
            int rows = k4a_image_get_height_pixels(colorImage);
            int cols = k4a_image_get_width_pixels(colorImage);
            Mat image2(rows, cols, CV_8UC4, (void*)buffer, cv::Mat::AUTO_STEP);
            // ...
            cv::imshow("test", image2);
            k4a_image_release(colorImage);


        }
2

There are 2 best solutions below

0
Rivindu Madushan On

Try converting the image to RGB before showing.

Mat cimg;
cvtColor(image2, cimg, CV_GRAY2RGB);

Then show the new image

cv::imshow("test", cimg);
0
C.M. On

If you see a gray image only it's probably because it doesn't have the time to show. You can add a waitKey after showing the image :

cv::imshow("test", image2);
int keyboard = waitKey(30);

You can use the variable keyboard to quit the program/function for example. The argument of waitKey indicates the amount of time to wait.