00001 /*!@file Gist/test-gist.C extract gist from an image 00002 using available features from the image */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // 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: Christian Siagian <siagian@usc.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Gist/test-gist.C $ 00036 // $Id: test-gist.C 10827 2009-02-11 09:40:02Z itti $ 00037 // 00038 00039 // ###################################################################### 00040 /*! display and save gist information */ 00041 00042 #include "Component/ModelManager.H" 00043 #include "GUI/XWinManaged.H" 00044 #include "Image/DrawOps.H" 00045 #include "Image/Pixels.H" 00046 #include "Neuro/GistEstimatorStd.H" 00047 #include "Neuro/GistEstimatorTexton.H" 00048 #include "Neuro/NeuroOpts.H" 00049 #include "Neuro/StdBrain.H" 00050 #include "Neuro/gistParams.H" 00051 #include "Raster/Raster.H" 00052 #include "Util/Types.H" 00053 #include "Media/MPEGStream.H" 00054 #include "Media/MediaOpts.H" 00055 #include "Channels/ChannelOpts.H" 00056 00057 #include "Media/SimFrameSeries.H" 00058 #include "Simulation/SimEventQueueConfigurator.H" 00059 00060 #include <cstdio> 00061 #include <cstdlib> 00062 #include <cstring> 00063 #include <math.h> 00064 #include <stdio.h> 00065 00066 // ###################################################################### 00067 void saveData(Image<double> data, std::string fName); 00068 00069 // ###################################################################### 00070 // training procedure 00071 int main(const int argc, const char **argv) 00072 { 00073 MYLOGVERB = LOG_INFO; // suppress debug messages 00074 00075 // Instantiate a ModelManager: 00076 ModelManager manager("test-gist Model"); 00077 00078 // Instantiate our various ModelComponents: 00079 nub::soft_ref<SimEventQueueConfigurator> 00080 seqc(new SimEventQueueConfigurator(manager)); 00081 manager.addSubComponent(seqc); 00082 00083 nub::soft_ref<StdBrain> brain(new StdBrain(manager)); 00084 manager.addSubComponent(brain); 00085 00086 nub::ref<SimInputFrameSeries> ifs(new SimInputFrameSeries(manager)); 00087 manager.addSubComponent(ifs); 00088 00089 manager.setOptionValString(&OPT_SingleChannelSaveRawMaps, "true"); 00090 manager.setOptionValString(&OPT_GistEstimatorType,"Std"); 00091 00092 // Parse command-line: 00093 if (manager.parseCommandLine(argc, argv,"<filename stem> ", 0, 1) == false) 00094 return(1); 00095 00096 nub::ref<SimEventQueue> seq = seqc->getQ(); 00097 00098 // do post-command-line configs: 00099 // GenericFrameSpec fs =ifs->peekFrameSpec(); 00100 // int w = fs.getWidth(), h = fs.getHeight(); 00101 // XWinManaged iWin(img.getDims(),0,0, "win"); 00102 //lzc int s = 20; 00103 //lzc XWinManaged gWin(Dims(NUM_GIST_COL * s, NUM_GIST_FEAT * s), 0,0,"Histogram"); 00104 00105 // let's get all our ModelComponent instances started: 00106 manager.start(); 00107 00108 // get the GistEstimator 00109 LFATAL("fixme"); 00110 nub::soft_ref<GistEstimator> ge; /////// = 00111 ////// dynCastWeak<GistEstimator>(brain->getGE()); 00112 if (ge.isInvalid()) LFATAL("I am useless without a GistEstimator"); 00113 00114 // main loop: 00115 SimStatus status = SIM_CONTINUE; 00116 int fNum = -1; int step = 0; 00117 while(status == SIM_CONTINUE) 00118 { 00119 // switch to next time step: 00120 status = seq->evolve(); 00121 00122 // weird way to detect that new frame is just processed 00123 // and that it's not eof 00124 int fr = ifs->frame(); 00125 if(step > 0) step++; if(fr != fNum) { fNum = fr; step = 1; } 00126 if(step == 3) 00127 { 00128 // display the image 00129 // Image<PixRGB<byte> > img = ifs->readRGB(); 00130 //drawGrid(img, w/4,h/4,1,1,PixRGB<byte>(255,255,255)); 00131 //iWin.drawImage(img,0,0); 00132 00133 // display the gist histogram 00134 //gWin.drawImage(ge->getGistImage(s),0,0); 00135 00136 // save data to a file 00137 Image<double> gistV = ge->getGist(); 00138 00139 if(manager.numExtraArgs() > 0) 00140 { 00141 std::string name 00142 (sformat("%s_%06d.gist", manager.getExtraArg(0).c_str(),fNum)); 00143 LINFO("saving: %s", name.c_str()); 00144 saveData(gistV, name); 00145 } 00146 step = 0; 00147 } 00148 } 00149 00150 // stop all our ModelComponents 00151 //lzc Raster::waitForKey(); 00152 manager.stop(); 00153 } 00154 00155 // ###################################################################### 00156 // save the gist feature vector to a file 00157 void saveData(Image<double> data, std::string fName) 00158 { 00159 // change the extension to .gist 00160 int ldpos = fName.find_last_of('.'); 00161 std::string gName = fName.substr(0, ldpos) + std::string(".gist"); 00162 LINFO("gist file name: %s", gName.c_str()); 00163 00164 // write the data to the gist file 00165 Image<double>::iterator aptr = data.beginw(); 00166 FILE *gfp; if((gfp = fopen(gName.c_str(),"wb")) != NULL) 00167 { 00168 for(int i = 0; i < data.getSize(); i++) 00169 { double val = *aptr++; fwrite(&val, sizeof(double), 1, gfp); } 00170 fclose(gfp); 00171 } 00172 else LFATAL("can't write: %s", gName.c_str()); 00173 00174 // double in[NUM_GIST_COL*NUM_GIST_FEAT]; 00175 // gfp = fopen(gName,"rb"); 00176 // fread(in,sizeof(double), NUM_GIST_COL*NUM_GIST_FEAT, gfp); 00177 // fclose(gfp); 00178 00179 // for(int i = 0; i < NUM_GIST_COL; i++) 00180 // { 00181 // for(int j = 0; j < NUM_GIST_FEAT; j++) 00182 // printf("%5.3f ",in[i*NUM_GIST_FEAT+j]); 00183 // printf("\n"); 00184 // } 00185 } 00186 00187 // ###################################################################### 00188 /* So things look consistent in everyone's emacs... */ 00189 /* Local Variables: */ 00190 /* indent-tabs-mode: nil */ 00191 /* End: */