World2DInput.C

Go to the documentation of this file.
00001 /*!@file Transport/World2DInput.C Simple 2D world */
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/Transport/World2DInput.C $
00035 // $Id: World2DInput.C 14349 2010-12-28 19:48:33Z lior $
00036 //
00037 
00038 #include "Transport/World2DInput.H"
00039 #include "Component/OptionManager.H"
00040 #include "Image/DrawOps.H"
00041 #include "Image/ColorOps.H"
00042 #include "Raster/GenericFrame.H"
00043 
00044 // ######################################################################
00045 World2DInput::World2DInput(OptionManager& mgr) :
00046   FrameIstream(mgr, "World2DInput Input", "World2DInputInput"),
00047   itsImageDims(320,240)
00048 {
00049   initRandomNumbers();
00050 
00051   itsCurrentPos.i = 0;
00052   itsCurrentPos.j = 128;
00053   itsFrame = 0;
00054 
00055 }
00056 
00057 World2DInput::~World2DInput()
00058 {
00059 }
00060 
00061 // ######################################################################
00062 void World2DInput::setConfigInfo(const std::string& dimsstring)
00063 {
00064   // NOTE: if you modify any behavior here, then please update the
00065   // corresponding documentation for the global "--in" option inside
00066   // the OPT_InputFrameSource definition in Media/MediaOpts.C
00067 
00068   if (dimsstring.size() == 0)
00069     return;
00070 
00071   Dims d; convertFromString(dimsstring, d);
00072   this->setImageDims(d);
00073 }
00074 
00075 // ######################################################################
00076 GenericFrameSpec World2DInput::peekFrameSpec()
00077 {
00078   GenericFrameSpec result;
00079 
00080   result.nativeType = GenericFrame::RGB_U8;
00081   result.videoFormat = VIDFMT_AUTO;
00082   result.videoByteSwap = false;
00083   result.dims = itsImageDims;
00084   result.floatFlags = 0;
00085 
00086   return result;
00087 }
00088 
00089 // ######################################################################
00090 GenericFrame World2DInput::readFrame()
00091 {
00092   generateWorld();
00093 
00094   Image<PixRGB<byte> > result = getImage();
00095   usleep(10000);
00096   return GenericFrame(result);
00097 }
00098 
00099 // ######################################################################
00100 void World2DInput::setImageDims(const Dims& s)
00101 {
00102   itsImageDims = s;
00103 }
00104 
00105 
00106 
00107 void World2DInput::generateWorld()
00108 {
00109 
00110   itsWorldImg = Image<PixRGB<byte> >(itsImageDims,ZEROS);
00111   itsWorldImg.clear(PixRGB<byte>(128,128,128));
00112 
00113 
00114  // float p = 128+128*sin((float)itsFrame*0.05);
00115  // drawDisk(itsWorldImg, Point2D<int>((int)p,50), 10, PixRGB<byte>(0,155,100));
00116 
00117  // p = 128+128*sin((float)itsFrame*0.01);
00118  // drawDisk(itsWorldImg, Point2D<int>((int)p,128), 30, PixRGB<byte>(0,255,100));
00119 
00120  // p = 200+50*sin((float)itsFrame*0.008);
00121  // drawDisk(itsWorldImg, Point2D<int>(50, (int)p), 15, PixRGB<byte>(255,255,100));
00122 
00123   //static int col = 240; //itsFrame;
00124   //drawFilledRect(itsWorldImg,
00125   //    Rectangle(Point2D<int>(col,128), Dims(50,50)),
00126   //    PixRGB<byte>(col,col,col));
00127   //col  = (col +1)%255;
00128 
00129   //drawSuperquadric(itsWorldImg, Point2D<int>(320/2, 240/2),
00130   //    50, 50, 0.5, PixRGB<byte>(0,255,0));
00131 
00132 
00133   //Draw overlapping rectangles for amodal completion
00134   Dims size(100,100);
00135   drawFilledRect(itsWorldImg,
00136       Rectangle(Point2D<int>((320/2)-(size.w()/2),
00137           (240/2)-(size.h()/2)), size),
00138       PixRGB<byte>(0,0,0));
00139 
00140   drawFilledRect(itsWorldImg,
00141       Rectangle(Point2D<int>((320/2)-(size.w()/2)-50,(240/2)-(size.h()/2)-50),
00142         size),
00143       PixRGB<byte>(255,255,255));
00144 
00145   //placeLine(itsWorldImg);
00146 
00147   //placeSquare(itsWorldImg);
00148  // placeLine(itsWorldImg);
00149 
00150  // placeSquare(itsWorldImg);
00151 
00152  // inplaceColorSpeckleNoise(itsWorldImg, (int)(itsWorldImg.size()*0.70));
00153   itsFrame++;
00154 }
00155 
00156 double World2DInput::getLineOri()
00157 {
00158 
00159   double ori;
00160   double horizStd = 15; //the probability of horizantal edges in the world
00161   double vertStd = 15; //the probability of vertical edges in the world
00162 
00163   double u = randomDouble();
00164 
00165   int idum = getIdum();
00166   if (u > 0.5)
00167     ori = 0 + horizStd*gasdev(idum);
00168   else
00169     ori = 90 + vertStd*gasdev(idum);
00170 
00171   if (ori < 0)
00172     ori += 180;
00173 
00174   return ori;
00175 }
00176 
00177 double World2DInput::getLineLength()
00178 {
00179 
00180   double length = 10 + randomUpToNotIncluding(itsImageDims.w()/10);
00181 
00182   return length;
00183 }
00184 
00185 PixRGB<byte> World2DInput::getLineColor()
00186 {
00187   return PixRGB<byte>(255,0,0);
00188 }
00189 
00190 
00191 void World2DInput::placeLine(Image<PixRGB<byte> > &img)
00192 {
00193 
00194   //The line parameters
00195   double ori = 45; //getLineOri();
00196   double length = 50; //getLineLength();
00197   PixRGB<byte> col = getLineColor();
00198   int rad = 1;
00199   //Point2D<int> pos(randomUpToNotIncluding(itsImageDims.w()),
00200   //    randomUpToNotIncluding(itsImageDims.h()));
00201   Point2D<int> pos(150,100);
00202 
00203   LINFO("Line param ori=%f, length=%f, pos=%i,%i",
00204       ori, length, pos.i, pos.j);
00205   //getchar();
00206   //draw the line
00207   int x1 = int(cos(ori*M_PI/180)*length);
00208   int y1 = int(sin(ori*M_PI/180)*length);
00209 
00210   Point2D<int> p1 = pos;
00211   Point2D<int> p2(pos.i+x1, pos.j-y1);
00212 
00213   drawLine(img, p1, p2, col, rad);
00214 
00215 }
00216 
00217 void World2DInput::placeSquare(Image<PixRGB<byte> > &img)
00218 {
00219 
00220   //The line parameters
00221   double ori = getLineOri();
00222   double size = getLineLength();
00223   PixRGB<byte> col = getLineColor();
00224   int rad = 1;
00225   Point2D<int> pos(randomUpToNotIncluding(itsImageDims.w()),
00226       randomUpToNotIncluding(itsImageDims.h()));
00227 // Point2D<int> pos(100,100);
00228 
00229   LINFO("Square param ori=%f, size=%f, pos=%i,%i",
00230       ori, size, pos.i, pos.j);
00231   //getchar();
00232   //draw the line
00233   drawRectOR(img, Rectangle(pos, Dims((int)size,(int)size)), col, rad, ori);
00234 
00235 }
00236 
00237 Image<PixRGB<byte> > World2DInput::getImage()
00238 {
00239 
00240   return itsWorldImg;
00241 
00242 }
00243 
00244 // ######################################################################
00245 /* So things look consistent in everyone's emacs... */
00246 /* Local Variables: */
00247 /* indent-tabs-mode: nil */
00248 /* End: */
Generated on Sun May 8 08:42:24 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3