我需要从Kinnect相机捕获彩色RGB图像,但我想在OpenCV中显示它,因为这只是更大程序的一部分。我知道如果你设置了标志,OpenCV与OpenNI兼容,但是虽然我努力了但是CMake找不到OpenNI2的路径所以我无法使用OpenNI构建OpenCV。无论如何,我认为知道如何手动将OpenNI帧转换为openCV帧是很好的,所以我决定采用这种方式。
为了在OpenNI中捕获颜色框架,我尝试了以下方法:
openni::Device device;
openni::VideoStream color;
openni::VideoFrameRef colorFrame;
rc = openni::OpenNI::initialize();
rc = device.open(openni::ANY_DEVICE);
rc = color.create(device, openni::SENSOR_COLOR);
rc = color.start();
color.readFrame(&colorFrame);
const openni::RGB888Pixel* imageBuffer = (const openni::RGB888Pixel*)colorFrame.getData();
但现在我不明白如何转换为cv :: Mat。
有没有人设法做到这一点?
好的,首先你应该通过这种方式将初始化与循环分开来读取帧。
初始化
openni::Device device;
openni::VideoStream color;
openni::VideoFrameRef colorFrame;
rc = openni::OpenNI::initialize();
rc = device.open(openni::ANY_DEVICE);
rc = color.create(device, openni::SENSOR_COLOR);
rc = color.start();
Mat frame;
现在是阅读框架的主循环。你已经做了几乎所有事情,唯一剩下的就是将缓冲区复制到openCV Mat。
循环阅读框架
while (true)
{
color.readFrame(&colorFrame);
const openni::RGB888Pixel* imageBuffer = (const openni::RGB888Pixel*)colorFrame.getData();
frame.create(colorFrame.getHeight(), colorFrame.getWidth(), CV_8UC3);
memcpy( frame.data, imageBuffer, 3*colorFrame.getHeight()*colorFrame.getWidth()*sizeof(uint8_t) );
cv::cvtColor(frame,frame,CV_BGR2RGB); //this will put colors right
}
关注@ Jav_Rock上面的答案,对于IR和深度,解决方案类似,除了以下(分别代替SENSOR_COLOR
,CV_8UC3
和RGB888Pixel
):
深度
SENSOR_DEPTH
CV_16UC1
DepthPixel
请注意,您可能希望将VideoStream的VideoFormat的PixelMode设置为PIXEL_FORMAT_DEPTH_100_UM
,否则您的深度图像将看起来非常黑。
与
SENSOR_IR
CV_16UC1
Grayscale16Pixel
最后,请注意深度和IR都不需要cv::cvtColor
调用。