将 cv::Mat 转换为 openni::VideoFrameRef

Convert cv::Mat to openni::VideoFrameRef

我有一个 kinect 流数据到 cv::Mat。我正在尝试获取一些使用 OpenNI 的示例代码 运行。

我可以通过某种方式将我的 Mat 转换为 OpenNI 格式的图像吗?

正好需要深度图,和OpenNI折腾了很久,放弃安装了。

我正在使用 OpenCV 3,Visual Studio 2013,Kinect v2 for Windows。

相关代码为:

void CDifodoCamera::loadFrame()
{
    //Read the newest frame
    openni::VideoFrameRef framed; //I assume I need to replace this with my Mat...
    depth_ch.readFrame(&framed);

    const int height = framed.getHeight();
    const int width = framed.getWidth();

    //Store the depth values
    const openni::DepthPixel* pDepthRow = (const openni::DepthPixel*)framed.getData();
    int rowSize = framed.getStrideInBytes() / sizeof(openni::DepthPixel);

    for (int yc = height-1; yc >= 0; --yc)
    {
        const openni::DepthPixel* pDepth = pDepthRow;
        for (int xc = width-1; xc >= 0; --xc, ++pDepth)
        {
            if (*pDepth < 4500.f)
                depth_wf(yc,xc) = 0.001f*(*pDepth);

            else
                depth_wf(yc,xc) = 0.f;
        }

        pDepthRow += rowSize;
    }
}

首先你需要了解你的数据是如何来的...如果它已经在 cv::Mat 你应该收到两张图片,一张用于 RGB 信息,通常是 3 通道 uchar cv::Mat 和另一张用于深度信息的图像,通常它以 16 位表示形式保存(以毫米为单位)(您不能将浮动垫保存为图像,但可以使用 opencv 保存为 yml/xml 文件)。

假设你想读取和处理包含深度信息的图像,你可以将代码更改为:

void CDifodoCamera::loadFrame()
{
    //Read the newest frame
    //the depth image should be png since it is the one which supports 16 bits and it must have the ANYDEPTH flag
    cv::Mat depth_im = cv::imread("img_name.png",CV_LOAD_IMAGE_ANYDEPTH); 

    const int height = depth_im.rows;
    const int width = depth_im.cols;

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (depth_im<unsigned short>(y,x) < 4500)
                depth_wf(y,x) = 0.001f * (float)depth_im<unsigned short>(y,x);

            else
                depth_wf(y,x) = 0.f;
        }
    }
}

希望对您有所帮助。如果您有任何问题,请直接提问 :)