00001 /*!@file Channels/EntropyChannel.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/EntropyChannel.C $ 00035 // $Id: EntropyChannel.C 7434 2006-11-11 02:15:19Z rjpeters $ 00036 // 00037 00038 #ifndef ENTROPYCHANNEL_C_DEFINED 00039 #define ENTROPYCHANNEL_C_DEFINED 00040 00041 #include "Channels/EntropyChannel.H" 00042 00043 #include "Image/Transforms.H" // for highThresh() 00044 00045 // ###################################################################### 00046 // Entropy channel member definitions 00047 // ###################################################################### 00048 EntropyChannel::EntropyChannel(OptionManager& mgr) : 00049 SingleChannel(mgr, "Entropy", "entropy", ENTROPY, 00050 rutz::shared_ptr< PyrBuilder<float> >(NULL)), 00051 itsQstep("EntropyChannelQstep", this, 4), 00052 itsMap() 00053 { } 00054 00055 // ###################################################################### 00056 EntropyChannel::~EntropyChannel() 00057 { } 00058 00059 // ###################################################################### 00060 bool EntropyChannel::outputAvailable() const 00061 { return itsMap.initialized(); } 00062 00063 // ###################################################################### 00064 void EntropyChannel::doInput(const InputFrame& inframe) 00065 { 00066 const LevelSpec ls = itsLevelSpec.getVal(); 00067 ASSERT(ls.levMin() == ls.levMax()); 00068 ASSERT(ls.delMin() == 0 && ls.delMax() == 0); 00069 ASSERT(ls.levMin() == ls.mapLevel()); 00070 ASSERT(inframe.grayFloat().initialized()); 00071 00072 const uint lev = ls.mapLevel(); 00073 00074 // figure out the tile and map sizes: 00075 const int siz = 1 << lev; 00076 const int w = inframe.getWidth(); 00077 const int h = inframe.getHeight(); 00078 itsMap.resize(w >> lev, h >> lev); 00079 const float siz2 = float(siz * siz); 00080 00081 // prepare a histogram for our quantized patch values: 00082 const int nsteps = 256 / itsQstep.getVal(); 00083 int histo[nsteps]; 00084 Image<byte> quant = inframe.grayFloat() / nsteps; // quantize 00085 quant = highThresh(quant, byte(nsteps - 1), byte(nsteps - 1)); // saturate 00086 00087 // let's loop over the tiles and compute entropy for each: 00088 Image<float>::iterator dest = itsMap.beginw(); 00089 00090 for (int j = 0; j <= h-siz; j += siz) 00091 { 00092 const int jmax = std::min(j + siz, h); 00093 for (int i = 0; i <= w-siz; i += siz) 00094 { 00095 const int imax = std::min(i + siz, w); 00096 Image<byte>::const_iterator src = quant.begin() + i + j * w; 00097 memset(histo, 0, nsteps * sizeof(int)); 00098 00099 // accumulate the histogram of values: 00100 for (int jj = j; jj < jmax; jj ++) 00101 { 00102 for (int ii = i; ii < imax; ii ++) 00103 histo[*src++] ++; 00104 00105 // skip to next input row: 00106 src += w - imax + i; 00107 } 00108 00109 // now compute the entropy: 00110 float e = 0.0f; 00111 for (int k = 0; k < nsteps; k ++) 00112 if (histo[k]) 00113 { 00114 const float freq = float(histo[k]) / siz2; 00115 e -= freq * logf(freq); 00116 } 00117 // store entropy in output: 00118 *dest++ = e; 00119 } 00120 } 00121 } 00122 00123 // ###################################################################### 00124 Image<float> EntropyChannel::getOutput() 00125 { return itsMap; } 00126 00127 00128 // ###################################################################### 00129 /* So things look consistent in everyone's emacs... */ 00130 /* Local Variables: */ 00131 /* indent-tabs-mode: nil */ 00132 /* End: */ 00133 00134 #endif // ENTROPYCHANNEL_C_DEFINED