test-tracking.C

Go to the documentation of this file.
00001 /*!@file Demo/test-tracking.C Test tracking  */
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: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Demo/test-tracking.C $
00035 // $Id: test-tracking.C 9412 2008-03-10 23:10:15Z farhan $
00036 //
00037 
00038 //
00039 #include "Component/ModelManager.H"
00040 #include "Devices/DeviceOpts.H"
00041 #include "GUI/XWindow.H"
00042 #include "Image/DrawOps.H"
00043 #include "Image/CutPaste.H"
00044 #include "Image/Image.H"
00045 #include "Image/Pixels.H"
00046 #include "Image/MathOps.H"
00047 #include "Neuro/EnvVisualCortex.H"
00048 #include "Media/FrameSeries.H"
00049 #include "Media/MediaOpts.H"
00050 #include "Transport/FrameInfo.H"
00051 #include "Raster/GenericFrame.H"
00052 #include "Raster/Raster.H"
00053 #include "Util/Timer.H"
00054 #include "Util/log.H"
00055 #include "Util/MathFunctions.H"
00056 #include "Learn/Bayes.H"
00057 #include "Learn/BackpropNetwork.H"
00058 #include "Envision/env_image_ops.h"
00059 #include "Neuro/BeoHeadBrain.H"
00060 
00061 #include "GUI/DebugWin.H"
00062 #include <ctype.h>
00063 #include <deque>
00064 #include <iterator>
00065 #include <stdlib.h> // for atoi(), malloc(), free()
00066 #include <string>
00067 #include <vector>
00068 #include <map>
00069 
00070 void display(Image<PixRGB<byte> > &leftImg,
00071     const Image<byte> &leftSmap,
00072     const Point2D<int> &leftWinner,
00073     const byte maxVal,
00074     const Point2D<int> &targetLoc);
00075 void display(const Image<PixRGB<byte> > &img,
00076     const Image<PixRGB<byte> > &smap, Point2D<int> &winner, Rectangle &rect);
00077 
00078 //ModelManager *mgr;
00079 XWinManaged *xwin;
00080 Timer timer;
00081 Image<PixRGB<byte> > disp;
00082 byte SmaxVal = 0;
00083 int smap_level = -1;
00084 
00085 bool debug = 0;
00086 bool init_points = true;
00087 
00088 
00089 int main(int argc, const char **argv)
00090 {
00091   // Instantiate a ModelManager:
00092   ModelManager *mgr = new ModelManager("USC Robot Head");
00093 
00094   nub::ref<InputFrameSeries> ifs(new InputFrameSeries(*mgr));
00095   mgr->addSubComponent(ifs);
00096 
00097   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr));
00098   mgr->addSubComponent(ofs);
00099 
00100   nub::ref<EnvVisualCortex> leftEvc(new EnvVisualCortex(*mgr));
00101   mgr->addSubComponent(leftEvc);
00102 
00103   nub::soft_ref<BeoHeadBrain> beoHeadBrain(new BeoHeadBrain(*mgr));
00104   mgr->addSubComponent(beoHeadBrain);
00105 
00106 
00107   mgr->exportOptions(MC_RECURSE);
00108   mgr->setOptionValString(&OPT_EvcMaxnormType, "None");
00109   mgr->setOptionValString(&OPT_EvcLevelSpec, "3,4,3,4,3");
00110 
00111   mgr->setOptionValString(&OPT_InputFrameSource, "V4L");
00112   mgr->setOptionValString(&OPT_FrameGrabberMode, "RGB24");
00113   mgr->setOptionValString(&OPT_FrameGrabberDims, "320x240");
00114   mgr->setOptionValString(&OPT_FrameGrabberFPS, "30");
00115 
00116   //leftEvc->setFweight(255);
00117   //leftEvc->setMweight(255);
00118   //leftEvc->setIweight(100);
00119   //leftEvc->setCweight(100);
00120   //leftEvc->setOweight(100);
00121 
00122   // Parse command-line:
00123   if (mgr->parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00124 
00125   // do post-command-line configs:
00126   Dims imageDims = ifs->peekDims();
00127 
00128   xwin = new XWinManaged(Dims(imageDims.w()*2,imageDims.h()*2+20),
00129       -1, -1, "ILab Robot Head Demo");
00130   disp = Image<PixRGB<byte> >(imageDims.w(),imageDims.h()+20, ZEROS);
00131 
00132   // let's get all our ModelComponent instances started:
00133   mgr->start();
00134 
00135   smap_level = leftEvc->getMapLevel();
00136 
00137   //start streaming
00138   ifs->startStream();
00139 
00140   timer.reset();
00141 
00142   byte leftMaxVal;
00143   Point2D<int> leftMaxPos;
00144 
00145   int frame = 0;
00146 
00147   beoHeadBrain->init(imageDims);
00148 
00149   while(1) {
00150 
00151     Image< PixRGB<byte> > leftImg;
00152     const FrameState is = ifs->updateNext();
00153     if (is == FRAME_COMPLETE)
00154       break;
00155 
00156     //grab the images
00157     GenericFrame input = ifs->readFrame();
00158     if (!input.initialized())
00159       break;
00160     leftImg = input.asRgb();
00161 
00162     leftEvc->input(leftImg);
00163 
00164     ofs->writeRGB(leftImg, "input", FrameInfo("Copy of input", SRC_POS));
00165 
00166     Image<float> leftVcxmap = leftEvc->getVCXmap();
00167     inplaceNormalize(leftVcxmap, 0.0F, 255.0F);
00168     Image<byte> leftSmap = leftVcxmap;
00169     findMax(leftSmap, leftMaxPos, leftMaxVal);
00170 
00171     Image<byte> grey = luminance(leftImg);
00172 
00173     Point2D<int> clickPos = xwin->getLastMouseClick();
00174     //int key = xwin->getLastKeyPress();
00175 
00176     Point2D<int> targetLoc;
00177     if (clickPos.isValid())
00178     {
00179       beoHeadBrain->setTarget(clickPos, grey);
00180     } else {
00181       targetLoc = beoHeadBrain->trackObject(grey);
00182     }
00183     display(leftImg, leftSmap, leftMaxPos,
00184         SmaxVal, targetLoc);
00185 
00186     frame++;
00187   }
00188 
00189 
00190   // stop all our ModelComponents
00191   mgr->stop();
00192 
00193   // all done!
00194   return 0;
00195 }
00196 
00197 void display(Image<PixRGB<byte> > &leftImg,
00198     const Image<byte> &leftSmap,
00199     const Point2D<int> &leftWinner,
00200     const byte maxVal,
00201     const Point2D<int> &targetLoc)
00202 {
00203   static int avgn = 0;
00204   static uint64 avgtime = 0;
00205   static double fps = 0;
00206   char msg[255];
00207 
00208   //Left Image
00209   drawCircle(leftImg,
00210       Point2D<int>(leftWinner.i *(1<<smap_level), leftWinner.j*(1<<smap_level)),
00211       30, PixRGB<byte>(255,0,0));
00212   drawCross(leftImg, Point2D<int>(leftImg.getWidth()/2, leftImg.getHeight()/2),
00213           PixRGB<byte>(0,255,0));
00214   sprintf(msg, "%i", maxVal);
00215   writeText(leftImg,
00216       Point2D<int>(leftWinner.i *(1<<smap_level), leftWinner.j*(1<<smap_level)),
00217         msg, PixRGB<byte>(255), PixRGB<byte>(127));
00218 
00219   if (targetLoc.isValid())
00220     drawCircle(leftImg, targetLoc, 3, PixRGB<byte>(0,255,0));
00221 
00222   xwin->drawImage(leftImg, 0, 0);
00223   Image<PixRGB<byte> > leftSmapDisp = toRGB(quickInterpolate(leftSmap, 1 << smap_level));
00224   //xwin->drawImage(leftSmapDisp, 0, leftImg.getHeight());
00225 
00226 
00227   //calculate fps
00228   avgn++;
00229   avgtime += timer.getReset();
00230   if (avgn == 20)
00231   {
00232     fps = 1000.0F / double(avgtime) * double(avgn);
00233     avgtime = 0;
00234     avgn = 0;
00235   }
00236 
00237 
00238   Image<PixRGB<byte> > infoImg(leftImg.getWidth()*2, 20, NO_INIT);
00239   writeText(infoImg, Point2D<int>(0,0), msg,
00240         PixRGB<byte>(255), PixRGB<byte>(127));
00241   //xwin->drawImage(infoImg, 0, leftImg.getHeight()*2);
00242 
00243 }
00244 
00245 void display(const Image<PixRGB<byte> > &img, const Image<PixRGB<byte> > &out,
00246     Point2D<int> &winner, Rectangle &rect)
00247 {
00248   static int avgn = 0;
00249   static uint64 avgtime = 0;
00250   static double fps = 0;
00251   char msg[255];
00252 
00253   inplacePaste(disp, img, Point2D<int>(0,0));
00254   //drawCircle(disp, Point2D<int>(winner.i, winner.j), 30, PixRGB<byte>(255,0,0));
00255   drawRect(disp, rect, PixRGB<byte>(255,0,0));
00256   inplacePaste(disp, out, Point2D<int>(img.getWidth(), 0));
00257 
00258   //calculate fps
00259   avgn++;
00260   avgtime += timer.getReset();
00261   if (avgn == 20)
00262   {
00263     fps = 1000.0F / double(avgtime) * double(avgn);
00264     avgtime = 0;
00265     avgn = 0;
00266   }
00267 
00268   sprintf(msg, "%.1ffps ", fps);
00269 
00270   writeText(disp, Point2D<int>(0,img.getHeight()), msg,
00271         PixRGB<byte>(255), PixRGB<byte>(127));
00272 
00273   xwin->drawImage(disp);
00274 
00275 }
00276 
Generated on Sun May 8 08:40:37 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3