data_capture.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "data_capture.hpp"
00004 
00005 namespace fovis_example
00006 {
00007 
00008 DataCapture::DataCapture()
00009 {
00010   width = 640;
00011   height = 480;
00012 
00013   memset(&rgb_params, 0, sizeof(fovis::CameraIntrinsicsParameters));
00014   rgb_params.width = width;
00015   rgb_params.height = height;
00016 
00017   // TODO read these values from the camera somehow, instead of hard-coding it
00018   rgb_params.fx = 528.49404721;
00019   rgb_params.fy = rgb_params.fx;
00020   rgb_params.cx = width / 2.0;
00021   rgb_params.cy = height / 2.0;
00022 
00023   depth_image = new fovis::DepthImage(rgb_params, width, height);
00024   depth_data = new float[width * height];
00025   gray_buf = new uint8_t[width * height];
00026 
00027   freenect_angle = 0;
00028   device_number = 0;
00029 }
00030 
00031 DataCapture::~DataCapture()
00032 {
00033   delete[] depth_data;
00034   delete[] gray_buf;
00035 }
00036 
00037 bool
00038 DataCapture::initialize()
00039 {
00040   // initialize the kinect device
00041   if (freenect_init(&f_ctx, NULL) < 0) {
00042     printf("freenect_init() failed\n");
00043     return false;
00044   }
00045 
00046   freenect_set_log_level(f_ctx, FREENECT_LOG_ERROR);
00047 
00048   int num_devices = freenect_num_devices(f_ctx);
00049   printf("Number of devices found: %d\n", num_devices);
00050 
00051   if (num_devices < 1)
00052     return false;
00053 
00054   if (freenect_open_device(f_ctx, &f_dev, device_number) < 0) {
00055     printf("Could not open device\n");
00056     return false;
00057   }
00058 
00059   freenect_set_user(f_dev, this);
00060 
00061   freenect_frame_mode vmode = freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB);
00062   freenect_frame_mode dmode = freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_REGISTERED);
00063 
00064   freenect_set_video_mode(f_dev, vmode);
00065   freenect_set_depth_mode(f_dev, dmode);
00066 
00067   return true;
00068 }
00069 
00070 bool
00071 DataCapture::startDataCapture()
00072 {
00073   // start data capture
00074   printf("Starting data capture\n");
00075 
00076   freenect_set_tilt_degs(f_dev, freenect_angle);
00077   freenect_set_led(f_dev, LED_OFF);
00078   freenect_set_depth_callback(f_dev, &DataCapture::depth_cb);
00079   freenect_set_video_callback(f_dev, &DataCapture::image_cb);
00080 
00081   freenect_start_depth(f_dev);
00082   freenect_start_video(f_dev);
00083 
00084   return true;
00085 }
00086 
00087 bool
00088 DataCapture::stopDataCapture()
00089 {
00090   freenect_stop_depth(f_dev);
00091   freenect_stop_video(f_dev);
00092 
00093   freenect_close_device(f_dev);
00094   freenect_shutdown(f_ctx);
00095 
00096   return true;
00097 }
00098 
00099 bool
00100 DataCapture::captureOne()
00101 {
00102   while (freenect_process_events(f_ctx) >= 0) {
00103     if (have_image && have_depth) {
00104       have_image = false;
00105       have_depth = false;
00106       return true;
00107     }
00108   }
00109   return false;
00110 }
00111 
00112 void
00113 DataCapture::depth_cb(freenect_device *dev, void *data, uint32_t timestamp)
00114 {
00115   DataCapture* self = (DataCapture*)(freenect_get_user(dev));
00116   self->DepthCallback(data, timestamp);
00117 }
00118 
00119 void
00120 DataCapture::image_cb(freenect_device *dev, void *data, uint32_t timestamp)
00121 {
00122   DataCapture* self = (DataCapture*)(freenect_get_user(dev));
00123   self->ImageCallback(data, timestamp);
00124 }
00125 
00126 void
00127 DataCapture::DepthCallback(void* data, uint32_t timestamp)
00128 {
00129   have_depth = true;
00130 
00131   uint16_t* depth_mm = (uint16_t*)data;
00132   int num_pixels = width * height;
00133   for(int i=0; i<num_pixels; i++) {
00134     uint16_t d = depth_mm[i];
00135     if(d != 0) {
00136       depth_data[i] = d * 1e-3;
00137     } else {
00138       depth_data[i] = NAN;
00139     }
00140   }
00141   depth_image->setDepthImage(depth_data);
00142 }
00143 
00144 void
00145 DataCapture::ImageCallback(void* data, uint32_t timestamp)
00146 {
00147   have_image = true;
00148 
00149   int num_pixels = width * height;
00150   uint8_t* rgb_pixel = (uint8_t*)data;
00151   uint8_t* gray_pixel = gray_buf;
00152   for(int i=0; i<num_pixels; i++) {
00153     gray_pixel[0] = (rgb_pixel[0] + rgb_pixel[1] + rgb_pixel[2]) / 3;
00154     gray_pixel++;
00155     rgb_pixel += 3;
00156   }
00157 }
00158 
00159 }


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