How to get RGB and Depth image from Xtion Pro Live?

asked 2014-02-09 23:42:12 -0600

MonsterPC gravatar image

updated 2014-02-10 00:02:08 -0600

I have to use OpenCV and OpenNI.

there is no problem when Depth image is executed

but, As I try to use both the RGB and Depth to the problem.

Below is my code

#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <XnCppWrapper.h>

using namespace xn;

int main()
{
 XnStatus rc = XN_STATUS_OK;
 Context g_context;
  rc = g_context.Init();
 DepthGenerator g_depth;
 rc = g_depth.Create(g_context);
 DepthMetaData g_depthMD;
 const XnDepthPixel* ppDepth;
 xn::ImageGenerator image_gen;

 //Set it to VGA maps at 30 FPS
  XnMapOutputMode mapMode;
 image_gen.GetMapOutputMode(mapMode);

 mapMode.nXRes = XN_VGA_X_RES;
 mapMode.nYRes = XN_VGA_Y_RES;
 mapMode.nFPS = 30;

 rc = g_depth.SetMapOutputMode(mapMode);
 IplImage* tempimgrab = cvCreateImage(cvSize(XN_VGA_X_RES, XN_VGA_Y_RES),8,3);
  IplImage* rgb_img = cvCreateImage(cvSize(XN_VGA_X_RES, XN_VGA_Y_RES), IPL_DEPTH_8U,3);

 rc = g_context.StartGeneratingAll();
 cvNamedWindow("Depth",1);
 cvNamedWindow("RGB",2);

 while(1)
  {
  if(cvWaitKey(1) >= 0)
   break;

  rc = g_context.WaitAnyUpdateAll();
  xn::ImageMetaData imageMetaData;
  image_gen.GetMetaData(imageMetaData);
  xnOSMemCopy(rgb_img->imageData, imageMetaData.RGB24Data(), rgb_img->imageSize);
   cvCvtColor(rgb_img, rgb_img, CV_RGB2BGR);

  g_depth.GetMetaData(g_depthMD);
  ppDepth = g_depthMD.Data();

  int tempmax = 0, tempmin = 100000;

  for(int xx=0; xx < XN_VGA_X_RES*XN_VGA_Y_RES; xx++)
   {
   if(ppDepth[xx] > tempmax) tempmax = ppDepth[xx];
   if(ppDepth[xx] < tempmin) tempmin = ppDepth[xx];
  }

  for(int xx=0; xx < XN_VGA_X_RES*XN_VGA_Y_RES; xx++)
  {
   tempimgrab->imageData[xx*3+1] = 255-(unsigned char)((float)ppDepth[xx]/tempmax*255);
          tempimgrab->imageData[xx*3+2] = 255-(unsigned char)((float)ppDepth[xx]/tempmax*255);

   if(ppDepth[xx] == 0) 
   {
    tempimgrab->imageData[xx*3+1] = 0;
    tempimgrab->imageData[xx*3+2] = 0;
    }
  }
  cvShowImage("RGB image", rgb_img);
  cvShowImage("Depth", tempimgrab);
 }

 g_context.Shutdown();
 cvReleaseImage(&tempimgrab);
 cvDestroyWindow("Depth");
  if(rgb_img != NULL)
    cvReleaseImage(&rgb_img);

 return 0;
}

please, I need your help.

edit retag flag offensive close merge delete