使用身体索引图将天蓝色Kinect裁剪出身体

问题描述 投票:0回答:1

我正在使用Azure DK读取MKV文件,在其中我可以为每帧提取RGBA和相应的深度图像。之后,我使用人体追踪器使用深度图像来定位和追踪人。目前,我可以生成一个body index map,甚至可以将其转换为彩色相机空间,如下所示。

k4a::image extract_body_index_map(tracker& object_tracker)
{
    k4a::image body_index_map = k4a::image();

    k4abt::frame body_frame = object_tracker.pop_result();

    if (body_frame != nullptr)
    {
        uint32_t bodies = body_frame.get_num_bodies();

        cout << "Bodies Found: " << bodies << "\n";

        for (uint32_t index = 0; index < bodies; ++index)
        {
            k4abt_body_t body = body_frame.get_body(index);

            cout << "\tPERSON: " << body.id << "\n";
            body_index_map = body_frame.get_body_index_map();
        }
    }
    else { cerr << "ERROR: Body Pop Out Timeout\n\n"; }

    return body_index_map;
}

k4a::image convert_body_index_map_to_colour(k4a::image& body_index_map, k4a::image& depth, k4a::image& colour, k4a_transformation_t transformation)
{
    k4a_image_t depth_image_in_colour_space = nullptr;

    k4a_result_t result = k4a_image_create(K4A_IMAGE_FORMAT_DEPTH16, 
        colour.get_width_pixels(), colour.get_height_pixels(), 
        colour.get_width_pixels() * (int)sizeof(uint16_t), &depth_image_in_colour_space);

    if (result == K4A_RESULT_FAILED)
    {
        cerr << "ERROR: Failed to create depth image in colour space\n";
        return nullptr;
    }

    k4a_image_t body_index_in_colour_space = nullptr;

    result = k4a_image_create(K4A_IMAGE_FORMAT_CUSTOM8,
        colour.get_width_pixels(), colour.get_height_pixels(),
        colour.get_width_pixels() * (int)sizeof(uint8_t), &body_index_in_colour_space);

    if (result == K4A_RESULT_FAILED)
    {
        cerr << "ERROR: Failed to create body index map in colour space\n";
        return nullptr;
    }


    result = k4a_transformation_depth_image_to_color_camera_custom(transformation, depth.handle(),
        body_index_map.handle(), depth_image_in_colour_space, body_index_in_colour_space,
        K4A_TRANSFORMATION_INTERPOLATION_TYPE_NEAREST, K4ABT_BODY_INDEX_MAP_BACKGROUND);

    if (result == K4A_RESULT_FAILED)
    {
        cerr << "ERROR: Failed to transform body index map to colour space\n";
        return nullptr;
    }

    return k4a::image(body_index_in_colour_space);
}

我目前在如何操作上仍然受困:

  1. 使用生成的身体索引图裁剪/细分深度图像,以提取身体的视觉表示。我非常想复制this具有RGBA图像的部分

  2. 将裁剪后的图像转换为2D RGBA图像

任何帮助将不胜感激:)

c++ azure kinect-sdk azurekinect
1个回答
0
投票

弄乱后,我终于设法解决了这个问题。 我切换到OpenCV进行图像处理。

首先,我将body_index_in_colour_space转换为OpenCV Mat

Mat convertToMat(k4a::image kinect_image)
{
    uint8_t* pixel_data = kinect_image.get_buffer();

    if (pixel_data != nullptr) return Mat(kinect_image.get_height_pixels(), kinect_image.get_width_pixels(), CV_8UC4, (void*)pixel_data, Mat::AUTO_STEP);

    return Mat();
}

我也通过类似的方式将标志从CV_8UC1更改为CV_8UC4,将kinect BGRA图像转换为OpenCV Mat。之后,我只是在彩色图像的顶部绘制了索引图。

Mat drawIndexMapOnColourImage(Mat indexMap, Mat colour)
{
    Mat result;
    colour.copyTo(result);

    //TODO: Not efficient, fix this
    for(size_t row = 0; row < indexMap.rows; ++row)
        for (size_t col = 0; col < indexMap.cols; ++col)
        {
            if ((int)indexMap.at<uchar>(row, col) == 255) continue; // ignore background

            result.at<Vec3b>(row, col) = 150;
        }

    return result;
}
© www.soinside.com 2019 - 2024. All rights reserved.