使用Openni从Kinnect捕获RGB并使用OpenCV进行显示 [英] Capture RGB from Kinnect with Openni and show with OpenCV

查看:180
本文介绍了使用Openni从Kinnect捕获RGB并使用OpenCV进行显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Kinnect相机捕获彩色RGB图像,但是我想在OpenCV中显示它,因为这只是更大程序的一部分.我知道如果设置了标志,OpenCV将与OpenNI兼容,但是尽管我很努力CMake找不到OpenNI2的路径,所以我无法使用OpenNI构建OpenCV.无论如何,我认为知道如何将OpenNI帧手动转换为openCV帧是一件好事,所以我决定采用这种方式.

I need to capture the color RGB image from a Kinnect camera, but I want to show it in OpenCV as this is only a part of a bigger program. I know OpenCV has compatibility with OpenNI if you set the flag, but although I tried hard CMake couldn't find the path to OpenNI2 so I couldn't build OpenCV with OpenNI. Anyway I think it is good to know how to manually convert OpenNI frames to openCV frames, so I decided to follow this way.

为捕获OpenNI中的彩色框,我尝试了以下操作:

For capturing the color frame in OpenNI I tried the following:

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.

But now I don't understand how to do the conversion to cv::Mat.

有人强迫这样做吗?

推荐答案

好吧,首先,您应该以这种方式将初始化与循环分开以读取帧.

Ok, first you should separate initialization from the loop for reading frames, this way.

初始化

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.

Now comes the main loop for reading frames. You have done almost everything, the only thing left is to copy the buffer to the 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
}

这篇关于使用Openni从Kinnect捕获RGB并使用OpenCV进行显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆