00001 /*!@file Channels/ScorrChannel.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/ScorrChannel.C $ 00035 // $Id: ScorrChannel.C 11118 2009-04-15 07:34:33Z itti $ 00036 // 00037 00038 #ifndef SCORRCHANNEL_C_DEFINED 00039 #define SCORRCHANNEL_C_DEFINED 00040 00041 #include "Channels/ScorrChannel.H" 00042 00043 #include "Channels/ChannelOpts.H" 00044 #include "Component/OptionManager.H" 00045 #include "Image/MathOps.H" 00046 #include "Util/MathFunctions.H" 00047 00048 // ###################################################################### 00049 // Scorr channel member definitions 00050 // ###################################################################### 00051 ScorrChannel::ScorrChannel(OptionManager& mgr) : 00052 SingleChannel(mgr, "Scorr", "scorr", SCORR, 00053 rutz::shared_ptr< PyrBuilder<float> >(NULL)), 00054 itsRadius(&OPT_ScorrChannelRadius, this), // see Channels/ChannelOpts.{H,C} 00055 itsMap() 00056 { } 00057 00058 // ###################################################################### 00059 ScorrChannel::~ScorrChannel() 00060 { } 00061 00062 // ###################################################################### 00063 bool ScorrChannel::outputAvailable() const 00064 { return itsMap.initialized(); } 00065 00066 // accumulate correlation from a point pp: 00067 #define ACCUMCORR if (itsMap.coordsOk(pp) && itsMask.getVal(pp) == 0) \ 00068 { ++n; itsMask.setVal(pp, 255); \ 00069 corr += corrpatch(ima, topleft1, patchdims, ima, Point2D<int>(pp.i<<lev, pp.j<<lev)); } 00070 00071 // ###################################################################### 00072 void ScorrChannel::doInput(const InputFrame& inframe) 00073 { 00074 const LevelSpec ls = itsLevelSpec.getVal(); 00075 Image<float> ima = inframe.grayFloat(); 00076 ASSERT(ls.levMin() == ls.levMax()); 00077 ASSERT(ls.delMin() == 0 && ls.delMax() == 0); 00078 ASSERT(ls.levMin() == ls.mapLevel()); 00079 ASSERT(ima.initialized()); 00080 ASSERT(itsRadius.getVal() >= 1); 00081 00082 const uint lev = ls.mapLevel(); 00083 const int siz = 1 << lev; 00084 const int mw = ima.getWidth() >> lev, mh = ima.getHeight() >> lev; 00085 itsMap.resize(mw, mh); 00086 Image<byte> itsMask(mw, mh, NO_INIT); 00087 const int radius = itsRadius.getVal(); 00088 const int r2 = radius * radius; 00089 00090 // FIXME: this channel will not work with images whose dims are not 00091 // multiple of patch dims... 00092 Dims patchdims(siz, siz); 00093 00094 // loop over the destination and compute spatial correlations. Code 00095 // here is similar to that in Image::drawDisk(): 00096 Image<float>::iterator dest = itsMap.beginw(); 00097 for (int j = 0; j < mh; ++j) 00098 for (int i = 0; i < mw; ++i) 00099 { 00100 double corr = 0.0; int n = 0; itsMask.clear(); 00101 00102 // our center patch's top-left corner: 00103 const Point2D<int> topleft1(i << lev, j << lev); 00104 00105 // go over a circle and get cross-correlations. First do the 00106 // two horizontal extremes: 00107 Point2D<int> pp(i - radius, j); ACCUMCORR; pp.i = i + radius; ACCUMCORR; 00108 00109 // now we draw one quarter of the circle and symmetrize it. 00110 // NOTE: in this algo like in Image::drawCircle() there is 00111 // some repetition (points that get drawn twice). Have a look at 00112 // http://www.cs.unc.edu/~mcmillan/comp136/Lecture7/circle.html 00113 // for possibly better algos. Here we don't want to count 00114 // those correlations several times, so we just use a mask to 00115 // keep track of those we already have counted: 00116 int bound1 = radius, bound2; 00117 for (int y = 1; y <= radius; ++y) 00118 { 00119 bound2 = bound1; 00120 bound1 = int(sqrtf(float(r2 - y*y))); 00121 for (int x = bound1; x <= bound2; ++x) 00122 { 00123 pp.j = j - y; 00124 pp.i = i - x; ACCUMCORR; 00125 pp.i = i + x; ACCUMCORR; 00126 pp.j = j + y; ACCUMCORR; 00127 pp.i = i - x; ACCUMCORR; 00128 } 00129 } 00130 00131 if (n > 0) 00132 { 00133 const double val = 1.0 - corr / double(n); 00134 if (val >= 0.0) *dest++ = float(val); else *dest++ = 0.0F; 00135 } 00136 else *dest++ = 0.0F; 00137 } 00138 } 00139 00140 // ###################################################################### 00141 Image<float> ScorrChannel::getOutput() 00142 { return itsMap; } 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 // SCORRCHANNEL_C_DEFINED