使用Openni从Kinnect捕获RGB并使用OpenCV进行显示

问题描述 投票:8回答:2

我需要从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。

有没有人设法做到这一点?

opencv kinect rgb simple-openni
2个回答
12
投票

好的,首先你应该通过这种方式将初始化与循环分开来读取帧。

初始化

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
}

1
投票

关注@ Jav_Rock上面的答案,对于IR和深度,解决方案类似,除了以下(分别代替SENSOR_COLORCV_8UC3RGB888Pixel):

深度

  • 传感器类型:SENSOR_DEPTH
  • OpenCV类型:CV_16UC1
  • VideoFrameRef数据:DepthPixel

请注意,您可能希望将VideoStream的VideoFormat的PixelMode设置为PIXEL_FORMAT_DEPTH_100_UM,否则您的深度图像将看起来非常黑。

  • 传感器类型:SENSOR_IR
  • OpenCV类型:CV_16UC1
  • VideoFrameRef数据:Grayscale16Pixel

最后,请注意深度和IR都不需要cv::cvtColor调用。

© www.soinside.com 2019 - 2024. All rights reserved.