Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is how to use OpenNI2.2 with OpenCV. The code is untested, but it should work.

To be shorter, I omitted the verifications from the code, but you should really check the result of every operation like this:

openni::Status rc;
rc=anyOpenNIOpreation();
if(rc!=openni::STATUS_OK){
    printf("Error! %d\n\n",rc);
    openni::OpenNI::shutdown();
    exit(0);
}

First you initialize OpenNI and the cameras:

openni::Device device;
openni::VideoStream depth, color,ir;

openni::OpenNI::initialize();
device.open(openni::ANY_DEVICE);

ir.create(device,openni::SENSOR_IR);
depth.create(device, openni::SENSOR_DEPTH);
color.create(device, openni::SENSOR_COLOR);

In this example I'll use the depth stream for capture. First we need the resolution:

openni::VideoMode vm=depth.getVideoMode();
int cols,rows;
cols=vm.getResolutionX();
rows=vm.getResolutionY();

Then, we capture the image:

openni::VideoFrameRef frame;
depth.start();
stream->readFrame(&frame);   
depth.stop();

Finally, you get the frame data and create a Mat variable using that data:

openni::DepthPixel* dData = (openni::DepthPixel*)frame.getData();
cv::Mat depthImage(cols, rows, CV_16UC1, dData);

For the color frame you'll have to use openni::RGB888Pixel as pixel type and CV_8UC3 as matrix type.

Hope this helps!

openni::OpenNI::shutdown();

Here is how to use OpenNI2.2 with OpenCV. The code is untested, but it should work.

To be shorter, I omitted the verifications from the code, but you should really check the result of every operation like this:

openni::Status rc;
rc=anyOpenNIOpreation();
if(rc!=openni::STATUS_OK){
    printf("Error! %d\n\n",rc);
    openni::OpenNI::shutdown();
    exit(0);
}

First you initialize OpenNI and the cameras:

openni::Device device;
openni::VideoStream depth, color,ir;

openni::OpenNI::initialize();
device.open(openni::ANY_DEVICE);

ir.create(device,openni::SENSOR_IR);
depth.create(device, openni::SENSOR_DEPTH);
color.create(device, openni::SENSOR_COLOR);

In this example I'll use the depth stream for capture. First we need the resolution:

openni::VideoMode vm=depth.getVideoMode();
int cols,rows;
cols=vm.getResolutionX();
rows=vm.getResolutionY();

Then, we capture the image:

openni::VideoFrameRef frame;
depth.start();
stream->readFrame(&frame);   
depth.stop();

Finally, you get the frame data and create a Mat variable using that data:

openni::DepthPixel* dData = (openni::DepthPixel*)frame.getData();
cv::Mat depthImage(cols, rows, depthImage(rows, cols, CV_16UC1, dData);

For the color frame you'll have to use openni::RGB888Pixel as pixel type and CV_8UC3 as matrix type.

Hope this helps!

openni::OpenNI::shutdown();