ShiftedImage.C

00001 /*!@file Transport/RandomInput.C A FrameIstream subclass for
00002   generating random images */
00003 
00004 // //////////////////////////////////////////////////////////////////// //
00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00006 // by the University of Southern California (USC) and the iLab at USC.  //
00007 // See http://iLab.usc.edu for information about this project.          //
00008 // //////////////////////////////////////////////////////////////////// //
00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00011 // in Visual Environments, and Applications'' by Christof Koch and      //
00012 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00013 // pending; application number 09/912,225 filed July 23, 2001; see      //
00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00015 // //////////////////////////////////////////////////////////////////// //
00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00017 //                                                                      //
00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00019 // redistribute it and/or modify it under the terms of the GNU General  //
00020 // Public License as published by the Free Software Foundation; either  //
00021 // version 2 of the License, or (at your option) any later version.     //
00022 //                                                                      //
00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00026 // PURPOSE.  See the GNU General Public License for more details.       //
00027 //                                                                      //
00028 // You should have received a copy of the GNU General Public License    //
00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00031 // Boston, MA 02111-1307 USA.                                           //
00032 // //////////////////////////////////////////////////////////////////// //
00033 //
00034 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Transport/ShiftedImage.C $
00036 // $Id: ShiftedImage.C 8602 2007-07-20 23:10:44Z rjpeters $
00037 //
00038 
00039 #ifndef TRANSPORT_RANDOMINPUT_C_DEFINED
00040 #define TRANSPORT_RANDOMINPUT_C_DEFINED
00041 
00042 #include "Transport/ShiftedImage.H"
00043 
00044 #include "Image/Image.H"
00045 #include "Raster/Raster.H"
00046 #include "Image/Pixels.H"
00047 
00048 
00049 #include "Image/ShapeOps.H"
00050 
00051 #include "Raster/GenericFrame.H"
00052 
00053 // ######################################################################
00054 ShiftedImage::ShiftedImage(OptionManager& mgr)
00055   :
00056   FrameIstream(mgr, "Random Input", "ShiftedImage")
00057 {
00058   itsStep = 0;
00059 
00060   itsIsPlanar   = false;
00061   itsIsFoe      = false;
00062   itsIsRotation = false;
00063 }
00064 
00065 // ######################################################################
00066 ShiftedImage::~ShiftedImage()
00067 {}
00068 
00069 // ######################################################################
00070 void ShiftedImage::setConfigInfo(const std::string& options)
00071 {
00072   // NOTE: if you modify any behavior here, then please update the
00073   // corresponding documentation for the global "--in" option inside
00074   // the OPT_InputFrameSource definition in Media/MediaOpts.C
00075 
00076   LINFO("options: %s", options.c_str());
00077   if (options.size() == 0) return;
00078 
00079   // parse the options
00080 
00081   // get the image location first
00082   std::string rops = options;
00083   std::string::size_type fcpos = rops.find_first_of(':');
00084   std::string img  = rops.substr(0, fcpos);  
00085   itsImage = Raster::ReadRGB(img);
00086   LINFO("Image: %s: (%d %d)",img.c_str(), 
00087         itsImage.getWidth(), itsImage.getHeight());
00088 
00089   // get the shifting command
00090   std::string command = rops.substr(fcpos+1);
00091   std::string::size_type fbpos = rops.find_first_of('[');
00092   std::string::size_type bbpos = rops.find_first_of(']');
00093   std::string params;
00094   if(fbpos != std::string::npos)
00095     {
00096       std::string temp = rops.substr(fcpos+1, fbpos - fcpos - 1); 
00097       command = temp;
00098       params  = rops.substr(fbpos+1,bbpos - fbpos - 1);      
00099     }
00100   LINFO("Command: %s Params: %s", command.c_str(), params.c_str());
00101 
00102   // shifting parameters
00103   if(!command.compare("planar"))       setConfigPlanar(params);
00104   else 
00105     if(!command.compare("foe"))   setConfigFoe(params);
00106     else 
00107       if(!command.compare("rotation")) setConfigRotation(params);
00108       else LFATAL("unknown ShiftedImage option: %s", command.c_str());
00109 }
00110 
00111 // ######################################################################
00112 void ShiftedImage::setConfigPlanar(const std::string& params)
00113 {              
00114   itsIsPlanar = true;
00115 
00116   itsTotalSteps = 30; 
00117 
00118   itsDx = 2.0; 
00119   itsDy = 0.0;
00120   LINFO("Planar Motion");
00121 }
00122 
00123 // ######################################################################
00124 void ShiftedImage::setConfigFoe(const std::string& params)
00125 {              
00126   itsIsFoe = true;
00127   itsFoe = Point2D<int>(160,120);
00128 
00129   itsTotalSteps = 30;
00130 
00131   itsDx = 0.0; 
00132   itsDy = 0.0;
00133   LINFO("Focus of Expansion");
00134 }
00135 
00136 // ######################################################################
00137 void ShiftedImage::setConfigRotation(const std::string& params)
00138 {              
00139   itsIsRotation = true;
00140   itsRotationCenter = Point2D<int>(160,120);
00141 
00142   LINFO("Rotation Motion");
00143 }
00144 
00145 // ######################################################################
00146 GenericFrameSpec ShiftedImage::peekFrameSpec()
00147 {
00148   GenericFrameSpec result;
00149 
00150   result.nativeType = GenericFrame::RGB_U8;
00151   result.videoFormat = VIDFMT_AUTO;
00152   result.videoByteSwap = false;
00153   result.dims = itsImage.getDims();
00154   result.floatFlags = 0;
00155 
00156   return result;
00157 }
00158 
00159 // ######################################################################
00160 GenericFrame ShiftedImage::readFrame()
00161 {
00162   Image<PixRGB<byte> > result;
00163 
00164   // add dots
00165   if(itsIsPlanar)        result = getPlanarMotionStimuli(itsStep); 
00166   else if(itsIsFoe)      result = getFoeStimuli(itsStep);
00167   else if(itsIsRotation) result = getRotationMotionStimuli(itsStep); 
00168 
00169   itsStep++;
00170  
00171   return GenericFrame(result);
00172 }
00173 
00174 // ######################################################################
00175 Image<PixRGB<byte> > ShiftedImage::getPlanarMotionStimuli(uint step)
00176 {
00177   // just loop it 
00178   step = step % itsTotalSteps;
00179   
00180   uint width  = itsImage.getWidth();
00181   uint height = itsImage.getHeight(); 
00182   float scale =  1.25;
00183   Image<PixRGB<byte> > temp = rescale(itsImage, scale*width, scale*height);
00184   float nwidth  = temp.getWidth();
00185   float nheight = temp.getHeight(); 
00186 
00187   float sleft = 0.0; if(itsDx < 0.0) sleft = nwidth  - 1 - width;
00188   float stop  = 0.0; if(itsDy < 0.0) stop  = nheight - 1 - height; 
00189   
00190   float left  = sleft + itsDx*step;
00191   float top   = stop  + itsDy*step; 
00192 
00193   //if(top  < 0.0) top  = 0.0;
00194   //if(left < 0.0) left = 0.0;
00195 
00196   Rectangle r = 
00197     Rectangle::tlbrI(top, left, top+height-1, left+width-1);
00198 
00199 //   LINFO("[%3d/%3d] FOE(%7.3f %7.3f) %f p(%7.3f %7.3f) [[%7.3f %7.3f]] " 
00200 //          "[%3d %3d %3d %3d] temp(%3d %3d) ((%3d %3d))", 
00201 //         step, totalStep, 
00202 //         foeX,foeY, scale, px, py, top, left,
00203 //         r.top(), r.left(), r.bottomI(), r.rightI(), 
00204 //         temp.getWidth(), temp.getHeight(),
00205 //         r.width(),r.height());
00206   Image<PixRGB<byte> > result = crop(temp, r);
00207   return result;
00208 }
00209 
00210 // ######################################################################
00211  Image<PixRGB<byte> > ShiftedImage::getFoeStimuli(uint step)
00212 {
00213   uint mag = 2;
00214   
00215   // just loop it 
00216   step = step % itsTotalSteps;
00217 
00218   // set original image on first step
00219   if(step == 0) return itsImage; 
00220 
00221   uint width  = itsImage.getWidth();
00222   uint height = itsImage.getHeight();
00223 
00224   float nsize = (step/(itsTotalSteps - 1.0));
00225   float scale =  1.0 / (1.0 - nsize*1.0/mag);
00226   Image<PixRGB<byte> > temp = rescale(itsImage, scale*width, scale*height);
00227   float nwidth  = temp.getWidth();
00228   float nheight = temp.getHeight(); 
00229 
00230   float px = float(itsFoe.i)/float(width);
00231   float py = float(itsFoe.j)/float(height);
00232 
00233   float foeX  = px*nwidth; 
00234   float foeY  = py*nheight; 
00235 
00236   LINFO("[%d] %d %d -> %f %f", step, itsFoe.i, itsFoe.j, itsDx, itsDy);
00237 
00238   float left  = foeX - float(itsFoe.i) + itsDx*step;
00239   float top   = foeY - float(itsFoe.j) + itsDy*step; 
00240 
00241 
00242   //if(top  < 0.0) top  = 0.0;
00243   //if(left < 0.0) left = 0.0;
00244 
00245   Rectangle r = 
00246     Rectangle::tlbrI(top, left, top+height-1, left+width-1);
00247 
00248   LINFO("[%3d/%3d] FOE(%7.3f %7.3f) %f p(%7.3f %7.3f) [[%7.3f %7.3f]] " 
00249          "[%3d %3d %3d %3d] temp(%3d %3d) ((%3d %3d))", 
00250         step, itsTotalSteps, 
00251         foeX,foeY, scale, px, py, top, left,
00252         r.top(), r.left(), r.bottomI(), r.rightI(), 
00253         temp.getWidth(), temp.getHeight(),
00254         r.width(),r.height());
00255   Image<PixRGB<byte> > result = crop(temp, r);
00256 
00257   return result;
00258 }
00259 
00260 // ######################################################################
00261 Image<PixRGB<byte> > ShiftedImage::getRotationMotionStimuli(uint step)
00262 {
00263   uint width  = itsImage.getWidth();
00264   uint height = itsImage.getHeight();
00265   Image<PixRGB<byte> > temp(width, height, ZEROS);
00266 
00267   LINFO("Rotation Motion: FIXXX: NOT YET IMPLEMENTED");
00268 
00269   return temp;
00270 }
00271 
00272 // ######################################################################
00273 /* So things look consistent in everyone's emacs... */
00274 /* Local Variables: */
00275 /* indent-tabs-mode: nil */
00276 /* End: */
00277 
00278 #endif // TRANSPORT_RANDOMINPUT_C_DEFINED
Generated on Sun May 8 08:42:24 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3