ImageCache.C

Go to the documentation of this file.
00001 /*!@file Image/ImageCache.C implements an image cache with running average */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003   //
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: Dirk Walther <walther@caltech.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/ImageCache.C $
00035 // $Id: ImageCache.C 5736 2005-10-18 17:30:29Z rjpeters $
00036 //
00037 
00038 #include "Image/ImageCache.H"
00039 
00040 #include "Util/Assert.H"
00041 #include "Image/MathOps.H"
00042 #include "Image/ColorOps.H"
00043 #include "Image/Pixels.H"
00044 #include "Image/PixelsInst.H"      // for PIX_INST macro
00045 
00046 // ######################################################################
00047 // ##### Implementation of ImageCache<T>
00048 // ######################################################################
00049 template <class T>
00050 ImageCache<T>::ImageCache(uint maxSize)
00051   : itsMaxSize(maxSize)
00052 {}
00053 
00054 // ######################################################################
00055 template <class T>
00056 ImageCache<T>::~ImageCache()
00057 {}
00058 
00059 // ######################################################################
00060 template <class T>
00061 void ImageCache<T>::setMaxSize(const uint maxSize)
00062 {
00063   itsMaxSize = maxSize;
00064   // truncate if necessary:
00065   popOffOld();
00066 }
00067 
00068 // ######################################################################
00069 template <class T>
00070 uint ImageCache<T>::getMaxSize() const
00071 { return itsMaxSize; }
00072 
00073 // ######################################################################
00074 template <class T>
00075 void ImageCache<T>::push_back(const Image<T>& img)
00076 {
00077   doWhenAdd(img);
00078   itsCache.push_back(img);
00079   popOffOld();
00080   return;
00081 }
00082 
00083 // ######################################################################
00084 template <class T>
00085 void ImageCache<T>::popOffOld()
00086 {
00087   // now pop off old images
00088   if (itsMaxSize == 0) return;
00089 
00090   while (itsCache.size() > itsMaxSize)
00091     pop_front();
00092 
00093   return;
00094 }
00095 
00096 // ######################################################################
00097 template <class T>
00098 Image<T> ImageCache<T>::pop_front()
00099 {
00100   ASSERT(!itsCache.empty());
00101   Image<T> ret = itsCache.front();
00102   doWhenRemove(ret);
00103   itsCache.pop_front();
00104   return ret;
00105 }
00106 
00107 // ######################################################################
00108 template <class T>
00109 const Image<T>& ImageCache<T>::back() const
00110 {
00111   ASSERT(!itsCache.empty());
00112   return itsCache.back();
00113 }
00114 
00115 // ######################################################################
00116 template <class T>
00117 const Image<T>& ImageCache<T>::front() const
00118 {
00119   ASSERT(!itsCache.empty());
00120   return itsCache.front();
00121 }
00122 
00123 // ######################################################################
00124 template <class T>
00125 const Image<T>& ImageCache<T>::getImage(const uint lev) const
00126 {
00127   ASSERT(lev < itsCache.size());
00128   return itsCache[lev];
00129 }
00130 
00131 // ######################################################################
00132 template <class T>
00133 const Image<T>& ImageCache<T>::operator[](const uint lev) const
00134 { return getImage(lev); }
00135 
00136 // ######################################################################
00137 template <class T>
00138 uint ImageCache<T>::size() const
00139 { return itsCache.size(); }
00140 
00141 // ######################################################################
00142 template <class T>
00143 bool ImageCache<T>::empty() const
00144 { return itsCache.empty(); }
00145 
00146 // ######################################################################
00147 template <class T>
00148 void ImageCache<T>::clear()
00149 {
00150   // hopefully, overloads of pop_front() will take care of updating any
00151   // additional data members (like the sliding average image in
00152   // ImageCache):
00153   while(size()) this->pop_front();
00154 }
00155 
00156 // ######################################################################
00157 template <class T>
00158 void ImageCache<T>::doWhenAdd(const Image<T>& img)
00159 { }
00160 
00161 // ######################################################################
00162 template <class T>
00163 void ImageCache<T>::doWhenRemove(const Image<T>& img)
00164 { }
00165 
00166 // ######################################################################
00167 // ##### Implementation of ImageCacheAvg<T>
00168 // ######################################################################
00169 template <class T>
00170 ImageCacheAvg<T>::ImageCacheAvg()
00171   : ImageCache<T>(0)
00172 {}
00173 
00174 // ######################################################################
00175 template <class T>
00176 ImageCacheAvg<T>::ImageCacheAvg(uint maxSize)
00177   : ImageCache<T>(maxSize)
00178 {}
00179 
00180 // ######################################################################
00181 template <class T>
00182 Image<T> ImageCacheAvg<T>::mean() const
00183 {
00184   return Image<T> (this->itsSumImg / this->itsCache.size());
00185 }
00186 
00187 // ######################################################################
00188 template <class T>
00189 Image<T> ImageCacheAvg<T>::absDiffMean(const Image<T>& img) const
00190 {
00191   return absDiff(mean(), img);
00192 }
00193 
00194 // ######################################################################
00195 template <class T>
00196 Image<T> ImageCacheAvg<T>::clampedDiffMean(const Image<T>& img) const
00197 {
00198   return clampedDiff(img, mean());
00199 }
00200 
00201 // ######################################################################
00202 template <class T>
00203 Image<typename promote_trait<T,float>::TP> ImageCacheAvg<T>::sum() const
00204 {
00205   return itsSumImg;
00206 }
00207 
00208 // ######################################################################
00209 template <class T>
00210 void ImageCacheAvg<T>::doWhenAdd(const Image<T>& img)
00211 {
00212   if (itsSumImg.initialized())
00213     {
00214       ASSERT(itsSumImg.isSameSize(img));
00215       itsSumImg += img;
00216    }
00217   else
00218     {
00219       itsSumImg = img;
00220     }
00221 }
00222 
00223 // ######################################################################
00224 template <class T>
00225 void ImageCacheAvg<T>::doWhenRemove(const Image<T>& img)
00226 {
00227   ASSERT(itsSumImg.initialized());
00228   itsSumImg -= img;
00229 }
00230 
00231 
00232 // ######################################################################
00233 // ##### Implementation of ImageCacheMinMax<T>
00234 // ######################################################################
00235 template <class T>
00236 ImageCacheMinMax<T>::ImageCacheMinMax()
00237   : ImageCache<T>(0)
00238 {}
00239 
00240 // ######################################################################
00241 template <class T>
00242 ImageCacheMinMax<T>::ImageCacheMinMax(uint maxSize)
00243   : ImageCache<T>(maxSize)
00244 {}
00245 
00246 // ######################################################################
00247 template <class T>
00248 Image<T> ImageCacheMinMax<T>::getMax() const
00249 {
00250   if (this->itsCache.size() == 0) return Image<T>(); // empty
00251   typename std::deque< Image<T> >::const_iterator
00252     itr = this->itsCache.begin(), end = this->itsCache.end();
00253   Image<T> ret = *itr++;
00254   while(itr != end) ret = takeMax(ret, *itr++);
00255   return ret;
00256 }
00257 
00258 // ######################################################################
00259 template <class T>
00260 Image<T> ImageCacheMinMax<T>::getMin() const
00261 {
00262   if (this->itsCache.size() == 0) return Image<T>(); // empty
00263   typename std::deque< Image<T> >::const_iterator
00264     itr = this->itsCache.begin(), end = this->itsCache.end();
00265   Image<T> ret = *itr++;
00266   while(itr != end) ret = takeMin(ret, *itr++);
00267   return ret;
00268 }
00269 
00270 // ######################################################################
00271 template <class T>
00272 void ImageCacheMinMax<T>::doWhenAdd(const Image<T>& img)
00273 { }
00274 
00275 // ######################################################################
00276 template <class T>
00277 void ImageCacheMinMax<T>::doWhenRemove(const Image<T>& img)
00278 { }
00279 
00280 // ######################################################################
00281 
00282 // See PixelsTypesDefine.H for information about PIX_INST
00283 
00284 #ifdef INVT_INST_BYTE
00285 PIX_INST(ImageCache, byte);
00286 PIX_INST(ImageCacheAvg, byte);
00287 template class ImageCacheMinMax<byte>;
00288 #endif
00289 
00290 #ifdef INVT_INST_INT16
00291 PIX_INST(ImageCache, int16);
00292 PIX_INST(ImageCacheAvg, int16);
00293 template class ImageCacheMinMax<int16>;
00294 #endif
00295 
00296 #ifdef INVT_INST_INT32
00297 PIX_INST(ImageCache, int32);
00298 PIX_INST(ImageCacheAvg, int32);
00299 template class ImageCacheMinMax<int32>;
00300 #endif
00301 
00302 #ifdef INVT_INST_FLOAT
00303 PIX_INST(ImageCache, float);
00304 PIX_INST(ImageCacheAvg, float);
00305 template class ImageCacheMinMax<float>;
00306 #endif
00307 
00308 // ######################################################################
00309 /* So things look consistent in everyone's emacs... */
00310 /* Local Variables: */
00311 /* indent-tabs-mode: nil */
00312 /* End: */
Generated on Sun May 8 08:40:54 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3