00001 /*!@file Channels/VarianceChannel.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/VarianceChannel.C $ 00035 // $Id: VarianceChannel.C 8939 2007-11-04 07:38:34Z itti $ 00036 // 00037 00038 #ifndef VARIANCECHANNEL_C_DEFINED 00039 #define VARIANCECHANNEL_C_DEFINED 00040 00041 #include "Channels/VarianceChannel.H" 00042 00043 // ###################################################################### 00044 // Variance channel member definitions 00045 // ###################################################################### 00046 VarianceChannel::VarianceChannel(OptionManager& mgr) : 00047 SingleChannel(mgr, "Variance", "variance", VARIANCE, 00048 rutz::shared_ptr< PyrBuilder<float> >(NULL)), 00049 itsMap() 00050 { } 00051 00052 // ###################################################################### 00053 VarianceChannel::~VarianceChannel() 00054 { } 00055 00056 // ###################################################################### 00057 bool VarianceChannel::outputAvailable() const 00058 { return itsMap.initialized(); } 00059 00060 // ###################################################################### 00061 void VarianceChannel::doInput(const InputFrame& inframe) 00062 { 00063 const LevelSpec ls = itsLevelSpec.getVal(); 00064 ASSERT(ls.levMin() == ls.levMax()); 00065 ASSERT(ls.delMin() == 0 && ls.delMax() == 0); 00066 ASSERT(ls.levMin() == ls.mapLevel()); 00067 ASSERT(inframe.grayFloat().initialized()); 00068 00069 const uint lev = ls.mapLevel(); 00070 00071 // figure out the tile and map sizes: 00072 const int siz = 1 << lev; 00073 const int w = inframe.getWidth(); 00074 const int h = inframe.getHeight(); 00075 itsMap.resize(w >> lev, h >> lev); 00076 00077 // let's loop over the tiles and compute variance for each: We use 00078 // the well-known identity E[(X-E[X])^2] = E[X^2] - E[X]^2 and we 00079 // also use the small-sample approximation to the variance (divide 00080 // by n-1 instead of n): 00081 Image<float>::iterator dest = itsMap.beginw(); 00082 for (int j = 0; j <= h-siz; j += siz) 00083 { 00084 const int jmax = std::min(j + siz, h); 00085 for (int i = 0; i <= w-siz; i += siz) 00086 { 00087 const int imax = std::min(i + siz, w); 00088 double sum = 0.0, sumsq = 0.0; 00089 Image<float>::const_iterator src = 00090 inframe.grayFloat().begin() + i + j * w; 00091 00092 // compute sum and sum of squares: 00093 for (int jj = j; jj < jmax; jj ++) 00094 { 00095 for (int ii = i; ii < imax; ii ++) 00096 { 00097 const double val = *src++; 00098 sum += val; sumsq += val * val; 00099 } 00100 // skip to next input row: 00101 src += w - imax + i; 00102 } 00103 00104 // now compute the variance: 00105 const double npix = double((jmax - j) * (imax - i)); 00106 const double mean = sum / npix; 00107 double var = (sumsq - mean * mean * npix) / (npix - 1.0); 00108 if (var < 0.0) var = 0.0; // sometimes happens due to rounding 00109 00110 *dest++ = sqrtf(float(var)) / 255.0F; 00111 } 00112 } 00113 } 00114 00115 // ###################################################################### 00116 Image<float> VarianceChannel::getOutput() 00117 { return itsMap; } 00118 00119 00120 // ###################################################################### 00121 /* So things look consistent in everyone's emacs... */ 00122 /* Local Variables: */ 00123 /* indent-tabs-mode: nil */ 00124 /* End: */ 00125 00126 #endif // VARIANCECHANNEL_C_DEFINED