好的,首先你应该通过这种方式将初始化与循环分开来读取帧.
初始化
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
}