How to: Convert an OpenCV cv::Mat to an OpenGL texture

I’m working on using OpenCV to get Kinect sensor data via OpenNI, and needed a way to get a matrix (cv::Mat) into an OpenGL texture – so I wrote a function to do just that – woo! Apologies in advance for the terrible juggling ;-)

The function used to perform the sensor data to texture conversion is:

You can then use the above function like this:

There’s one very important issue to watch out for when using OpenCV and OpenNI together which I’ve commented in the code, but I’ll place here as well as it can be a real deal breaker:

There appears to be a threading issue with the OpenCV grab() function where if you try to grab the device before it’s ready to provide the next frame it takes up to 2 seconds to provide the frame, which it might do for a little while before crashing the XnSensorServer process & then you can’t get any more frames without restarting the application. This results in horrible, stuttery framerates and garbled sensor data.

I’ve found that this can be worked around by playing an mp3 in the background. No, really. I’m guessing the threading of the mp3 player introduces some kind of latency which prevents the grab() function being called too soon. Try it if you don’t believe me!

So just be aware that if you’re using a Kinect you have to be careful with the grab() function… The source code used to create the above video is provided in full after the jump, if you’re interested.

Cheers!

Continue reading How to: Convert an OpenCV cv::Mat to an OpenGL texture

Kinect Tech: Skeletal Mapping, Gestures and Accuracy

The Kinect has really fired people’s imaginations and there’s some great work happening right now – I can’t wait until I become a part of it =D

1.) The OpenNI library working with the Kinect to perform skeletal mapping:

2.) Using gestures and voice commands to navigate medical imagery:

3.) Apparently Microsoft are working on modifying the Kinect to quadruple its 3D sensing (structured light camera) accuracy from 320×200 to 640×480, at which point it will be able to detect fingers and other small features: ms-quadrupling-kinect-accuracy [eurogamer].