Normalize.C

Go to the documentation of this file.
00001 /*!@file Image/Normalize.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: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/Normalize.C $
00035 // $Id: Normalize.C 9412 2008-03-10 23:10:15Z farhan $
00036 //
00037 
00038 #ifndef IMAGE_NORMALIZE_C_DEFINED
00039 #define IMAGE_NORMALIZE_C_DEFINED
00040 
00041 #include "Image/Normalize.H"
00042 
00043 #include "Image/ColorOps.H"
00044 #include "Image/CutPaste.H"
00045 #include "Image/DrawOps.H"
00046 #include "Image/Image.H"
00047 #include "Image/MathOps.H"
00048 #include "Image/Pixels.H"
00049 #include "Image/Range.H"
00050 #include "Util/sformat.H"
00051 
00052 // ######################################################################
00053 Image<float> normalizeFloat(const Image<float>& src, const int flags)
00054 {
00055   if (flags == 0)
00056     return src;
00057 
00058   const Range<float> srcrng = rangeOf(src);
00059   Range<float> dstrng = srcrng;
00060 
00061   Image<float> nsrc = src;
00062   if (flags & FLOAT_NORM_0_255)
00063     {
00064       inplaceNormalize(nsrc, 0.0f, 255.0f);
00065       dstrng = Range<float>(0.0f, 255.0f);
00066     }
00067 
00068   Image<float> result = nsrc;
00069 
00070   if (flags & FLOAT_NORM_WITH_SCALE)
00071     {
00072       const int width = std::max(src.getWidth(), 144);
00073 
00074       result = Image<float>(width, src.getHeight() + 50, NO_INIT);
00075       result.clear(dstrng.min());
00076       inplacePaste(result, nsrc, Point2D<int>(0,0));
00077 
00078       writeText(result, Point2D<int>(0, src.getHeight()),
00079                 sformat("min: %g", srcrng.min()).c_str(),
00080                 // use the max,min values of the destination image
00081                 // range as the foreground and background colors for
00082                 // text rendering:
00083                 dstrng.max(), dstrng.min());
00084 
00085       writeText(result, Point2D<int>(0, src.getHeight()+25),
00086                 sformat("max: %g", srcrng.max()).c_str(),
00087                 dstrng.max(), dstrng.min());
00088     }
00089 
00090   return result;
00091 }
00092 
00093 // ######################################################################
00094 Image<PixRGB<float> >
00095 normalizeFloatRgb(const Image<PixRGB<float> >& src,
00096                   const int flags)
00097 {
00098   if (flags == 0)
00099     return src;
00100 
00101   float srcmin, srcmax;
00102   getMinMaxC(src, srcmin, srcmax);
00103   float dstmin = srcmin, dstmax = srcmax;
00104 
00105   Image<PixRGB<float> > nsrc = src;
00106   if (flags & FLOAT_NORM_0_255)
00107     {
00108       const float scale = 255.0f / srcmax;
00109       for (Image<PixRGB<float> >::iterator
00110              nptr = nsrc.beginw(), stop = nsrc.endw();
00111            nptr != stop; ++nptr)
00112         {
00113           *nptr = (*nptr - srcmin) * scale;
00114         }
00115       dstmin = 0.0f; dstmax = 255.0f;
00116     }
00117 
00118   Image<PixRGB<float> > result = nsrc;
00119 
00120   if (flags & FLOAT_NORM_WITH_SCALE)
00121     {
00122       const int width = std::max(src.getWidth(), 144);
00123 
00124       result = Image<PixRGB<float> >(width, src.getHeight() + 50, NO_INIT);
00125       result.clear(PixRGB<float>(dstmin));
00126       inplacePaste(result, nsrc, Point2D<int>(0,0));
00127 
00128       writeText(result, Point2D<int>(0, src.getHeight()),
00129                 sformat("min: %g", srcmin).c_str(),
00130                 // use the max,min values of the destination image
00131                 // range as the foreground and background colors for
00132                 // text rendering:
00133                 PixRGB<float>(dstmax),
00134                 PixRGB<float>(dstmin));
00135 
00136       writeText(result, Point2D<int>(0, src.getHeight()+25),
00137                 sformat("max: %g", srcmax).c_str(),
00138                 PixRGB<float>(dstmax),
00139                 PixRGB<float>(dstmin));
00140     }
00141 
00142   return result;
00143 }
00144 
00145 // ######################################################################
00146 /* So things look consistent in everyone's emacs... */
00147 /* Local Variables: */
00148 /* indent-tabs-mode: nil */
00149 /* End: */
00150 
00151 #endif // IMAGE_NORMALIZE_C_DEFINED
Generated on Sun May 8 08:05:14 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3