test-followColor-master.C

Go to the documentation of this file.
00001 /*!@file Beobot/test-followColor-master.C color segment following - master */
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:  T. Nathan Mundhenk <mundhenk@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Beobot/test-followColor-master.C $
00035 // $Id: test-followColor-master.C 9412 2008-03-10 23:10:15Z farhan $
00036 //
00037 
00038 #include "Beowulf/Beowulf.H"
00039 #include "Component/ModelManager.H"
00040 #include "Devices/CameraControl.H"
00041 #include "Devices/FrameGrabberConfigurator.H"
00042 #include "Devices/KeyBoard.H"
00043 #include "Image/Image.H"
00044 #include "Media/FrameSeries.H"
00045 #include "Transport/FrameIstream.H"
00046 #include "Util/Timer.H"
00047 #include "Util/Types.H"
00048 #include "Util/log.H"
00049 #include "VFAT/segmentImageMerge.H"
00050 #include "rutz/shared_ptr.h"
00051 #include <cstdio>
00052 #include <cstdlib>
00053 
00054 // number of frames over which framerate info is averaged:
00055 #define NAVG 20
00056 
00057 // ######################################################################
00058 //! The main routine. Grab frames, process, send commands to slave node.
00059 int main(const int argc, const char **argv)
00060 {
00061   // instantiate a model manager
00062   ModelManager manager( "Following Color Segments - Master" );
00063 
00064   nub::soft_ref<FrameGrabberConfigurator>
00065     gbc( new FrameGrabberConfigurator( manager ) );
00066   manager.addSubComponent( gbc );
00067   nub::soft_ref<CameraControl>
00068     camera( new CameraControl( manager, "Camera Controller", "CameraControl",
00069                                0, true, 0) );
00070   manager.addSubComponent( camera );
00071   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00072   manager.addSubComponent(ofs);
00073 
00074   // parse command-line
00075   if( manager.parseCommandLine( argc, argv, "", 0, 0 ) == false )
00076     {
00077       LFATAL( "Command-line parse error\n" );
00078     }
00079 
00080   // do post-command-line configs:
00081   nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber();
00082   if (gb.isInvalid())
00083     LFATAL("You need to select a frame grabber type via the "
00084            "--fg-type=XX command-line option for this program "
00085           "to be useful");
00086 
00087   // variables for segmenting and tracking
00088   int width = gb->getWidth(), height = gb->getHeight();
00089   float delay = 0;
00090   float H,S,V,Hs,Ss,Vs;
00091   float LOTcount = 0;
00092 
00093   // wait for key press 'A'
00094   KeyBoard myKB;
00095   while( myKB.getKey( true ) != KBD_KEY1 )
00096     printf("\a"); //beep
00097 
00098   // let's get all our ModelComponent instances started:
00099   manager.start();
00100 
00101   // timer initialization
00102   Timer tim; Image< PixRGB<byte> > ima; Image< PixRGB<float> > fima;
00103   Timer camPause;       // to pause the move command
00104   camPause.reset();
00105   uint64 t[NAVG]; int frame = 0;
00106 
00107   // configure segmenter and tracker
00108   segmentImage segment(HSV);
00109   segmentImageTrack track(1000, &segment);
00110   H = 200; Hs = 20;
00111   S = .70; Ss = .20;
00112   V = 150; Vs = 150;
00113   segment.setHue(H,Hs,0);
00114   segment.setSat(S,Ss,0);
00115   segment.setVal(V,Vs,0);
00116   segment.setHSVavg(15);
00117   segment.setFrame(0,0,width/4,height/4,width/4,height/4);
00118 
00119   while(1) {
00120     // start timing
00121     tim.reset();
00122 
00123     ima = gb->readRGB();
00124 
00125     uint64 t0 = tim.get();  // to measure display time
00126 
00127     // decimate image to 1/4 size
00128     fima = decXY(ima);
00129     fima = decXY(fima);
00130 
00131     // segment image
00132     segment.segment(fima);
00133     Image<byte> outputI = segment.returnNormalizedCandidates();
00134     segment.calcMassCenter();
00135     track.track(0);
00136 
00137     for(int i = 0; i < segment.numberBlobs(); i++)
00138     {
00139       if(track.isCandidate(i) == true)
00140       {
00141         segment.getHSVvalueMean(i,&H,&S,&V,&Hs,&Ss,&Vs);
00142         int tt = segment.getYmin(i); int bb = segment.getYmax(i);
00143         int ll = segment.getXmin(i); int rr = segment.getXmax(i);
00144         if((bb != tt) && (ll != rr))
00145           drawRect(ima, Rectangle::tlbrI(tt*4,ll*4,bb*4,rr*4),
00146                    PixRGB<byte>(255,255,0),1);
00147         drawCircle(ima, Point2D<int>((int)segment.getCenterX(i)*4
00148                                 ,(int)segment.getCenterY(i)*4)
00149                    ,(int)sqrt((double)segment.getMass(i)),
00150                    PixRGB<byte>(0,0,255),2);
00151         drawCircle(ima, Point2D<int>((int)segment.getCenterX(i)*4
00152                                 ,(int)segment.getCenterY(i)*4)
00153                    ,2,PixRGB<byte>(255,0,0),2);
00154       }
00155       if(track.returnLOT() == true)
00156       {
00157         if(LOTcount == 2)
00158         {
00159           H = 200; Hs = 20;
00160           S = .70; Ss = .20;
00161           V = 150; Vs = 150;
00162           LOTcount = 0;
00163         }
00164         else
00165         {
00166           LOTcount++;
00167         }
00168       }
00169       segment.setHue(H,(Hs*3),0);
00170       segment.setSat(S,(Ss*3),0);
00171       segment.setVal(V,(Vs*3),0);
00172     }
00173     drawCircle(ima, Point2D<int>((int)track.getObjectX()*4
00174                             ,(int)track.getObjectY()*4)
00175                ,2,PixRGB<byte>(0,255,0));
00176 
00177     if(camPause.get() > delay)
00178     {
00179       LINFO( "Object mass: %d", track.getMass() );
00180       int modi = (int)track.getObjectX()*8;
00181       int modj = 480-((int)track.getObjectY()*8);
00182       if(modi > 0 && modi < 640 && modj > 0 && modj < 480)
00183       {
00184         if(!track.returnLOT() &&
00185            track.getMass() < 2400 && track.getMass() > 30 )
00186         {
00187           /* // send speed and steer command to Board B
00188           if( car->getSpeed() < 0.18 )
00189             car->setSpeed( car->getSpeed() + 0.01 );
00190           car->setSteering( 1.0f * 1/320 * ( modi - 320 ) );
00191           */
00192           LINFO( "Steering to %f", 1.0f * 1/320 * ( modi - 320 ) );
00193         }
00194         else
00195           {
00196             /* // send speed and steer command to Board B
00197             car->setSpeed( 0.0 );
00198             car->setSteering( 0.0 );
00199             */
00200             LINFO("Loss of Track, stopping");
00201           }
00202       }
00203     }
00204 
00205     // display segment image if option was specified
00206     ofs->writeRGB(ima, "input");
00207     ofs->writeGray(outputI, "normalizedCandidates");
00208 
00209     // compute and show framerate over the last NAVG frames:
00210     t[frame % NAVG] = tim.get();
00211     t0 = t[frame % NAVG] - t0;
00212     if (t0 > 28) LINFO("Display took %llums", t0);
00213 
00214     if (frame % NAVG == 0 && frame > 0)
00215       {
00216         uint64 avg = 0; for (int i = 0; i < NAVG; i ++) avg += t[i];
00217         float avg2 = 1000.0 / (float)avg * NAVG;
00218         printf("Framerate: %.1f fps\n", avg2);
00219       }
00220     frame ++;
00221   }
00222 
00223 
00224   /************************ MERGING CHANGES??? ******************
00225 
00226   // allocate X-Windows if we are using them
00227   XWindow *wini = NULL;
00228   XWindow *wino1 = NULL;
00229   XWindow *wino2 = NULL;
00230   if( usingX )
00231     {
00232       wini = new XWindow( Dims(width, height), 0, 0, "test-input window" );
00233       wino1 = new XWindow( Dims(width/4, height/4), 0, 0, "test-output window 1" );
00234       wino2 = new XWindow( Dims(width/4, height/4), 0, 0, "test-output window 2" );
00235     }
00236 
00237   Timer tim;
00238   Image< PixRGB<byte> > ima;
00239   Timer camPause;       // to pause the move command
00240   camPause.reset();
00241 
00242   uint64 t[NAVG]; int frame = 0;
00243   segmentImageMerge segmenter(2);
00244   // set up tracking parameters
00245   segmenter.setTrackColor(10,10,0.15,0.20,150,150,0,true,15);
00246   //segmenter.setTrackColor(10,10,0.15,0.20,150,150,1,false,15);
00247   segmenter.setTrackColor(270,10,0.18,0.25,60,60,1,true,15);
00248   segmenter.setAdaptBound(15,5,.30,.25,140,100,0);
00249   segmenter.setAdaptBound(285,265,.25,.15,80,40,1);
00250   segmenter.setFrame(0,0,width/4,height/4,width/4,height/4,0);
00251   segmenter.setFrame(0,0,width/4,height/4,width/4,height/4,1);
00252   segmenter.setCircleColor(0,255,0,0);
00253   segmenter.setCircleColor(0,0,255,1);
00254   segmenter.setBoxColor(255,255,0,0);
00255   segmenter.setBoxColor(255,0,255,1);
00256 
00257   segmenter.setAdapt(3,true,3,true,3,true,0);
00258   segmenter.setAdapt(3,true,3,true,3,true,1);
00259 
00260   while(1) {
00261     tim.reset();
00262     ima = gb->readRGB();
00263     uint64 t0 = tim.get();  // to measure display time
00264 
00265     Image<PixRGB<byte> > Aux1;
00266     Image<PixRGB<byte> > Aux2;
00267     Aux1.resize(100,450,true);
00268     Aux2.resize(100,450,true);
00269 
00270     Image<byte> outputI1;
00271     Image<byte> outputI2;
00272 
00273     display = ima;
00274     segmenter.trackImage(ima,&display,&outputI1,0,&Aux1);
00275     segmenter.trackImage(ima,&display,&outputI2,1,&Aux2);
00276     segmenter.mergeImages(&display);
00277 
00278     if(camPause.get() > delay)
00279     {
00280       int modi,modj;
00281       segmenter.getImageTrackXY(&modi,&modj,0);
00282       //segmenter.getImageTrackXYMerge(&modi,&modj);
00283       modi = modi*8;
00284       modj = 480-modj*8;
00285       if(modi > 0 && modi < 640 && modj > 0 && modj < 480)
00286       {
00287         if(segmenter.returnLOT(0) == false)
00288         {
00289           camPause.reset();
00290 
00291           // send the speed and steer command to Board B
00292           //car->setSpeed( 0.18f );
00293           //car->setSteering( 0.5f * 1/320 * (modi-320) );
00294 
00295 
00296           // delay = camera->moveCamXYFrame(modi,modj);
00297         }
00298  else
00299         {
00300           LINFO("Loss of track, stopping");
00301 
00302           // send speed and steer command to Board B
00303           //car->setSpeed( 0.0f );
00304           //car->setSteering( 0.0f );
00305         }
00306       }
00307     }
00308 
00309     // display segment image in XWindow if option was specified
00310     if( usingX )
00311     {
00312       wini->drawImage(ima);
00313       wino1->drawImage(outputI1);
00314       wino2->drawImage(outputI2);
00315     }
00316     t[frame % NAVG] = tim.get();
00317     t0 = t[frame % NAVG] - t0;
00318     if (t0 > 28) LINFO("Display took %llums", t0);
00319 
00320     // compute and show framerate over the last NAVG frames:
00321     if (frame % NAVG == 0 && frame > 0)
00322       {
00323         uint64 avg = 0; for (int i = 0; i < NAVG; i ++) avg += t[i];
00324         float avg2 = 1000.0 / (float)avg * NAVG;
00325         printf("Framerate: %.1f fps\n", avg2);
00326       }
00327     frame++;
00328   }
00329   ********** END MERGING CHANGES????*********************************/
00330 
00331   manager.stop();
00332 
00333   return 0;
00334 }
00335 
00336 // ######################################################################
00337 /* So things look consistent in everyone's emacs... */
00338 /* Local Variables: */
00339 /* indent-tabs-mode: nil */
00340 /* End: */
Generated on Sun May 8 08:40:15 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3