从Kinect传感器读取彩色图像到cv::Mat



我有一个简单的问题:

我讨厌在数组中存储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());

我需要在上面的代码中替换什么来使这个函数工作

非常感谢你的阅读和回答。如果这里有很多拼写错误,我真的很抱歉我的英语。我的母语不是英语

RGB888将在openv -speak中表示CV_8UC3, 8位无符号值,有3个通道。

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

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

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);

最新更新