data_capture.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "data_capture.hpp"
00004 
00005 #define CHECK_STATUS(rc, msg) if((rc) != XN_STATUS_OK) { \
00006   fprintf(stderr, "%s: %s\n", (msg), xnGetStatusString(rc)); return false; }
00007 
00008 namespace fovis_example
00009 {
00010 
00011 DataCapture::DataCapture()
00012 {
00013   width = 640;
00014   height = 480;
00015 
00016   memset(&rgb_params, 0, sizeof(fovis::CameraIntrinsicsParameters));
00017   rgb_params.width = width;
00018   rgb_params.height = height;
00019 
00020   // TODO read these values from the camera somehow, instead of hard-coding it
00021   // Unfortunately, the OpenNI API doesn't seem to expose them.
00022   rgb_params.fx = 528.49404721; 
00023   rgb_params.fy = rgb_params.fx;
00024   rgb_params.cx = width / 2.0;
00025   rgb_params.cy = height / 2.0;
00026 
00027   depth_image = new fovis::DepthImage(rgb_params, width, height);
00028   depth_data = new float[width * height];
00029   gray_buf = new uint8_t[width * height];
00030 }
00031 
00032 DataCapture::~DataCapture()
00033 {
00034   delete[] depth_data;
00035   delete[] gray_buf;
00036 }
00037 
00038 bool
00039 DataCapture::initialize()
00040 {
00041   XnStatus rc = context.Init();
00042   CHECK_STATUS(rc, "Initializing device context");
00043 
00044   printf("Initializing image stream\n");
00045   image_gen.Create(context);
00046   rc = image_gen.Create(context);
00047   CHECK_STATUS(rc, "Initializing image stream");
00048 
00049   // set output format to RGB
00050   image_gen.SetPixelFormat(XN_PIXEL_FORMAT_RGB24);
00051 
00052   XnMapOutputMode image_mode;
00053   image_mode.nXRes = width;
00054   image_mode.nYRes = height;
00055   image_mode.nFPS = 30;
00056   image_gen.SetMapOutputMode(image_mode);
00057   CHECK_STATUS(rc, "Setting image output mode");
00058 
00059 
00060   printf("Initializing depth stream\n");
00061   rc = depth_gen.Create(context);
00062   CHECK_STATUS(rc, "Initializing depth stream");
00063 
00064   depth_gen.SetMapOutputMode(image_mode);
00065   CHECK_STATUS(rc, "Setting depth output mode");
00066 
00067   depth_gen.GetMetaData(depth_md);
00068   printf("Depth offset: %d %d\n", depth_md.XOffset(), depth_md.YOffset());
00069   // XXX do we need to do something with the depth offset?
00070 
00071   // set the depth image viewpoint
00072   depth_gen.GetAlternativeViewPointCap().SetViewPoint(image_gen);
00073 
00074   // read off the depth camera field of view.  This is the FOV corresponding to
00075   // the IR camera viewpoint, regardless of the alternative viewpoint settings.
00076   XnFieldOfView fov;
00077   rc = depth_gen.GetFieldOfView(fov);
00078   return true;
00079 }
00080 
00081 bool
00082 DataCapture::startDataCapture()
00083 {
00084   // start data capture
00085   printf("Starting data capture\n");
00086   XnStatus rc = context.StartGeneratingAll();
00087   CHECK_STATUS(rc, "Starting data capture");
00088   return true;
00089 }
00090 
00091 bool
00092 DataCapture::stopDataCapture()
00093 {
00094   context.StopGeneratingAll();
00095   context.Shutdown();
00096   return true;
00097 }
00098 
00099 bool
00100 DataCapture::captureOne()
00101 {
00102   // Read a new frame
00103   XnStatus rc = context.WaitAndUpdateAll();
00104   CHECK_STATUS(rc, "Reading frame");
00105 
00106   // grab the image data
00107   image_gen.GetMetaData(image_md);
00108   const XnRGB24Pixel* rgb_data = image_md.RGB24Data();
00109 
00110   // convert to grayscale.
00111   int num_rgb_pixels = width * height;
00112   for(int i=0; i<num_rgb_pixels; i++) {
00113 //    gray_buf[i] = (int)round((rgb_data->nRed + 
00114 //                              rgb_data->nGreen + 
00115 //                              rgb_data->nBlue) / 3.0);
00116     gray_buf[i] = (int)round(0.2125 * rgb_data->nRed + 
00117                              0.7154 * rgb_data->nGreen + 
00118                              0.0721 * rgb_data->nBlue);
00119     rgb_data++;
00120   }
00121 
00122   // grab the depth data
00123   depth_gen.GetMetaData(depth_md);
00124   int depth_data_nbytes = width * height * sizeof(uint16_t);
00125   const uint16_t* depth_data_u16 = depth_md.Data();
00126 
00127   // convert to meters, and set unknown depth values to NAN
00128   int num_depth_pixels = width * height;
00129   for(int i=0; i<num_depth_pixels; i++) {
00130     uint16_t d = depth_data_u16[i];
00131     if(d != 0) {
00132       depth_data[i] = d * 1e-3;
00133     } else {
00134       depth_data[i] = NAN;
00135     }
00136   }
00137 
00138   depth_image->setDepthImage(depth_data);
00139   return true;
00140 }
00141 
00142 }


libfovis
Author(s): Albert Huang, Maurice Fallon
autogenerated on Thu Jun 6 2019 20:16:12