test-grab.C

Go to the documentation of this file.
00001 /*!@file AppDevices/test-grab.C Test frame grabbing and X display */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppDevices/test-grab.C $
00035 // $Id: test-grab.C 14290 2010-12-01 21:44:03Z itti $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Devices/FrameGrabberConfigurator.H"
00040 #include "GUI/XWindow.H"
00041 #include "Image/Image.H"
00042 #include "Image/Pixels.H"
00043 #include "Raster/Raster.H"
00044 #include "Raster/GenericFrame.H"
00045 #include "Transport/FrameIstream.H"
00046 #include "Util/Timer.H"
00047 #include "Util/Types.H"
00048 #include "Util/log.H"
00049 
00050 #include <cstdio>
00051 #include <cstdlib>
00052 #include <cstring>
00053 
00054 #define NAVG 20
00055 
00056 /*! This simple executable tests video frame grabbing through the
00057   video4linux driver (see V4Lgrabber.H) or the IEEE1394 (firewire)
00058   grabber (see IEEE1394grabber.H). Selection of the grabber type is
00059   made via the --fg-type=XX command-line option. */
00060 int main(const int argc, const char **argv)
00061 {
00062   // instantiate a model manager:
00063   ModelManager manager("Frame Grabber Tester");
00064 
00065   // Instantiate our various ModelComponents:
00066   nub::soft_ref<FrameGrabberConfigurator>
00067     gbc(new FrameGrabberConfigurator(manager));
00068   manager.addSubComponent(gbc);
00069 
00070   // Parse command-line:
00071   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00072 
00073   // do post-command-line configs:
00074   nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber();
00075   if (gb.isInvalid())
00076     LFATAL("You need to select a frame grabber type via the "
00077            "--fg-type=XX command-line option for this program "
00078            "to be useful");
00079 
00080   // let's get all our ModelComponent instances started:
00081   manager.start();
00082 
00083   // get ready for main loop:
00084   Timer tim; uint64 t[NAVG]; int frame = 0;
00085   GenericFrameSpec fspec = gb->peekFrameSpec();
00086   Dims windims = fspec.dims;
00087   if (fspec.nativeType == GenericFrame::RGBD) windims = Dims(windims.w() * 2, windims.h());
00088   XWindow win(windims, -1, -1, "test-grab window");
00089   int count = 0;
00090 
00091   // prepare a gamma table for RGBD displays (e.g., Kinect grabber):
00092   uint16 itsGamma[2048];
00093   for (int i = 0; i < 2048; ++i) {
00094     float v = i/2048.0;
00095     v = powf(v, 3)* 6;
00096     itsGamma[i] = v*6*256;
00097   }
00098 
00099   // get the frame grabber to start streaming:
00100   gb->startStream();
00101 
00102   while(1) {
00103     ++count; tim.reset();
00104 
00105     GenericFrame fr = gb->readFrame();
00106 
00107     Image< PixRGB<byte> > ima = fr.asRgbU8();
00108 
00109     if (fspec.nativeType == GenericFrame::RGBD) {
00110       Image<uint16> dimg = fr.asGrayU16(); // get the depth image
00111 
00112       Image<PixRGB<byte> > d(dimg.getDims(), NO_INIT);
00113       const int sz = dimg.size();
00114       for (int i = 0; i < sz; ++i) {
00115         uint v = dimg.getVal(i); if (v > 2047) v = 2047;
00116         int pval = itsGamma[v];
00117         int lb = pval & 0xff;
00118         switch (pval>>8) {
00119         case 0: d.setVal(i, PixRGB<byte>(255, 255-lb, 255-lb)); break;
00120         case 1: d.setVal(i, PixRGB<byte>(255, lb, 0)); break;
00121         case 2: d.setVal(i, PixRGB<byte>(255-lb, 255, 0)); break;
00122         case 3: d.setVal(i, PixRGB<byte>(0, 255, lb)); break;
00123         case 4: d.setVal(i, PixRGB<byte>(0, 255-lb, 255)); break;
00124         case 5: d.setVal(i, PixRGB<byte>(0, 0, 255-lb)); break;
00125         default: d.setVal(i, PixRGB<byte>(0, 0, 0)); break;
00126         }
00127       }
00128       ima = concatX(ima, d);
00129     }
00130 
00131     uint64 t0 = tim.get();  // to measure display time
00132 
00133     win.drawImage(ima);
00134 
00135     t[frame % NAVG] = tim.get();
00136     t0 = t[frame % NAVG] - t0;
00137     if (t0 > 20000ULL) LINFO("Display took %lluus", t0);
00138 
00139 
00140     // compute and show framerate over the last NAVG frames:
00141     if (frame % NAVG == 0 && frame > 0)
00142       {
00143         uint64 avg = 0ULL; for (int i = 0; i < NAVG; i ++) avg += t[i];
00144         float avg2 = 1000.0F / float(avg) * float(NAVG);
00145         printf("Framerate: %.1f fps\n", avg2);
00146       }
00147     frame ++;
00148   }
00149 
00150   // stop all our ModelComponents
00151   manager.stop();
00152 
00153   // all done!
00154   return 0;
00155 }
00156 
00157 // ######################################################################
00158 /* So things look consistent in everyone's emacs... */
00159 /* Local Variables: */
00160 /* indent-tabs-mode: nil */
00161 /* End: */
Generated on Sun May 8 08:04:10 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3