PN03contrastChannel.C

Go to the documentation of this file.
00001 /*!@file Channels/PN03contrastChannel.C */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the 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:
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Channels/PN03contrastChannel.C $
00035 // $Id: PN03contrastChannel.C 7434 2006-11-11 02:15:19Z rjpeters $
00036 //
00037 
00038 #ifndef PN03CONTRASTCHANNEL_C_DEFINED
00039 #define PN03CONTRASTCHANNEL_C_DEFINED
00040 
00041 #include "Channels/PN03contrastChannel.H"
00042 
00043 // ######################################################################
00044 // PN03contrast channel member definitions
00045 // ######################################################################
00046 PN03contrastChannel::PN03contrastChannel(OptionManager& mgr) :
00047   SingleChannel(mgr, "PN03contrast", "pn03contrast", PN03CONTRAST,
00048                 rutz::shared_ptr< PyrBuilder<float> >(NULL)),
00049   itsMap()
00050 { }
00051 
00052 // ######################################################################
00053 PN03contrastChannel::~PN03contrastChannel()
00054 { }
00055 
00056 // ######################################################################
00057 bool PN03contrastChannel::outputAvailable() const
00058 { return itsMap.initialized(); }
00059 
00060 // ######################################################################
00061 void PN03contrastChannel::doInput(const InputFrame& inframe)
00062 {
00063 //    LERROR("\n START doInput of PN03contrast \n");
00064 
00065   const LevelSpec ls = itsLevelSpec.getVal();
00066   ASSERT(ls.levMin() == ls.levMax());
00067   ASSERT(ls.delMin() == 0 && ls.delMax() == 0);
00068   ASSERT(ls.levMin() == ls.mapLevel());
00069   ASSERT(inframe.grayFloat().initialized());
00070 
00071   const uint lev = ls.mapLevel();
00072 
00073   // figure out the tile and map sizes:
00074   const int siz = 1 << lev;
00075   const int w = inframe.getWidth();
00076   const int h = inframe.getHeight();
00077   itsMap.resize(w >> lev, h >> lev);
00078   const float siz2 = float(siz * siz);
00079 
00080   // let's loop over the tiles and compute contrast for each:
00081   Image<float>::iterator dest = itsMap.beginw();
00082   for (int j = 0; j < h; j += siz)
00083   {
00084       const int jmax = std::min(j + siz, h);
00085       for (int i = 0; i < w; i += siz)
00086       {
00087            const int imax = std::min(i + siz, w);
00088            float patch_sum=0;
00089            double patch_sumsq=0;
00090            for (int jj = j; jj < jmax; jj ++)
00091            {
00092                for (int ii = i; ii < imax; ii ++) {
00093                    float pix_val = inframe.grayFloat().getVal(ii,jj);
00094                    patch_sum+=pix_val;
00095                    patch_sumsq+=pix_val*pix_val;
00096                }
00097            }
00098 //           patch_sum = std::accumulate(inframe.grayFloat().begin() + i+j*w, inframe.grayFloat().begin() + imax+jmax*w, 0.0); // includes out-of-patch pixels!
00099            const double patch_mean =  patch_sum / double(siz2);
00100            const double patch_var = patch_sumsq / double(siz2) - patch_mean*patch_mean;
00101 
00102                // calculate the sd of pixel intensities (for long_patch_var)
00103 //            double patch_diffsum=0;
00104 //            if (i==0 && j==0) LERROR("patch mean: %.2f\n", patch_mean);
00105 //            if (i==0 && j==0) LERROR( "[ " );
00106 //            for (int jj = j; jj < jmax; jj ++)
00107 //            {
00108 //                for (int ii = i; ii < imax; ii ++) {
00109 //                    double pix_val = inframe.grayFloat().getVal(ii,jj);
00110 //                    if (i==0 && j==0) std::cerr << pix_val << " ";
00111 //                    double pix_diff = pix_val-patch_mean;
00112 //                    double pix_var = double(pix_diff*pix_diff);
00113 //                    patch_diffsum+=pix_var;
00114 //                }
00115 //                if (i==0 && j==0) std::cerr << "\n  ";
00116 //            }
00117 //            if (i==0 && j==0) LERROR(" ]\n");
00118 
00119           // now compute the contrast:
00120 
00121 //           if (i==0 && j==0) LERROR("short patch var %.2f\n", patch_var);
00122 //            double long_patch_var=patch_diffsum/double(siz2);
00123 //            if (i==0 && j==0) LERROR("long patch var %.2f\n", long_patch_var);
00124 
00125            const double patch_cntrst = sqrt(patch_var);
00126            if (i==0 && j==0) LERROR( "\nfirst %ix%i patch contrast: %.2f\n\n", siz, siz, patch_cntrst);
00127            *dest++ = patch_cntrst;
00128         }
00129     }
00130 //  LERROR("\n END doInput of PN03contrast \n");
00131 }
00132 
00133 // ######################################################################
00134 Image<float> PN03contrastChannel::getOutput()
00135 { return itsMap; }
00136 
00137 
00138 // ######################################################################
00139 /* So things look consistent in everyone's emacs... */
00140 /* Local Variables: */
00141 /* indent-tabs-mode: nil */
00142 /* End: */
00143 
00144 #endif // PN03CONTRASTCHANNEL_C_DEFINED
Generated on Sun May 8 08:40:22 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3