MultiColorBandChannel.C

Go to the documentation of this file.
00001 /*!@file Channels/MultiColorBandChannel.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/MultiColorBandChannel.C $
00035 // $Id: MultiColorBandChannel.C 8195 2007-03-30 04:34:07Z rjpeters $
00036 //
00037 
00038 #ifndef MULTICOLORBANDCHANNEL_C_DEFINED
00039 #define MULTICOLORBANDCHANNEL_C_DEFINED
00040 
00041 #include "Channels/MultiColorBandChannel.H"
00042 
00043 #include "Channels/ChannelOpts.H"
00044 #include "Channels/ColorBandChannel.H"
00045 #include "Component/OptionManager.H"
00046 #include "Image/Pixels.H"
00047 #include "rutz/trace.h"
00048 
00049 #include <vector>
00050 
00051 // ######################################################################
00052 // MultiColorBandChannel member definitions:
00053 // ######################################################################
00054 
00055 // ######################################################################
00056 MultiColorBandChannel::MultiColorBandChannel(OptionManager& mgr) :
00057   ComplexChannel(mgr, "MultiColorBand", "multicolorband", COLBAND),
00058   itsNumBands(&OPT_NumColorBands, this),  // see Channels/ChannelOpts.{H,C}
00059   itsSatBands(&OPT_NumSatBands, this),  // see Channels/ChannelOpts.{H,C}
00060   itsHueSigma(&OPT_HueBandWidth, this),  // see Channels/ChannelOpts.{H,C}
00061   itsSatSigma(&OPT_SatBandWidth, this)  // see Channels/ChannelOpts.{H,C}
00062 {
00063 GVX_TRACE(__PRETTY_FUNCTION__);
00064   // let's create our subchannels (may be reconfigured if itsNumBands
00065   // gets changed):
00066   buildSubChans();
00067 }
00068 
00069 // ######################################################################
00070 void MultiColorBandChannel::buildSubChans()
00071 {
00072 GVX_TRACE(__PRETTY_FUNCTION__);
00073   // kill any subchans we may have had...
00074   this->removeAllSubChans();
00075 
00076   // let's instantiate our subchannels now that we know how many we
00077   // want. They will inherit the current values (typically
00078   // post-command-line parsing) of all their options as they are
00079   // constructed:
00080   LINFO("Using %d hue bands", itsNumBands.getVal());
00081   for (uint i = 0; i < itsNumBands.getVal(); i ++)
00082     {
00083       nub::ref<ColorBandChannel> chan
00084         (makeSharedComp(new ColorBandChannel(getManager(), i)));
00085 
00086       this->addSubChan(chan);
00087 
00088       // let's export options on the newly built channel:
00089       chan->exportOptions(MC_RECURSE);
00090     }
00091 
00092   LINFO("Using %d saturation bands", itsSatBands.getVal());
00093   for (uint i = 0; i < itsSatBands.getVal(); i ++)
00094     {
00095       nub::ref<ColorBandChannel> chan
00096         (makeSharedComp(new ColorBandChannel
00097                         (getManager(), itsNumBands.getVal()+i)));
00098 
00099       this->addSubChan(chan);
00100 
00101       // let's export options on the newly built channel:
00102       chan->exportOptions(MC_RECURSE);
00103     }
00104 }
00105 
00106 // ######################################################################
00107 void MultiColorBandChannel::paramChanged(ModelParamBase* const param,
00108                                          const bool valueChanged,
00109                                          ParamClient::ChangeStatus* status)
00110 {
00111 GVX_TRACE(__PRETTY_FUNCTION__);
00112   ComplexChannel::paramChanged(param, valueChanged, status);
00113 
00114   // if the param is our number of orientations and it has become
00115   // different from our number of channels, let's reconfigure:
00116   if (param == &itsNumBands &&
00117       numChans() != (itsNumBands.getVal() + itsSatBands.getVal()))
00118     buildSubChans();
00119 
00120   else if (param == &itsSatBands &&
00121            numChans() != (itsNumBands.getVal() + itsSatBands.getVal()))
00122     buildSubChans();
00123 }
00124 
00125 // ######################################################################
00126 MultiColorBandChannel::~MultiColorBandChannel()
00127 {
00128 GVX_TRACE(__PRETTY_FUNCTION__);
00129 }
00130 
00131 // ######################################################################
00132 ColorBandChannel& MultiColorBandChannel::band(const uint idx) const
00133 {
00134 GVX_TRACE(__PRETTY_FUNCTION__);
00135   // Since we are dynamic_cast'ing a reference, this operation will either
00136   // succeed or throw an exception.
00137   return *(dynCast<ColorBandChannel>(subChan(idx)));
00138 }
00139 
00140 // ######################################################################
00141 void MultiColorBandChannel::doInput(const InputFrame& inframe)
00142 {
00143 GVX_TRACE(__PRETTY_FUNCTION__);
00144   ASSERT(inframe.colorFloat().initialized());
00145   /*
00146     in the old implementation, we only supported 3 bands: r, g, b. in
00147     the new implementation, we allow several bands, i.e., distributed
00148     coding of color.  ASSERT(numChans() == 3);
00149   */
00150   // create bands with different ranges of hues and saturation
00151   uint numHue = itsNumBands.getVal(), numSat = itsSatBands.getVal();
00152   std::vector<Image<float> > sub_input(numHue + numSat);
00153   Image<float>::iterator bptr[sub_input.size()];
00154   // initialize the pointers
00155   for (uint i = 0; i < sub_input.size(); ++i) {
00156     sub_input[i].resize(inframe.getDims(), true);
00157     bptr[i] = sub_input[i].beginw();
00158   }
00159   // some constants for the loop
00160   float hue_range = 360/numHue; // range of each band
00161   float c1 = 10000.0f / (sqrt(6.28) * itsHueSigma.getVal());
00162   float c2 = 2.0f * itsHueSigma.getVal() * itsHueSigma.getVal();
00163   float c3 = 10000.0f / (sqrt(6.28) * itsSatSigma.getVal());
00164   float c4 = 2.0f * itsSatSigma.getVal() * itsSatSigma.getVal();
00165   // get the hue and saturation at all pixels
00166   Image<PixRGB<float> >::const_iterator aptr = inframe.colorFloat().begin();
00167   Image<PixRGB<float> >::const_iterator astop = inframe.colorFloat().end();
00168   while (aptr != astop)
00169     {
00170       float h, s, v;
00171       PixHSV<float>(*aptr).getHSV(h, s, v);
00172       aptr++;
00173       // what is the response of each band to this hue?
00174       for (uint i = 0; i < numHue; i++){
00175         if (v == 0.0f || s == 0.0f)
00176           *(bptr[i]) = 0.0f; // response to black and white is zero
00177         else {
00178           float mean = i * hue_range;
00179           float dist = h-mean;
00180           if (dist > 180) dist = 360 - dist;
00181           else if (dist < -180) dist = dist + 360;
00182           *(bptr[i]) = c1 * exp(-1.0f * dist * dist / c2);
00183         }
00184         bptr[i] =  bptr[i] + 1;
00185       }
00186       // what is the response of each band to this saturation?
00187       for (uint i = 0; i < numSat; i++){
00188         if (v == 0.0f)
00189           *(bptr[numHue+i]) = 0.0f; // response to black is zero
00190         else {
00191           float mean = (i+0.5)/numSat;
00192           float dist = s-mean;
00193           *(bptr[numHue+i]) = c3 * exp(-1.0f * dist * dist / c4);
00194         }
00195         bptr[numHue+i] =  bptr[numHue+i] + 1;
00196       }
00197     }
00198   for (uint i = 0; i < sub_input.size(); ++i)
00199     subChan(i)->input(InputFrame::fromGrayFloat
00200                       (&sub_input[i], inframe.time(),
00201                        &inframe.clipMask(), inframe.pyrCache()));
00202 }
00203 
00204 // ######################################################################
00205 /* So things look consistent in everyone's emacs... */
00206 /* Local Variables: */
00207 /* indent-tabs-mode: nil */
00208 /* End: */
00209 
00210 #endif // MULTICOLORBANDCHANNEL_C_DEFINED
Generated on Sun May 8 08:04:41 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3