Advertisement
Guest User

Untitled

a guest
Dec 12th, 2010
3,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include "XnCppWrapper.h"
  2. #include "opencv/cv.h"
  3. #include "opencv/highgui.h"
  4. #include "stdio.h"
  5.  
  6. #define MAX_DEPTH 10000
  7.  
  8. using namespace xn;
  9.  
  10. //Global Variables
  11. xn::Context context;
  12. DepthGenerator Xn_depth;
  13. ImageGenerator Xn_image;
  14. XnStatus nRetVal = XN_STATUS_OK;
  15.  
  16.  
  17. unsigned short depth[MAX_DEPTH];
  18. char *depth_data;
  19.  
  20. void raw2depth(){
  21.     int i;
  22.     for ( i=0; i<MAX_DEPTH; i++) {
  23.         //float dist =100/(-0.0030711016*i + 3.3309495161); //ros distance (cm)
  24.         //float dist2 = 12.36 * tanf(i/ 2842.5 + 1.1863);//Stéphane Magnenat distance (cm)
  25.         float v = (float)i/MAX_DEPTH;//for visualization purposes only
  26.         v = powf(v, 2);
  27.         v = v*36*256;
  28.         depth[i] = v;
  29.         //printf("%f    ",dist2);
  30.        
  31.         /*
  32.          * XnUInt64 F_;
  33.          * XnDouble pixel_size_;
  34.         // get the focal length in mm (ZPS = zero plane distance)
  35.             depth_.GetIntProperty ("ZPD", F_)
  36.         // get the pixel size in mm ("ZPPS" = pixel size at zero plane)
  37.             depth_.GetRealProperty ("ZPPS", pixel_size_)
  38.             X = (u - 320) * depth_md_[k] * pixel_size_ * 0.001 / F_;
  39.             Y = (v - 240) * depth_md_[k] * pixel_size_ * 0.001 / F_;
  40.             Z = depth_md_[k] * 0.001; // from mm in meters!
  41.          */
  42.     }
  43. }
  44.  
  45. void depth2rgb(const XnDepthPixel* Xn_disparity){
  46.     int i;
  47.     //const unsigned short *disparity = Xn_disparity;
  48.    
  49.     for (i=0; i<307200; i++) {
  50.         int pval = depth[Xn_disparity[i]];
  51.         //printf("%d: %u %d \n",i,*Xn_disparity,depth[*Xn_disparity]);
  52.         //fflush(stdout);
  53.         int lb = pval & 0xff;
  54.         switch (pval>>8) {
  55.             case 0:
  56.                 depth_data[3*i+0] = 255;
  57.                 depth_data[3*i+1] = 255-lb;
  58.                 depth_data[3*i+2] = 255-lb;
  59.                 break;
  60.             case 1:
  61.                 depth_data[3*i+0] = 255;
  62.                 depth_data[3*i+1] = lb;
  63.                 depth_data[3*i+2] = 0;
  64.                 break;
  65.             case 2:
  66.                 depth_data[3*i+0] = 255-lb;
  67.                 depth_data[3*i+1] = 255;
  68.                 depth_data[3*i+2] = 0;
  69.                 break;
  70.             case 3:
  71.                 depth_data[3*i+0] = 0;
  72.                 depth_data[3*i+1] = 255;
  73.                 depth_data[3*i+2] = lb;
  74.                 break;
  75.             case 4:
  76.                 depth_data[3*i+0] = 0;
  77.                 depth_data[3*i+1] = 255-lb;
  78.                 depth_data[3*i+2] = 255;
  79.                 break;
  80.             case 5:
  81.                 depth_data[3*i+0] = 0;
  82.                 depth_data[3*i+1] = 0;
  83.                 depth_data[3*i+2] = 255-lb;
  84.                 break;
  85.             default:
  86.                 depth_data[3*i+0] = 0;
  87.                 depth_data[3*i+1] = 0;
  88.                 depth_data[3*i+2] = 0;
  89.                 break;
  90.         }
  91.     }
  92. }
  93.  
  94. void rgbd_init(){
  95. /// Initialize context object
  96. nRetVal = context.Init();
  97. if (nRetVal != XN_STATUS_OK){
  98.         printf("Failed to initialize context: %s\n", xnGetStatusString(nRetVal));
  99.     }
  100.  
  101. /// Create a DepthGenerator node
  102. nRetVal = Xn_depth.Create(context);
  103. if (nRetVal != XN_STATUS_OK){
  104.         printf("Failed to create depth generator: %s\n", xnGetStatusString(nRetVal));
  105.     }
  106.  
  107. /// Create a ImageGenerator node
  108. nRetVal = Xn_image.Create(context);
  109. if (nRetVal != XN_STATUS_OK){
  110.         printf("Failed to create image generator: %s\n", xnGetStatusString(nRetVal));
  111.     }
  112.    
  113. /// Make it start generating data
  114. nRetVal = context.StartGeneratingAll();
  115. if (nRetVal != XN_STATUS_OK){
  116.         printf("Failed generating data: %s\n", xnGetStatusString(nRetVal));
  117.     }
  118.    
  119. ///Sync the DepthGenerator with the ImageGenerator
  120. //~ nRetVal = Xn_depth.GetFrameSyncCap().FrameSyncWith(Xn_image);
  121. //~ if (nRetVal != XN_STATUS_OK){
  122.         //~ printf("Failed to sync Depth and RGB streams: %s\n", xnGetStatusString(nRetVal));
  123.     //~ }
  124. /// Set the view point of the DepthGenerator to match the ImageGenerator
  125. //~ nRetVal = Xn_depth.GetAlternativeViewPointCap().SetViewPoint(Xn_image);
  126. //~ if (nRetVal != XN_STATUS_OK){
  127.         //~ printf("Failed to match Depth and RGB points of view: %s\n", xnGetStatusString(nRetVal));
  128.     //~ }
  129. }
  130.  
  131. int main(int argc, char **argv) {
  132.  
  133. //RGB-D Sensor Initialization
  134. rgbd_init();
  135.  
  136. //OpenCV stuff
  137. cvNamedWindow("RGB", CV_WINDOW_AUTOSIZE);
  138. cvNamedWindow("Depth", CV_WINDOW_AUTOSIZE);
  139. IplImage *rgbimg = cvCreateImageHeader(cvSize(640,480), 8, 3);
  140. IplImage *depthimg = cvCreateImageHeader(cvSize(640,480), 8, 3);   
  141.  
  142. raw2depth();
  143. depth_data = (char*)malloc(640*480*3);
  144.  
  145. while (true) {
  146.     // Wait for new data to be available
  147.     nRetVal = context.WaitAndUpdateAll();
  148.     if (nRetVal != XN_STATUS_OK)
  149.     {
  150.         printf("Failed updating data: %s\n", xnGetStatusString(nRetVal));
  151.         continue;
  152.     }
  153.  
  154.     // Take current depth map
  155.     const XnDepthPixel* pDepthMap = Xn_depth.GetDepthMap();
  156.     // Take current image
  157.     const XnRGB24Pixel* pImage = Xn_image.GetRGB24ImageMap();
  158.    
  159.     //process depth map
  160.     depth2rgb(pDepthMap);
  161.     cvSetData(depthimg,depth_data, 640*3);
  162.     cvShowImage("Depth",depthimg);
  163.    
  164.     //process image data
  165.     XnRGB24Pixel* ucpImage = const_cast<XnRGB24Pixel*> (pImage);
  166.     cvSetData(rgbimg,ucpImage, 640*3);
  167.     cvCvtColor(rgbimg,rgbimg,CV_RGB2BGR);
  168.     cvShowImage("RGB",rgbimg);
  169.    
  170.     char key =cvWaitKey(30);
  171.     if (key==13)
  172.         continue;
  173.     if (key==27)
  174.         break;
  175. }
  176.  
  177. // Clean-up
  178. context.Shutdown();
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement