psycho-foe-make-experiment.C

00001 /*!@file AppPsycho/gaborSearch.C                       */
00002 // //////////////////////////////////////////////////////////////////// //
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00004 // University of Southern California (USC) and the iLab at USC.         //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 //
00032 // Primary maintainer for this file: Elnaz Nouri <enouri@usc.edu>
00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppPsycho/psycho-foe-make-experiment.C $
00034 // $Id: psycho-foe-make-experiment.C 12962 2010-03-06 02:13:53Z irock $
00035 //
00036 
00037 //generate random search arrays of colored gabor patches using the gaborfilterRGB function
00038 
00039 #include "Component/ModelManager.H"
00040 #include "GUI/XWinManaged.H"
00041 #include "Image/CutPaste.H"
00042 #include "Image/DrawOps.H"
00043 #include "Image/FourierEngine.H"
00044 #include "Image/Image.H"
00045 #include "Image/Kernels.H"     // for gaborFilter()
00046 #include "Image/MathOps.H"
00047 #include "Image/ShapeOps.H"
00048 #include "Image/DrawOps.H"
00049 #include "Image/Transforms.H"
00050 #include "Raster/PngWriter.H"
00051 #include "Raster/Raster.H"
00052 #include "Util/FileUtil.H"
00053 #include "Util/log.H"
00054 #include <stdio.h>
00055 #include <algorithm>
00056 #include <time.h>
00057 
00058 
00059 #define WIDTH 1280
00060 #define HEIGHT 720
00061 
00062 typedef std::complex<float> complexf;
00063 
00064 int main(int argc, char** argv)
00065 {
00066   clock_t t1=  clock();
00067   //instantiate a model manager
00068   ModelManager manager("AppPsycho: gabor search stimuli");
00069 
00070   //dimensions of window
00071   Dims dims(WIDTH,HEIGHT);
00072 
00073   float stddev  = 150.0;
00074   float freq    = 5.0;
00075   float theta1  = 10;
00076   float hueShift= 5.0;
00077   float f,t,h;
00078   int gaborSize   = 320;
00079   int n_gabors_w  = dims.w()/gaborSize;
00080   int n_gabors_h  = dims.h()/gaborSize;
00081   int totalGabors = n_gabors_w * n_gabors_h;
00082 
00083   char filename[255],setnumber[255] ,imagenumber[255];
00084 
00085   Image<PixRGB<byte> > dispImg(WIDTH,HEIGHT,NO_INIT);
00086 
00087  if (manager.parseCommandLine(argc, argv,"<setnumber> <imagenumber>", 2, 2) == false){
00088 
00089    return(1);
00090 }
00091   manager.start();
00092 
00093   // get command line parameters
00094   sscanf(argv[1],"%s",setnumber);
00095   sscanf(argv[2],"%s",imagenumber);
00096   sprintf(filename, "/home2/tmp/u/elno/research/exp1/spec/set%s/%sSPEC.dat",setnumber, imagenumber);
00097   FILE* specFile = fopen(filename, "w+");
00098   if(specFile==0)
00099     LFATAL("couldnt open file:%s", filename);
00100   fprintf(specFile,"stddev = %g; freq = %g; theta = %g; hueShift = %g",stddev,freq,theta1,hueShift);
00101 
00102   //make a 2d vector of images//
00103 
00104   std::vector< Image<PixRGB<byte> > > ivector(totalGabors + 1); // vector of gabors
00105   std::vector< Image<PixRGB<byte> > > constructArray(totalGabors + 1);
00106   std::vector<Point2D<int> > gaborItems(totalGabors + 1);  //vector to hold xy coordinates of the gabor Patches
00107   std::vector<Point2D<int> > circleItems(totalGabors + 1);  //vector to hold xy coordinates of the circle
00108   std::vector<Point2D<int> > randPos(totalGabors + 1);  //vector to hold xy coordinates of the gabor Patches
00109   std::vector<int> used (totalGabors + 1);
00110   Dims standarddims(gaborSize,gaborSize);
00111 
00112   srand(time(NULL));
00113   //lets create some random positions
00114   int cnt =1;
00115   int xOffset =0;
00116   int yOffset =40; //yOffset required to compensate for the gaborSize/2 freespace
00117  for(int j = 1; j <= n_gabors_h; j++)
00118     for (int i = 1; i <= n_gabors_w; i++)
00119       {
00120          if(i>1 && i<n_gabors_w-1 && j>1 && j<n_gabors_h-1)
00121            randPos[cnt] =   Point2D<int>((i*gaborSize-gaborSize)+(rand()*15-5) +xOffset ,(j*gaborSize-gaborSize)+(rand()*15-5) + yOffset);
00122          else
00123            randPos[cnt] = Point2D<int>(i*gaborSize-gaborSize + xOffset ,j*gaborSize-gaborSize + yOffset);
00124          cnt++;
00125       }
00126 
00127   //create gabor patches and assign pseudorandom xy positions.
00128   std::vector<int>::iterator result;
00129   srand(time(NULL));
00130   int randIndex = rand() % totalGabors + 1;
00131   used.push_back(randIndex);
00132   result = used.end();
00133 
00134   cnt=1;
00135   for(int j = 1; j <= n_gabors_h; j++)
00136     for (int i = 1; i <= n_gabors_w; i++)
00137       {
00138 
00139         //frequencies can vary between 0.0075 and 0.0225 (0.0065-0.0075, and 0.0225-0.0250 reserved for post-training testing)
00140         f = ((rand()*(float(225-75)/RAND_MAX)) + 75)/10000;
00141         //orientation can vary between 15-75 deg (0-15 deg and 75-90 reserved for post-training testing)
00142         t = (rand()*(float(155-25)/RAND_MAX)) + 25;
00143         //hue shifts can vary between 0-360
00144         h = (rand()*(((float)360/RAND_MAX)));
00145 
00146         ivector[cnt] = Image<PixRGB<byte> >(gaborSize,gaborSize,NO_INIT);
00147         ivector[cnt].clear(PixRGB<byte>(128,128,128));
00148         //drawDisk(constructArray[cnt], gaborItems[cnt]+gaborSize/2 ,gaborSize/4 , PixRGB<byte>(255,255,  0));
00149         //drawDisk(ivector[cnt], Point2D<int> (gaborSize/2,gaborSize/2) ,gaborSize/4 , PixRGB<byte>(128,128,128));
00150         ivector[cnt] = rescale(ivector[cnt],standarddims);
00151         //sprintf(filename, "/home2/tmp/u/elno/research/exp1/stim/fgImages/set%s/%szgab_%d_%d.png",setnumber, imagenumber, i,j);
00152         //Raster::WriteRGB(ivector[cnt],filename);
00153 
00154         ivector[cnt] = ivector[cnt]+ rescale(gaborFilterRGB(stddev, f,t,h),standarddims);
00155 
00156         //sprintf(filename, "/home2/tmp/u/elno/research/exp1/stim/fgImages/set%s/%sz2gab_%d_%d.png",setnumber, imagenumber, i,j);
00157         //Raster::WriteRGB(ivector[cnt],filename);
00158 
00159         //MASK IT
00160         for (int a = 0; a <= gaborSize; ++a)
00161         for (int b = 0; b <= gaborSize; ++b)
00162         {
00163                 if( (squareOf(a-gaborSize/2) + squareOf(b-gaborSize/2)) > squareOf(gaborSize/4) )
00164                         if (ivector[cnt].coordsOk(a, b))
00165                                 ivector[cnt].setVal(a, b, PixRGB<byte>(0,0,0));
00166         }
00167 
00168         while(result!=used.end())
00169           {
00170             srand(time(NULL));
00171             randIndex = rand() % totalGabors + 1;
00172             result = find(used.begin(),used.end(),randIndex);
00173           }
00174 
00175         used.push_back(randIndex);
00176         gaborItems[cnt] = randPos[randIndex];
00177         fprintf(specFile,"\n\nGabor %d",cnt);
00178         fprintf(specFile,"\nXpos\t%d\nYpos\t%d\nstddev\t%.0f\nHueShift\t%.0f\nfrequency\t%f\nOrientation\t%.0f",
00179         gaborItems[cnt].i,gaborItems[cnt].j,stddev,h,f*1000,t);
00180         cnt++;
00181       }
00182 
00183   //lets paste the gabors into one big image
00184   cnt=1;
00185   for(int j = 1; j <= n_gabors_h; j++)
00186     for (int i = 1; i <= n_gabors_w; i++)
00187       {
00188         constructArray[cnt] = Image<PixRGB<byte> >(WIDTH,HEIGHT,NO_INIT);
00189         constructArray[cnt].clear(PixRGB<byte>(0,0,0));
00190          inplacePaste(constructArray[cnt],ivector[cnt],gaborItems[cnt]);
00191           //sprintf(filename, "/lab/ilab19/elnaz/research/exp1/stim/fgImages/set%s/%szconst_%d_%d.png",setnumber, imagenumber, i,j);
00192         //Raster::WriteRGB(constructArray[cnt],filename);
00193          cnt++;
00194       }
00195 
00196 dispImg.clear(PixRGB<byte>(0,0,0));
00197   //lets put them together
00198   for (int i=1; i < cnt; i++){
00199     dispImg = dispImg + constructArray[i]*1;
00200         //sprintf(filename, "/lab/ilab19/elnaz/research/exp1/stim/fgImages/set%s/%szzzzzz_%d.png",setnumber, imagenumber, i);
00201         //Raster::WriteRGB(dispImg,filename);
00202 }
00203 
00204 
00205   Image <PixRGB<byte> > myResult;
00206   double meanGray=0.0;
00207   myResult = dispImg;
00208   fprintf(specFile,"\nGray %f",meanGray);
00209   sprintf(filename, "/home2/tmp/u/elno/research/exp1/stim/fgImages/set%s/%sARRAY.png",setnumber, imagenumber);
00210   Raster::WriteRGB(myResult,filename);
00211 
00212   //create save the target image
00213   Image<double> target(WIDTH,HEIGHT,ZEROS);
00214   Image<PixRGB<byte> > targetBkg(WIDTH,HEIGHT,ZEROS);
00215 
00216   srand(time(NULL));
00217   randIndex = rand() % totalGabors + 1;
00218   fprintf(specFile,"\n\ntarget %d",randIndex);
00219 
00220   inplacePaste(targetBkg, ivector[randIndex],Point2D<int>((WIDTH/2)-(gaborSize/2),(HEIGHT/2)-(gaborSize/2)));
00221   Image<double>::iterator aptr= target.beginw();
00222 
00223   Image<PixRGB<byte> > targetRGB = target;
00224   targetRGB = targetRGB + targetBkg;
00225 
00226 //MASK IT
00227   for (int a = 0; a <= WIDTH; ++a)
00228   for (int b = 0; b <= HEIGHT; ++b)
00229     {
00230         if( (squareOf(a-WIDTH/2) + squareOf(b-HEIGHT/2)) > squareOf(gaborSize/4) )
00231                 if (targetRGB.coordsOk(a, b))
00232                           targetRGB.setVal(a, b, PixRGB<byte>(128,128,128));
00233     }
00234 
00235   while(aptr!=target.endw())
00236     *aptr++ = meanGray;
00237 
00238 
00239   char targetFileName[255];
00240   sprintf(targetFileName, "/home2/tmp/u/elno/research/exp1/stim/fgImages/set%s/%sTARGET.png",setnumber, imagenumber);
00241   Raster::WriteRGB(targetRGB,targetFileName);
00242 
00243   //create the screen for reporting position of target gabor
00244   Image<PixRGB<byte> > reportDot(WIDTH,HEIGHT,ZEROS);
00245   reportDot.clear(PixRGB<byte>(128,128,128));
00246   for(int i=1;i<=totalGabors;i++)
00247     {
00248       //  drawDisk(reportDot, gaborItems[i]+(gaborSize/2), 10,PixRGB<byte>(255,255,255));
00249       writeText(reportDot, gaborItems[i]+(gaborSize/2)-10, sformat("%d",i).c_str(),
00250                 PixRGB<byte>(0,0,0),PixRGB<byte>(0),SimpleFont::FIXED(12) ,true);
00251     }
00252 
00253   char reportDotFile[255];
00254   sprintf(reportDotFile, "/home2/tmp/u/elno/research/exp1/stim/fgImages/set%s/%sREPORT.png",setnumber, imagenumber
00255 );
00256   Raster::WriteRGB(reportDot, reportDotFile);
00257 
00258   clock_t t2=  clock();
00259   LINFO("generated search array in %fs", double(t2-t1)/CLOCKS_PER_SEC);
00260 
00261   fclose(specFile);
00262   //finish all
00263   manager.stop();
00264   return 0;
00265 
00266 }
00267 
00268 
00269 
00270 // ######################################################################
00271 /* So things look consistent in everyone's emacs... */
00272 /* Local Variables: */
00273 /* indent-tabs-mode: nil */
00274 /* End: */
Generated on Sun May 8 08:40:08 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3