SFS.C

Go to the documentation of this file.
00001 /*!@file SceneUnderstanding/SFS.C  Shape from shading */
00002 
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: Lior Elazary <elazary@usc.edu>
00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/plugins/SceneUnderstanding/SFS.C $
00036 // $Id: SFS.C 13551 2010-06-10 21:56:32Z itti $
00037 //
00038 
00039 #ifndef SFS_C_DEFINED
00040 #define SFS_C_DEFINED
00041 
00042 #include "plugins/SceneUnderstanding/SFS.H"
00043 
00044 #include "Image/DrawOps.H"
00045 #include "Image/MathOps.H"
00046 #include "Image/Layout.H"
00047 #include "Simulation/SimEventQueue.H"
00048 #include "Simulation/SimEvents.H"
00049 #include "Media/MediaSimEvents.H"
00050 #include "Channels/InputFrame.H"
00051 #include "Image/Kernels.H"
00052 #include "Image/FilterOps.H"
00053 #include "Image/Convolutions.H"
00054 #include "GUI/DebugWin.H"
00055 #include <math.h>
00056 #include <fcntl.h>
00057 #include <limits>
00058 #include <string>
00059 
00060 const ModelOptionCateg MOC_SFS = {
00061   MOC_SORTPRI_3,   "SFS-Related Options" };
00062 
00063 // Used by: SimulationViewerEyeMvt
00064 const ModelOptionDef OPT_SFSShowDebug =
00065   { MODOPT_ARG(bool), "SFSShowDebug", &MOC_SFS, OPTEXP_CORE,
00066     "Show debug img",
00067     "sfs-debug", '\0', "<true|false>", "false" };
00068 
00069 
00070 // ######################################################################
00071 SFS::SFS(OptionManager& mgr, const std::string& descrName,
00072     const std::string& tagName) :
00073   SimModule(mgr, descrName, tagName),
00074   SIMCALLBACK_INIT(SimEventInputFrame),
00075   SIMCALLBACK_INIT(SimEventSaveOutput),
00076   itsShowDebug(&OPT_SFSShowDebug, this),
00077   itsInitialized(false),
00078   itsNumIter(10),
00079   itsPs(52.46),
00080   itsQs(11.73)
00081 
00082 {
00083 }
00084 
00085 // ######################################################################
00086 SFS::~SFS()
00087 {
00088 
00089 }
00090 
00091 // ######################################################################
00092 void SFS::onSimEventInputFrame(SimEventQueue& q,
00093                                   rutz::shared_ptr<SimEventInputFrame>& e)
00094 {
00095 
00096 }
00097 
00098 // ######################################################################
00099 void SFS::onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00100 {
00101   if (itsShowDebug.getVal())
00102     {
00103       // get the OFS to save to, assuming sinfo is of type
00104       // SimModuleSaveInfo (will throw a fatal exception otherwise):
00105       nub::ref<FrameOstream> ofs =
00106         dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00107       Layout<PixRGB<byte> > disp = getDebugImage();
00108       ofs->writeRgbLayout(disp, "SFS", FrameInfo("SFS", SRC_POS));
00109     }
00110 }
00111 
00112 
00113 // ######################################################################
00114 void SFS::evolve()
00115 {
00116 
00117 }
00118 
00119 // ######################################################################
00120 void SFS::evolve(const Image<byte>& img)
00121 {
00122 
00123   Image<float> Zn1(img.getDims(), NO_INIT);
00124   Image<float> Si1(img.getDims(), NO_INIT);
00125 
00126   /* assume the initial estimate zero at time n-1 */
00127   for(int i=0;i<img.getWidth();i++)
00128     for(int j=0;j<img.getHeight();j++)
00129     {
00130       Zn1.setVal(i,j,0.0);
00131       Si1.setVal(i,j, 0.01);
00132     }
00133 
00134   double Wn=0.0001*0.0001;
00135 
00136   for(int iter=0; iter<itsNumIter; iter++)
00137   {
00138     Image<float> Zn(img.getDims(), NO_INIT);
00139     Image<float> Si(img.getDims(), NO_INIT);
00140 
00141     for(int i=0;i<img.getWidth();i++)
00142       for(int j=0;j<img.getHeight();j++)
00143       {
00144         double p,q;
00145         if(j-1 < 0 || i-1 < 0) /* take care boundary */
00146           p = q = 0.0;
00147         else {
00148           p = Zn1.getVal(i,j) - Zn1.getVal(i,j-1);
00149           q = Zn1.getVal(i,j) - Zn1.getVal(i-1,j);
00150         }
00151         double pq = 1.0 + p*p + q*q;
00152         double PQs = 1.0 + itsPs*itsPs + itsQs*itsQs;
00153         double Eij = img.getVal(i,j)/255.0;
00154         double fZ = -1.0*(Eij - std::max(0.0,(1+p*itsPs+q*itsQs)/(sqrt(pq)*sqrt(PQs))));
00155         double dfZ = -1.0*( (itsPs+itsQs)/(sqrt(pq)*sqrt(PQs)) -
00156                             (p+q)*(1.0+p*itsPs+q*itsQs) /
00157                             (sqrt(pq*pq*pq)*sqrt(PQs)));
00158         double Y = fZ + dfZ*Zn1.getVal(i,j);
00159         double K = Si1.getVal(i,j)*dfZ/(Wn+dfZ*Si1.getVal(i,j)*dfZ);
00160         Si.setVal(i,j,  (1.0 - K*dfZ)*Si1.getVal(i,j));
00161         Zn.setVal(i,j, Zn1.getVal(i,j) + K*(Y-dfZ*Zn1.getVal(i,j)));
00162       }
00163 
00164     Image<float> height = Zn;
00165     inplaceNormalize(height, 0.0F, 100.0F);
00166     Dims dims;
00167     Image<PixRGB<byte> > dImg = img;
00168     Image<PixRGB<byte> > pImg = warp3D(dImg, height, 70.0, 40.0, 150.0F, dims);
00169     SHOWIMG(pImg);
00170 
00171     for(int i=0;i<img.getWidth();i++)
00172       for(int j=0;j<img.getHeight();j++)
00173       {
00174         Zn1.setVal(i,j, Zn.getVal(i,j));
00175         Si1.setVal(i,j, Si.getVal(i,j));
00176       }
00177   }
00178 
00179 }
00180 
00181 Layout<PixRGB<byte> > SFS::getDebugImage()
00182 {
00183 
00184   Layout<PixRGB<byte> > disp;
00185 
00186   return disp;
00187 
00188 }
00189 
00190 // ######################################################################
00191 /* So things look consistent in everyone's emacs... */
00192 /* Local Variables: */
00193 /* indent-tabs-mode: nil */
00194 /* End: */
00195 
00196 #endif
00197 
Generated on Sun May 8 08:41:09 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3