【问题标题】:Reading the Color Image from Kinect sensor into cv::Mat从 Kinect 传感器读取彩色图像到 cv::Mat
【发布时间】:2014-06-03 13:21:11
【问题描述】:

我有一个简单的问题:

我讨厌将 RGB 图片存储在数组中,并希望将其放入 cv:Mat。

我已经用 Kinect 传感器的深度图像完成了,如下所示:

VideoFrameRef lDepthFrame;
lStatus = gDepthVideoStream.readFrame(&lDepthFrame);

cv::Mat lDepthMat(lDepthFrame.getHeight(), lDepthFrame.getWidth(), CV_16U, (uint16_t*)lDepthFrame.getData());

我现在的问题是我不知道要为彩色图像使用哪种类型(上面我使用的是 CV_16U)。彩色图像为 RGB888/RGB24。所以它有 3 个字节大,每种颜色一个。

所以我所拥有的彩色图像是这样的:

VideoFrameRef lColorFrame;
lStatus = gRgbVideoStream.readFrame(&lColorFrame);
cv::Mat lRgbMat(lColorFrame.getHeight(), lColorFrame.getWidth(), <????>, (<????>*)lColorFrame.getData());

我需要在上面的代码中替换什么才能使这个功能起作用。

非常感谢您阅读并希望回答。如果这里有很多拼写错误,我真的很抱歉我的英语。我不是母语人士

【问题讨论】:

  • CV_8UC3 用于 RGB 图像(3 个 8 位通道)
  • 谢谢,效果很好。对于 lColorFrame 之前的类型转换,我使用 (uint16_t*) 就像上面一样。因此,如果有人感兴趣,结果如下所示:cv::Mat lRgbMat(lColorFrame.getHeight(), lColorFrame.getWidth(), CV_8UC3, (uint16_t*)lColorFrame.getData());
  • "for the typecast" - 在内部,它是一个 uchar* (但这并不重要,你如何将它投射到那里,一个 void* 会做同样的事情)

标签: opencv types kinect


【解决方案1】:

RGB888 在 opencv-speak 中将是 CV_8UC3,8 位 unsigend 值,具有 3 个通道。

如果 RGB 实际上是 BGR,则可能会出现问题 - 但您可以稍后交换通道。

【讨论】:

    【解决方案2】:

    你可以做这样简单的事情:

    IColorFrame* pColorFrame = nullptr;
    cv::Mat image;
    if (SUCCEEDED(m_pColorFrameReader->AcquireLatestFrame(&pColorFrame)) {
    
        // First you probably would like to get the image dimensions coming from your kinect device
        IFrameDescription* pFrameDescription = nullptr;
        int nWidth = 0;
        int nHeight = 0;
    
        pColorFrame->get_FrameDescription(&pFrameDescription);
        pFrameDescription->get_Width(&nWidth);
        pFrameDescription->get_Height(&nHeight);
    
        // now you just need to allocate your opencv matrix and copy the data from the IColorFrame object
        imageData.create(nHeight, nWidth , CV_8UC4);
        BYTE* imgDataPtr = (BYTE*)imageData.data;
        pColorFrame->CopyConvertedFrameDataToArray(nWidth * nHeight * 4, imgDataPtr, ColorImageFormat_Bgra);
    
        // release your pointer
        pFrameDescription->Release();
        pFrameDescription = nullptr;
    }
    
    pColorFrame->Release();
    pColorFrame = nullptr;
    

    然后你可以简单地使用opencv方法来显示图像;

    cv::imshow("Kinect Image", image);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2015-04-04
      • 2019-01-20
      • 2014-11-05
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多