locust_model.C

00001 /*!@file Beobot/locust_model2.C implement the locust model for collision detection with frame series*/
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: Farhan Baluch <fbaluch@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Beobot/locust_model.C $
00035 // $Id: locust_model.C 14376 2011-01-11 02:44:34Z pez $//
00036 
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Devices/FrameGrabberConfigurator.H"
00040 #include "Devices/DeviceOpts.H"
00041 #include "GUI/XWindow.H"
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "Raster/Raster.H"
00045 #include "Transport/FrameIstream.H"
00046 #include "Util/Timer.H"
00047 #include "Util/Types.H"
00048 #include "Util/log.H"
00049 #include "Image/MathOps.H"
00050 #include "Image/ColorOps.H"
00051 #include "Image/Convolver.H"
00052 #include "Image/DrawOps.H"
00053 #include "Media/FrameSeries.H"
00054 #include "Media/MediaOpts.H"
00055 #include "Raster/GenericFrame.H"
00056 
00057 
00058 #include <cstdio>
00059 #include <cstdlib>
00060 #include <cstring>
00061 
00062 int main(int argc, const char **argv)
00063 {
00064   //Instantiate a ModelManager:
00065   ModelManager *mgr = new ModelManager("locust model frame series style");
00066 
00067   nub::ref<InputFrameSeries> ifs(new InputFrameSeries(*mgr));
00068   mgr->addSubComponent(ifs);
00069 
00070   mgr->setOptionValString(&OPT_FrameGrabberMode, "RGB24");
00071   mgr->setOptionValString(&OPT_FrameGrabberDims, "320x240");
00072   mgr->setOptionValString(&OPT_FrameGrabberFPS, "30");
00073 
00074   mgr->exportOptions(MC_RECURSE);
00075 
00076  // Parse command-line:
00077   if (mgr->parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00078 
00079   //XWindow xwin(Dims(1050,490),-1, -1, "locust model");
00080   //Image<PixRGB<byte> > inputImg = Image<PixRGB<byte> >(imageDims.w(),imageDims.h()+20, ZEROS);
00081 
00082   Dims layer_screen(1150,490);
00083   XWindow layers(layer_screen, 0, 0, "layers"); //preview window
00084 
00085   // let's get all our ModelComponent instances started:
00086   mgr->start();
00087 
00088   //layers.drawImage(bg, 0, 0);
00089 
00090   Image<float> p_layer_image[4];
00091   Image<float> i_layer_image[3];
00092   Image<float> s_layer_image[2];
00093 
00094   int maxHistory = 50;
00095   std::vector<float> lgmdPotential(1);
00096   float total_s_layer = 0;
00097   int framecnt=1;
00098   std::vector<Image< PixRGB <byte> >  > frames(5);
00099 
00100   const FrameState is = ifs->updateNext();
00101   if(is == FRAME_COMPLETE)
00102       LFATAL("frames completed!");
00103 
00104    //grab the images
00105   frames[framecnt] = ifs->readRGB();
00106    if(!frames[framecnt].initialized())
00107      LFATAL("frame killed");
00108 
00109 
00110   Image<PixRGB<byte> > temp_p, temp_i, temp_s;
00111   Image<float>::iterator aptr;
00112   int potentialCnt = 1;
00113 
00114 
00115   while(1){
00116 
00117     if(framecnt!=1)
00118       frames[1] = frames[4]; //make last frame of pervious batch the 1st frame for this one
00119 
00120     framecnt=2;
00121 
00122 
00123     while(framecnt<=4)
00124       {
00125 
00126         const FrameState is = ifs->updateNext();
00127         if(is == FRAME_COMPLETE)
00128           break;
00129 
00130         //grab the images
00131         frames[framecnt] = ifs->readRGB();
00132         if(!frames[framecnt].initialized())
00133           break;
00134 
00135 
00136         //LINFO("drawing frame %d",framecnt);
00137         layers.drawImage(frames[framecnt],0,0);
00138         framecnt++;
00139       }
00140     framecnt--;
00141 
00142 
00143 
00144   //p_layer processing
00145     p_layer_image[1] = absDiff(luminance(frames[framecnt-2]),luminance(frames[framecnt-3]));//p(t-2)
00146     p_layer_image[2] = absDiff(luminance(frames[framecnt-1]),luminance(frames[framecnt-2]));//p(t-1)
00147     p_layer_image[3] = absDiff(luminance(frames[framecnt]),luminance(frames[framecnt-1]));//p(t)
00148 
00149     temp_p = p_layer_image[1];
00150     writeText(temp_p,Point2D<int>(2,0),"P-layer",
00151               PixRGB<byte>(255,0,0),PixRGB<byte>(0,0,0), SimpleFont::FIXED(9));
00152     layers.drawImage(temp_p,321,0);
00153 
00154 
00155   //i_layer processing
00156 
00157   //define kernel and  convolver for inhibition layer
00158     Image<float> kernel(3,3,NO_INIT);
00159     std::fill(kernel.beginw(),kernel.endw(),1.0F/9.0F);
00160 
00161 
00162     i_layer_image[1] = ((p_layer_image[1]) + (p_layer_image[2]))*0.25;
00163     Convolver c1(kernel,i_layer_image[1].getDims());
00164     i_layer_image[1] = c1.spatialConvolve(i_layer_image[1]);          //i(t-1)
00165 
00166     i_layer_image[2] = ((p_layer_image[2]) + (p_layer_image[3]))*0.25;
00167     Convolver c2(kernel,i_layer_image[2].getDims());
00168     i_layer_image[2] = c2.spatialConvolve(i_layer_image[2]);           //i(t)
00169 
00170     temp_i = i_layer_image[1];
00171     writeText(temp_i,Point2D<int>(2,0),"I-layer",
00172               PixRGB<byte>(255,0,0),PixRGB<byte>(0,0,0), SimpleFont::FIXED(9));
00173     layers.drawImage(temp_i,0,241);
00174 
00175 
00176   //s_layer processing
00177     s_layer_image[1] = p_layer_image[3] - ((i_layer_image[1])*2);
00178     temp_s = s_layer_image[1];
00179     writeText(temp_s,Point2D<int>(2,0),"S-layer",
00180               PixRGB<byte>(255,0,0),PixRGB<byte>(0,0,0), SimpleFont::FIXED(9));
00181     layers.drawImage(temp_s, 321,241 );
00182 
00183 
00184     aptr  = s_layer_image[1].beginw();
00185 
00186     for (int w = 0; w < s_layer_image[1].getDims().w(); w++)
00187       for(int h = 0; h < s_layer_image[1].getDims().h(); h++)
00188         total_s_layer += *aptr++;
00189 
00190     LINFO("%f", total_s_layer);
00191 
00192     float tempPot = 1/(1 + exp(-total_s_layer/(320*240)));
00193     //float tempPot = total_s_layer;
00194     if(potentialCnt < maxHistory)
00195       {
00196         lgmdPotential.push_back(tempPot);
00197         potentialCnt++;
00198       }
00199     else
00200       {
00201         lgmdPotential.erase(lgmdPotential.begin(), lgmdPotential.begin()+1);
00202         lgmdPotential.push_back(tempPot);
00203         potentialCnt++;
00204       }
00205 
00206     Image<PixRGB<byte> > temp_grid = linePlot(lgmdPotential, 400, 400,
00207                                               0.0f, 5.0f, "time");
00208     char str[256];
00209     sprintf(str, "lgmd potential = %f", tempPot);
00210 
00211     writeText(temp_grid,Point2D<int>(2,0),str,
00212               PixRGB<byte>(255,0,0),PixRGB<byte>(0,0,0), SimpleFont::FIXED(9));
00213     layers.drawImage(temp_grid, 675, 25);
00214 
00215   }
00216 
00217 
00218   //stop all our modelcomponents
00219   mgr->stop();
00220 
00221   return 0;
00222 }
00223 
00224 // ######################################################################
00225 /* So things look consistent in everyone's emacs... */
00226 /* Local Variables: */
00227 /* indent-tabs-mode: nil */
00228 /* End: */
00229 
00230 
00231 
00232 
00233 
Generated on Sun May 8 08:04:29 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3