Ask Your Question

OpenCV + Asus Xtion sensor

asked 2014-05-17 00:44:39 -0500

chaanakya gravatar image

Dear all,

I am having trouble getting the Asus Xtion sensor to work with the OpenCV framework. I know (or at least I believe) the problem is not my laptop as I have an example program working with PCL (the Point Cloud Library). I am using Debian Sid with a custom compiled OpenCV (to ensure support for OpenNI and the Xtion sensor). The following code is what should work, but is not:

#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;

int main(int argc, char* argv[])
  VideoCapture capture( CAP_OPENNI_ASUS );
  if (!capture.isOpened())
  Mat depthMap;
  capture >> depthMap;

  if( waitKey( 30 ) >= 0 )

This code compiles just fine. When I run it, however, with the Xtion sensor plugged in, it gives me the following output:

CvCapture_OpenNI::CvCapture_OpenNI : Failed to run xml script: Bad Parameter sent to the device!

What exactly is going on and how do I fix it? I have tried searching on Google and haven't found anything useful except for other people with the same problem and no responses.

Thank you very much in advance.



edit retag flag offensive close merge delete


Does SamplesConfig.xml have anything at all to do with this not working? It seems that it should read /etc/openni/SamplesConfig.xml. However, I don't think it's doing that, since a) PCL worked (and works) just fine and b) nothing I do is actually changing the behavior of OpenCV.

chaanakya gravatar imagechaanakya ( 2014-05-18 12:11:25 -0500 )edit


I don't know the answer, but maybe I can give you a hint. (I never used OpenNI 1.5, it's quite outdated; I prefer to use the newer OpenNI2.2 and transform the captured data to OpenCV Mat).

To get back to your question: the OpenNI capture is implemented in the modules/highgui/src/cap_openni.cpp file. There you can find a global variable called XMLConfig, right at the beginning. The CvCapture_OpenNI constructor tries to run this script, but it seems that it fails; there is a parameter in the XML text that your device doesn't understand.

workarounds: a) use OpenNI separately and transform the image to Mat; b) try to get a valid XML config file for your device; c) set the device in verbose mode (check the settings in OpenNI), maybe it gives you more hint about that bad parameter.

kbarni gravatar imagekbarni ( 2014-05-19 08:55:49 -0500 )edit

Thanks kbarni - I did change that variable and the result does work. How exactly do you do a) though? I'd like to do that if possible. Thanks!

chaanakya gravatar imagechaanakya ( 2014-05-26 23:02:10 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-05-27 03:38:28 -0500

kbarni gravatar image

updated 2014-05-27 03:39:09 -0500

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;
    printf("Error! %d\n\n",rc);

First you initialize OpenNI and the cameras:

openni::Device device;
openni::VideoStream depth, color,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;

Then, we capture the image:

openni::VideoFrameRef frame;

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

openni::DepthPixel* dData = (openni::DepthPixel*)frame.getData();
cv::Mat 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!

edit flag offensive delete link more

Question Tools


Asked: 2014-05-17 00:44:39 -0500

Seen: 6,489 times

Last updated: May 27 '14