【问题标题】:Convert cv::Mat to openni::VideoFrameRef将 cv::Mat 转换为 openni::VideoFrameRef
【发布时间】:2015-06-10 22:18:42
【问题描述】:

我有一个 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;
    }
}

【问题讨论】:

  • 为什么需要openni?我的意思是,这个示例代码可以重写为直接使用 opencv 图像而不用 openni....
  • 更好!我只是不确定如何获得一个垫子。我怎样才能重写它以使其工作?感谢您的宝贵时间。

标签: c++ opencv kinect openni


【解决方案1】:

首先,您需要了解您的数据是如何来的...如果它已经在 cv::Mat 中,您应该收到两张图像,一张用于 RGB 信息,通常是 3 通道 uchar cv::Mat,另一张用于通常它以 16 位表示形式保存的深度信息,以毫米为单位(您不能将 float mat 保存为图像,但您可以使用 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;
        }
    }
}

我希望这对你有帮助。如果您有任何问题,尽管问:)

【讨论】:

  • 您好,谢谢! depth_wf 定义为: /** 以图像分辨率存储原始深度帧的矩阵 */ Eigen::MatrixXf depth_wf; > 我在“无符号”和“浮点”上的代码出现错误,“类型名称不允许”。再次感谢
  • 如果是Eigen::Matrix,那么它没有类型(查看我编辑的帖子)。另一个(depth_im)必须是类型,因为它是一个opencv Mat
猜你喜欢
  • 1970-01-01
  • 2012-12-29
  • 2014-05-07
  • 2011-06-07
  • 2016-04-13
  • 2011-10-23
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
相关资源
最近更新 更多