00001 /*!@file Image/ImageCache.H 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.H $ 00035 // $Id: ImageCache.H 4663 2005-06-23 17:47:28Z rjpeters $ 00036 // 00037 00038 #ifndef IMAGECACHE_H_DEFINED 00039 #define IMAGECACHE_H_DEFINED 00040 00041 #include "Image/Image.H" 00042 #include "Util/Promotions.H" 00043 00044 #include <deque> 00045 00046 00047 // ###################################################################### 00048 //! base class for image caches that do computations on the fly 00049 /*! This base class has no op doWhenAdd and doWhenRemove functions 00050 that should be overridden in classes derived from this one.*/ 00051 template <class T> 00052 class ImageCache 00053 { 00054 public: 00055 //! Constructor 00056 /*! @param maxSize the maximum size of the cache. If this size is exceeded, 00057 images are popped off the front of the cache and disregarded for the 00058 computation of the mean. If maxSize = 0, the cache is not limited.*/ 00059 ImageCache(uint maxSize = 0); 00060 00061 //! Destructor 00062 virtual ~ImageCache(); 00063 00064 //! Set maximum number of images in the cache 00065 /*! If the cache currently has more images than the new specified 00066 maximum, it will be truncated to that maximum. */ 00067 virtual void setMaxSize(const uint maxSize); 00068 00069 //! Get maximum number of images in the cache 00070 virtual uint getMaxSize() const; 00071 00072 //! add image to cache - if the cache gets to big, old images are popped off 00073 virtual void push_back(const Image<T>& img); 00074 00075 //! pop the front Image (oldest) off the cache and return it 00076 virtual Image<T> pop_front(); 00077 00078 //! access the last Image (newest) in the queue 00079 virtual const Image<T>& back() const; 00080 00081 //! access the first Image (oldest) in the queue 00082 virtual const Image<T>& front() const; 00083 00084 //! Get image from a given level. 00085 virtual const Image<T>& getImage(const uint lev) const; 00086 00087 //! Get image from a given level (shorthand for getImage()). 00088 virtual const Image<T>& operator[](const uint lev) const; 00089 00090 //! return the current size of the cache 00091 /*! This may be smaller than the maximum size specified at 00092 construction or with setMaxSize(), if the cache is not full yet. */ 00093 virtual uint size() const; 00094 00095 //! true if the cache contains no elements 00096 virtual bool empty() const; 00097 00098 //! clear the cache, preserving its maxSize 00099 virtual void clear(); 00100 00101 protected: 00102 //! checks whether the cache has become too big and removes old entries 00103 virtual void popOffOld(); 00104 00105 //! called when an image is added - override in your derived classes! 00106 /*! in ImageCache, this function is no op*/ 00107 virtual void doWhenAdd(const Image<T>& img); 00108 00109 //! called when an image is removed - override in your derived classes 00110 /*! in ImageCache, this function is no op*/ 00111 virtual void doWhenRemove(const Image<T>& img); 00112 00113 //! the maximum size of images to be stored 00114 uint itsMaxSize; 00115 00116 //! the cache of the images 00117 std::deque< Image<T> > itsCache; 00118 }; 00119 00120 // ###################################################################### 00121 //! image cache to compute the running average 00122 template <class T> 00123 class ImageCacheAvg : public ImageCache<T> 00124 { 00125 public: 00126 //! Uninitialized constructor 00127 /*! By default, cache size is not limited. A limit can be set later 00128 using setMaxSize(). */ 00129 ImageCacheAvg(); 00130 00131 //! Constructor 00132 /*! @param maxSize the maximum size of the cache. If this size is exceeded, 00133 images are popped off the front of the cache and disregarded for the 00134 computation of the mean. If maxSize = 0, the cache is not limited.*/ 00135 ImageCacheAvg(uint maxSize); 00136 00137 //! return the mean of the images currently in the cache 00138 Image<T> mean() const; 00139 00140 //! return abs(img - mean) 00141 Image<T> absDiffMean(const Image<T>& img) const; 00142 00143 //! return (img - mean), clamped to zero where negative 00144 Image<T> clampedDiffMean(const Image<T>& img) const; 00145 00146 //! returns the sum of all cached images 00147 /*! This will be of type Image<int> if T = byte, int16 or int32; 00148 of type float if T = float; of type Image< PixRGB<int> > 00149 if T = PixRGB<byte>, PixRGB<int16> or PixRGB<int32>; 00150 and of type Image< PixRGB<float> > if T = PixRGB<float> */ 00151 Image<typename promote_trait<T,float>::TP> sum() const; 00152 00153 protected: 00154 //! called when an image is added - add image to the sum 00155 virtual void doWhenAdd(const Image<T>& img); 00156 00157 //! called when an image is removed - subtract image from the sum 00158 virtual void doWhenRemove(const Image<T>& img); 00159 00160 //! the sum of all the cached images 00161 Image<typename promote_trait<T,float>::TP> itsSumImg; 00162 }; 00163 00164 // ###################################################################### 00165 //! image cache to compute a running min/max 00166 /*! In this implementation, all the computations are done when 00167 getMax() or getMin() are called. That is, the cache is fairly dump 00168 and does not attempt to keep an updated min and max image as new 00169 images get added or removed. This differs substantially from the 00170 ImageCacheAvg implementation, where all computations are done as images 00171 are added/removed and getting the mean is a very cheap operation. */ 00172 template <class T> 00173 class ImageCacheMinMax : public ImageCache<T> 00174 { 00175 public: 00176 //! Uninitialized constructor 00177 /*! By default, cache size is not limited. A limit can be set later 00178 using setMaxSize(). */ 00179 ImageCacheMinMax(); 00180 00181 //! Constructor 00182 /*! @param maxSize the maximum size of the cache. If this size is exceeded, 00183 images are popped off the front of the cache and disregarded for the 00184 computation of the mean. If maxSize = 0, the cache is not limited.*/ 00185 ImageCacheMinMax(uint maxSize); 00186 00187 //! return the max of the images currently in the cache 00188 Image<T> getMax() const; 00189 00190 //! return the max of the images currently in the cache 00191 Image<T> getMin() const; 00192 00193 protected: 00194 //! called when an image is added - here it's a nop-op 00195 virtual void doWhenAdd(const Image<T>& img); 00196 00197 //! called when an image is removed - here it's a no-op 00198 virtual void doWhenRemove(const Image<T>& img); 00199 }; 00200 00201 #endif 00202 00203 // ###################################################################### 00204 /* So things look consistent in everyone's emacs... */ 00205 /* Local Variables: */ 00206 /* indent-tabs-mode: nil */ 00207 /* End: */