00001 /*!@file Image/ShapeOps.H Shape operations on Image 00002 */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: Rob Peters <rjpeters@klab.caltech.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/ShapeOps.H $ 00036 // $Id: ShapeOps.H 9412 2008-03-10 23:10:15Z farhan $ 00037 // 00038 00039 #ifndef IMAGE_SHAPEOPS_H_DEFINED 00040 #define IMAGE_SHAPEOPS_H_DEFINED 00041 00042 #include "Util/Assert.H" 00043 #include "Util/Types.H" 00044 00045 #include <string> 00046 00047 class Dims; 00048 template <class T> class Point2D; 00049 class Rectangle; 00050 00051 template <class T> class Image; 00052 template <class T> class PixRGB; 00053 00054 //! Compute quick and dirty scaling down of array. 00055 template <class T_or_RGB> 00056 Image<T_or_RGB> quickLocalAvg(const Image<T_or_RGB>& array, const int scale); 00057 00058 //! Compute local average of each 2x2 block and decimate by 2x2 00059 template <class T_or_RGB> 00060 Image<T_or_RGB> quickLocalAvg2x2(const Image<T_or_RGB>& array); 00061 00062 //! Compute quick and dirty scaling down of array. 00063 template <class T_or_RGB> 00064 Image<T_or_RGB> quickLocalMax(const Image<T_or_RGB>& array, const int scale); 00065 00066 //! Compute quick and dirty scaling down of array. 00067 template <class T_or_RGB> 00068 Image<T_or_RGB> quickLocalMin(const Image<T_or_RGB>& array, const int scale); 00069 00070 //! Dumb interpolation. 00071 template <class T_or_RGB> 00072 Image<T_or_RGB> quickInterpolate(const Image<T_or_RGB>& src, 00073 const int sfactor); 00074 00075 //! Double image size, using linear interpolation 00076 /*! This function doubles the size of an image in the x and y 00077 directions. It differs from intXY() in that some interpolation will 00078 be made to create the new pixels, instead of duplication or 00079 insersion of zeros. It also differs slightly from using rescale() in 00080 the weights used for the interpolation. Specifically, we obtain: 00081 00082 <PRE> 00083 ORIGa NEW1 ORIGb ... 00084 NEW2 NEW3 NEW4 ... 00085 ORIGc NEW5 ORIGd ... 00086 ... ... ... ... 00087 00088 where ORIGx are the pixels from the original image, and 00089 00090 NEW1 = (ORIGa + ORIGb) / 2 00091 NEW2 = (ORIGa + ORIGc) / 2 00092 NEW3 = (ORIGa + ORIGb + ORIGc + ORIGd) / 4 00093 NEW4 = (ORIGb + ORIGd) / 2 00094 NEW5 = (ORIGc + ORIGd) / 2 00095 </PRE> 00096 00097 Note that for the last row and column we assume something that is 00098 half-way between a duplication and a black border. */ 00099 template <class T_or_RGB> 00100 Image<T_or_RGB> interpolate(const Image<T_or_RGB>& src); 00101 00102 //! Scale to new width & height using bilinear interpolation. 00103 template <class T_or_RGB> 00104 Image<T_or_RGB> rescaleBilinear(const Image<T_or_RGB>& src, const Dims& dims); 00105 00106 //! Scale to new width & height using bilinear interpolation. 00107 template <class T_or_RGB> 00108 Image<T_or_RGB> rescaleBilinear(const Image<T_or_RGB>& src, int width, int height); 00109 00110 //! Scale to new width & height with no interpolation. 00111 template <class T_or_RGB> 00112 Image<T_or_RGB> rescaleNI(const Image<T_or_RGB>& src, const Dims& dims); 00113 00114 //! Scale to new width & height with no interpolation. 00115 template <class T_or_RGB> 00116 Image<T_or_RGB> rescaleNI(const Image<T_or_RGB>& src, int width, int height); 00117 00118 /// Different types of image rescaling functions 00119 enum RescaleType 00120 { 00121 RESCALE_SIMPLE_NOINTERP, 00122 RESCALE_SIMPLE_BILINEAR, 00123 RESCALE_FILTER_BOX, 00124 RESCALE_FILTER_TRIANGLE, 00125 RESCALE_FILTER_BELL, 00126 RESCALE_FILTER_BSPLINE, 00127 RESCALE_FILTER_HERMITE, 00128 RESCALE_FILTER_LANCZOS3, 00129 RESCALE_FILTER_MITCHELL 00130 }; 00131 00132 /// Convert a character mnemonic into a RescaleType; 00133 RescaleType getRescaleTypeFromChar(char ftype); 00134 00135 /// Convert RescaleType -> string 00136 std::string convertToString(RescaleType ftype); 00137 00138 /// Convert string -> RescaleType 00139 void convertFromString(const std::string& str1, RescaleType& ftype); 00140 00141 /// Generic image rescaling function with runtime-selectable rescaling algorithm 00142 template <class T_or_RGB> 00143 Image<T_or_RGB> rescale(const Image<T_or_RGB>& src, const Dims& newdims, 00144 RescaleType ftype = RESCALE_SIMPLE_BILINEAR); 00145 00146 /// Generic image rescaling function with runtime-selectable rescaling algorithm 00147 template <class T_or_RGB> 00148 Image<T_or_RGB> rescale(const Image<T_or_RGB>& src, 00149 const int width, const int height, 00150 RescaleType ftype = RESCALE_SIMPLE_BILINEAR); 00151 00152 00153 //! Scale to new width & height 00154 /*! Calls rescale() or rescaleNI() depending on value of interp */ 00155 template <class T_or_RGB> 00156 Image<T_or_RGB> rescaleOpt(const Image<T_or_RGB>& src, 00157 const Dims& dims, const bool interp); 00158 00159 //! Scale to new width & height 00160 /*! Calls rescale() or rescaleNI() depending on value of interp */ 00161 template <class T_or_RGB> 00162 Image<T_or_RGB> rescaleOpt(const Image<T_or_RGB>& src, int width, int height, 00163 const bool interp); 00164 00165 //! downscale an image using fancy widgets like anti-aliasing, resampling 00166 /*! Adapted from PhotoPNMtools by Boris Van Schooten boris@13thmonkey.org 00167 */ 00168 template <class T> 00169 Image<PixRGB<T> > downscaleFancy(const Image<PixRGB<T> >& src, 00170 int width, int height, int weighting_slope, 00171 bool no_weight_black); 00172 00173 //! Downsize using alternating lowpass/decimate operations. 00174 /*! The width used internally in the lowpass filters is given by 00175 filterWidth (default = 9). */ 00176 template <class T> 00177 Image<T> downSize(const Image<T>& src, const Dims& dims, 00178 const int filterWidth = 9); 00179 00180 //! Downsize using alternating lowpass/decimate operations. 00181 /*! The width used internally in the lowpass filters is given by 00182 filterWidth (default = 9). */ 00183 template <class T> 00184 Image<T> downSize(const Image<T>& src, const int width, const int height, 00185 const int filterWidth = 9); 00186 00187 //! Like downSize(), except image proportions don't have to match exactly. 00188 /*! This operation applies alternating lowpass/decimate as long as the 00189 result is larger than the desired size, and then finally does a 00190 bilinear interpolation to the final size. If the desired size is 00191 actually larger than the input size, then the image is simply 00192 upscaled with a bilinear interpolation. The advantage of using 00193 this operation over just using rescale() directly is that 00194 rescale() may introduce aliasing if an image is being downsized by 00195 several ocatves; successive lowpass/decimate operations ensure 00196 that total image energy is preserved. */ 00197 Image<float> downSizeClean(const Image<float>& src, const Dims& new_dims, 00198 const int filterWidth = 9); 00199 00200 //! Concatenate all image arrays, Nx arrays per line. 00201 /*! Also reshape each array to (destX, destY) if these are != -1. */ 00202 template <class T_or_RGB> 00203 Image<T_or_RGB> concatArray(const Image<T_or_RGB> arr[], 00204 const int nbarr, const int Nx, 00205 const int destX = -1, const int destY = -1); 00206 00207 //! Decimate in X and Y (take one every 'factor' pixels). 00208 /*! @param xfactor factor by which to decimate in the x direction 00209 00210 @param yfactor factor by which to decimate in the y direction; if 00211 this value is less than zero, then just take yfactor=xfactor 00212 */ 00213 template <class T_or_RGB> 00214 Image<T_or_RGB> decXY(const Image<T_or_RGB>& src, 00215 const int xfactor = 2, const int yfactor = -1); 00216 00217 //! Decimate in X (take one every 'factor' pixels). 00218 template <class T_or_RGB> 00219 Image<T_or_RGB> decX(const Image<T_or_RGB>& src, const int factor = 2); 00220 00221 //! Decimate in Y (take one every 'factor' pixels). 00222 template <class T_or_RGB> 00223 Image<T_or_RGB> decY(const Image<T_or_RGB>& src, const int factor = 2); 00224 00225 //! Blur and then decimate in Y. Equivalent to decY(sepFilter(src)). 00226 /*! This is logically equivalent to constructing a boxcar filter of 00227 size factor, then doing decY(sepFilter(src, [], filter)). However, 00228 this implementation is optimized to be much faster, especially if 00229 factor is large, since we avoid computing the filter results that 00230 would just be thrown away by the subsequent decimation. */ 00231 template <class T> 00232 Image<T> blurAndDecY(const Image<T>& src, const int factor); 00233 00234 //! Interpolate in X and Y (zero-pad if dupli == false, or duplicate). 00235 template <class T_or_RGB> 00236 Image<T_or_RGB> intXY(const Image<T_or_RGB>& src, const bool dupli); 00237 00238 //! Interpolate in X (zero-pad if dupli == false or duplicate). 00239 template <class T_or_RGB> 00240 Image<T_or_RGB> intX(const Image<T_or_RGB>& src, const bool dupli); 00241 00242 //! Interpolate in Y (zero-pad if dupli == false or duplicate). 00243 template <class T_or_RGB> 00244 Image<T_or_RGB> intY(const Image<T_or_RGB>& src, const bool dupli); 00245 00246 //! Zoom up an image by duplicating pixels. 00247 /*! Separate scaling factors can be given for the x and y dimensions. 00248 00249 @param src Input image. 00250 @param xzoom Zoom factor for x dimension (default 2). 00251 @param yzoom Zoom factor for y dimension. If negative (the default), 00252 then yzoom is taken to be the same as xzoom. 00253 */ 00254 template <class T_or_RGB> 00255 Image<T_or_RGB> zoomXY(const Image<T_or_RGB>& src, 00256 int xzoom = 2, int yzoom = -1); 00257 00258 //! Rotate an image about (x,y) by ang(in Radians), without interpolation 00259 template <class T_or_RGB> 00260 Image<T_or_RGB> rotate(const Image<T_or_RGB>& srcImg, 00261 const int x, const int y, 00262 const float ang); 00263 00264 //! Zoom by a factor numer/denom if numer>denom, else decimate by a factor of denom/numer 00265 template <class T> 00266 inline Image<T> zoomRational(const Image<T>& in, 00267 const uint numer, const uint denom) 00268 { 00269 ASSERT(numer > 0); 00270 ASSERT(denom > 0); 00271 00272 return (numer > denom) 00273 ? zoomXY(in, numer / denom) 00274 : decXY(in, denom / numer); 00275 } 00276 00277 // ###################################################################### 00278 /* So things look consistent in everyone's emacs... */ 00279 /* Local Variables: */ 00280 /* indent-tabs-mode: nil */ 00281 /* End: */ 00282 00283 #endif // !IMAGE_SHAPEOPS_H_DEFINED