extractsalientpatches.C

00001 /*!@file INVT/neovision2.C CUDA-accelerated Neovision2 integrated demo */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the 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: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/INVT/neovision2-cuda.C $
00035 // $Id: neovision2-cuda.C 13232 2010-04-15 02:15:06Z dparks $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Image/ColorOps.H"
00040 #include "Image/CutPaste.H"
00041 #include "Image/DrawOps.H"
00042 #include "Image/FilterOps.H"
00043 #include "Image/Image.H"
00044 #include "Image/ImageSet.H"
00045 #include "Image/Layout.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/Pixels.H"
00048 #include "Image/PyramidOps.H"
00049 #include "Image/ShapeOps.H"
00050 #include "Image/Transforms.H"
00051 #include "Media/FrameSeries.H"
00052 #include "Media/MediaOpts.H"
00053 #include "Raster/GenericFrame.H"
00054 #include "Raster/Raster.H"
00055 #include "Transport/FrameInfo.H"
00056 #include "Transport/TransportOpts.H"
00057 #include "Util/csignals.H"
00058 #include "rutz/shared_ptr.h"
00059 #include "rutz/trace.h"
00060 #include "rutz/rand.h"
00061 
00062 #include "CUDA/CudaSaliency.H"
00063 
00064 #include <ctype.h>
00065 #include <deque>
00066 #include <iterator>
00067 #include <limits>
00068 #include <stdlib.h> // for atoi(), malloc(), free()
00069 #include <string.h>
00070 #include <sys/resource.h>
00071 #include <signal.h>
00072 #include <time.h>
00073 #include <vector>
00074 
00075 
00076 
00077 // ######################################################################
00078 int submain(int argc, const char** argv)
00079 {
00080   volatile int signum = 0;
00081   signal(SIGPIPE, SIG_IGN);
00082   catchsignals(&signum);
00083 
00084   // Instantiate our various ModelComponents:
00085 
00086   ModelManager manager("Nv2");
00087 
00088   nub::ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00089   manager.addSubComponent(ifs);
00090 
00091   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00092   manager.addSubComponent(ofs);
00093 
00094   nub::ref<CudaSaliency> cus(new CudaSaliency(manager));
00095   manager.addSubComponent(cus);
00096 
00097   if (manager.parseCommandLine((const int)argc, (const char**)argv, "<sampleimagesdir>", 1, 1) == false)
00098     return 1;
00099 
00100   manager.exportOptions(MC_RECURSE);
00101 
00102   manager.start();
00103 
00104   std::string sampleImagesDir;
00105 
00106   sampleImagesDir = manager.getExtraArg(0);
00107 
00108 
00109   int retval = 0;
00110   ifs->startStream();
00111   const GenericFrameSpec fspec = ifs->peekFrameSpec();
00112   Image<PixRGB<byte> > rgbin_last;
00113   // Randomize the seed
00114   //rutz::urand r(time((time_t*)0) + getpid());
00115   // Don't randomize the seed, i want the same frames each time
00116   rutz::urand r(0);
00117 
00118   int curPickedFrame=0;
00119 
00120   while (true)
00121     {
00122       if (signum != 0) {
00123         LINFO("quitting because %s was caught", signame(signum));
00124         retval = -1;
00125         break;
00126       }
00127 
00128       if (ofs->becameVoid()) {
00129         LINFO("quitting because output stream was closed or became void");
00130         break;
00131       }
00132 
00133       //
00134       // get the next frame from our input source
00135       //
00136       const FrameState is = ifs->updateNext();
00137       if (is == FRAME_COMPLETE) break;
00138 
00139       GenericFrame input = ifs->readFrame();
00140       if (!input.initialized()) break;
00141 
00142       const Image<PixRGB<byte> > rgbin = input.asRgb();
00143 
00144       rgbin_last = rgbin;
00145       cus->doInput(rgbin);
00146 
00147 
00148       // Sub sample video, randomly pick roughly every N frames
00149       int willPick = r.idraw(100);
00150 
00151       if(willPick==0)
00152         {
00153             //Different Maps
00154           CudaImage<float> out = cus->getCudaOutput();
00155 
00156           std::vector<Point2D<int> > loc =  cus->getSalMaxLoc();
00157 
00158           int rectW = cus->getPatchSize();
00159           int rectH = cus->getPatchSize();
00160       
00161           for(unsigned int i=0; i<loc.size(); i++)
00162             {
00163               Point2D<int> modLoc;
00164               modLoc.i = loc[i].i * rgbin.getWidth() / double(out.getWidth());
00165               modLoc.j = loc[i].j * rgbin.getHeight() / double(out.getHeight());
00166 
00167               int fi = std::max(modLoc.i-int(rectW/2.0),0);
00168               int fj = std::max(modLoc.j-int(rectH/2.0),0);
00169               Point2D<int> p = Point2D<int>(fi,fj);
00170               Rectangle foa = Rectangle(p,Dims(rectW,rectH));
00171               printf("FOA:<%s>  ",convertToString(modLoc).c_str());
00172               Image<PixRGB<byte> > patch = crop(rgbin,foa);
00173               Image<PixRGB<byte> > patchResize = rescale(patch, 256, 256);
00174 
00175               char fname[200];
00176               sprintf(fname,"%s/Sample-%d-%d.png",sampleImagesDir.c_str(),curPickedFrame,i);
00177               Raster::WriteRGB(patchResize,fname);
00178 
00179             }
00180           printf("\n");
00181           curPickedFrame++;
00182         }
00183 
00184     }
00185 
00186   manager.stop();
00187   return retval;
00188 }
00189 
00190 // ######################################################################
00191 int main(int argc, const char** argv)
00192 {
00193   try {
00194     return submain(argc, argv);
00195   } catch (...) {
00196     REPORT_CURRENT_EXCEPTION;
00197   }
00198 }
00199 
00200 // ######################################################################
00201 /* So things look consistent in everyone's emacs... */
00202 /* Local Variables: */
00203 /* mode: c++ */
00204 /* indent-tabs-mode: nil */
00205 /* End: */
Generated on Sun May 8 08:40:57 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3