how to use Kinect with openni and opencv

user3441653 picture user3441653 · Mar 20, 2014 · Viewed 18k times · Source

for the beginning, I just need to capture the RGBstream and convert it in a sequence of opencv image. it shouldn`t be so hard, but I found more that one codes online, but they don't run in my computer. I don't know where is the mistake.

could you suggest me a tutorial o a really simple code that allow me to understand how to use Kinect libraries? in the beginning I tried Kinect sdk, after a while a choose OPENNI.

help me, thx!

ps: I'm using c++ AND VISUAL STUDIO 2010

Answer

George Profenza picture George Profenza · Mar 20, 2014

AFAIK out of the box OpenCV supports OpenNI 1.5.x. If you haven't installed OpenNI, do so first, in this particular order(which is important):

  1. Install OpenNI 1.5.7
  2. Install NITE(compatible for 1.5.7)
  3. If you're using a Kinect (and not an Asus) sensor, also install Avin's SensorKinect driver

At this point you should have OpenNI installed, so go ahead and run one of the samples.

The prebuilt opencv library isn't compiled with OpenCV support by default so you will need to build opencv from source to enable OpenNI support.

Install CMakeGUI if you haven't done so already. This will allow you to easily configure the opencv built process. Run it, browse to the opencv source folder, pick a destination directory to place your build files and hit configure.

You should have a large list of options. If you scroll, your should see the OpenNI install folder detected (if not you should fix the path) and also you should and WITH_OPENNI flag you can enable.

When you're done press generate, wich should generate the visual studio project files you need to easily compile the opencv library.

For more details on building opencv from source on windows also check out the official documentation

When you're done compiling you should have opencv built with openni support and you should be able run something simple as:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

int main(){
    cout << "opening device(s)" << endl;

    VideoCapture sensor1;
    sensor1.open(CV_CAP_OPENNI);

    if( !sensor1.isOpened() ){
        cout << "Can not open capture object 1." << endl;
        return -1;
    }

    for(;;){
        Mat depth1;

        if( !sensor1.grab() ){
            cout << "Sensor1 can not grab images." << endl;
            return -1;
        }else if( sensor1.retrieve( depth1, CV_CAP_OPENNI_DEPTH_MAP ) ) imshow("depth1",depth1);

        if( waitKey( 30 ) == 27 )   break;//ESC to exit

   }
}

Also see this similar answer. If you need to use OpenNI 2.x see these resources: