VisualCortex.C

Go to the documentation of this file.
00001 /*!@file Neuro/VisualCortex.C Implementation for visual cortex class */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // 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: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Neuro/VisualCortex.C $
00035 // $Id: VisualCortex.C 13065 2010-03-28 00:01:00Z itti $
00036 //
00037 
00038 #include "Neuro/VisualCortex.H"
00039 
00040 #include "Channels/ChannelOpts.H"
00041 #include "Channels/RawVisualCortex.H"
00042 #include "Channels/IntegerRawVisualCortex.H"
00043 #include "Channels/ChannelFacets.H"
00044 #include "Component/GlobalOpts.H"
00045 #include "Component/OptionManager.H"
00046 #include "Component/ParamMap.H"
00047 #include "Image/ColorOps.H"   // for luminance()
00048 #include "Image/MathOps.H"    // for distance()
00049 #include "Image/Pixels.H"
00050 #include "Image/PyramidOps.H"
00051 #include "Image/ShapeOps.H"
00052 #include "Image/Transforms.H" // for chamfer34()
00053 #include "Media/MediaSimEvents.H"
00054 #include "Neuro/EnvVisualCortex.H"
00055 #include "Neuro/NeuroOpts.H"
00056 #include "Neuro/NeuroSimEvents.H"
00057 #include "Neuro/WTAwinner.H"
00058 #include "Simulation/SimEventQueue.H"
00059 #include "Transport/FrameInfo.H"
00060 #include "Transport/FrameOstream.H"
00061 #include "Util/sformat.H"
00062 #include "rutz/mutex.h"
00063 #include "rutz/trace.h"
00064 
00065 #include <algorithm>
00066 #include <cctype>
00067 #include <cstdio>
00068 #include <vector>
00069 
00070 // ######################################################################
00071 // ######################################################################
00072 VisualCortex::VisualCortex(OptionManager& mgr,
00073                            const std::string& descrName,
00074                            const std::string& tag) :
00075   SimModule(mgr, descrName, tag)
00076 {
00077 GVX_TRACE(__PRETTY_FUNCTION__);
00078 }
00079 
00080 // ######################################################################
00081 VisualCortex::~VisualCortex()
00082 {
00083 GVX_TRACE(__PRETTY_FUNCTION__);
00084 }
00085 
00086 // ######################################################################
00087 // ######################################################################
00088 VisualCortexStd::VisualCortexStd(OptionManager& mgr,
00089                            const std::string& descrName,
00090                            const std::string& tag) :
00091   VisualCortex(mgr, descrName, tag),
00092   SIMCALLBACK_INIT(SimEventRetinaImage),
00093   SIMCALLBACK_INIT(SimEventSaveOutput),
00094   SIMREQHANDLER_INIT(SimReqVCXfeatures),
00095   SIMREQHANDLER_INIT(SimReqVCXmaps),
00096   SIMREQHANDLER_INIT(SimReqVCXchanVis),
00097   itsVCX(new RawVisualCortex(mgr))
00098 {
00099 GVX_TRACE(__PRETTY_FUNCTION__);
00100  addSubComponent(itsVCX);
00101 }
00102 
00103 // ######################################################################
00104 VisualCortexStd::VisualCortexStd(OptionManager& mgr,
00105                                  const std::string& descrName,
00106                                  const std::string& tag, nub::ref<RawVisualCortex> vcx) :
00107   VisualCortex(mgr, descrName, tag),
00108   SIMCALLBACK_INIT(SimEventRetinaImage),
00109   SIMCALLBACK_INIT(SimEventSaveOutput),
00110   SIMREQHANDLER_INIT(SimReqVCXfeatures),
00111   SIMREQHANDLER_INIT(SimReqVCXmaps),
00112   SIMREQHANDLER_INIT(SimReqVCXchanVis),
00113   itsVCX(vcx)
00114 {
00115 GVX_TRACE(__PRETTY_FUNCTION__);
00116  addSubComponent(itsVCX);
00117 }
00118 
00119 // ######################################################################
00120 VisualCortexStd::~VisualCortexStd()
00121 {
00122 GVX_TRACE(__PRETTY_FUNCTION__);
00123 }
00124 
00125 // ######################################################################
00126 void VisualCortexStd::onSimEventRetinaImage(SimEventQueue& q, rutz::shared_ptr<SimEventRetinaImage>& e)
00127 {
00128   // process the retina image:
00129   itsVCX->input(e->frame());
00130 
00131   // post an event with our output:
00132   q.post(rutz::make_shared(new SimEventVisualCortexOutput(this, itsVCX->getOutput())));
00133 }
00134 
00135 // ######################################################################
00136 void VisualCortexStd::handleSimReqVCXfeatures(SimEventQueue& q, rutz::shared_ptr<SimReqVCXfeatures>& r)
00137 {
00138   itsVCX->getFeatures(r->loc(), r->features());
00139 }
00140 
00141 // ######################################################################
00142 void VisualCortexStd::handleSimReqVCXmaps(SimEventQueue& q, rutz::shared_ptr<SimReqVCXmaps>& r)
00143 {
00144   r->populateChannelMaps(itsVCX.get());
00145 }
00146 
00147 // ######################################################################
00148 void VisualCortexStd::handleSimReqVCXchanVis(SimEventQueue& q, rutz::shared_ptr<SimReqVCXchanVis>& r)
00149 {
00150   LINFO("Triggering visitor %s", r->toString().c_str());
00151   r->preProcessing(itsVCX.get());
00152   itsVCX->accept(*(r->visitor()));
00153   r->postProcessing(itsVCX.get());
00154 }
00155 
00156 // ######################################################################
00157 void VisualCortexStd::onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00158 {
00159   // get the OFS to save to, assuming sinfo is of type
00160   // SimModuleSaveInfo (will throw a fatal exception otherwise):
00161   nub::ref<FrameOstream> ofs = dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00162 
00163   itsVCX->saveResults(ofs);
00164 }
00165 
00166 // ######################################################################
00167 // ######################################################################
00168 VisualCortexInt::VisualCortexInt(OptionManager& mgr,
00169                            const std::string& descrName,
00170                            const std::string& tag) :
00171   VisualCortex(mgr, descrName, tag),
00172   SIMCALLBACK_INIT(SimEventRetinaImage),
00173   SIMCALLBACK_INIT(SimEventSaveOutput),
00174   SIMREQHANDLER_INIT(SimReqVCXfeatures),
00175   SIMREQHANDLER_INIT(SimReqVCXmaps),
00176   SIMREQHANDLER_INIT(SimReqVCXchanVis),
00177   itsIME(new IntegerMathEngine(mgr)),
00178   itsVCX(new IntegerRawVisualCortex(mgr, itsIME))
00179 {
00180 GVX_TRACE(__PRETTY_FUNCTION__);
00181  addSubComponent(itsIME);
00182  addSubComponent(itsVCX);
00183 
00184  // set some defaults:
00185  mgr.setOptionValString(&OPT_MaxNormType, "Maxnorm");
00186  mgr.setOptionValString(&OPT_DirectionChannelLowThresh, "0");
00187  mgr.setOptionValString(&OPT_IntChannelScaleBits, "16");
00188  mgr.setOptionValString(&OPT_IntMathLowPass5, "lp5optim");
00189  mgr.setOptionValString(&OPT_IntMathLowPass9, "lp9optim");
00190  mgr.setOptionValString(&OPT_RawVisualCortexOutputFactor, "1.0e-12");
00191 }
00192 
00193 // ######################################################################
00194 VisualCortexInt::VisualCortexInt(OptionManager& mgr,
00195                                  const std::string& descrName,
00196                                  const std::string& tag, nub::ref<IntegerMathEngine> ime,
00197                                  nub::ref<IntegerRawVisualCortex> vcx) :
00198   VisualCortex(mgr, descrName, tag),
00199   SIMCALLBACK_INIT(SimEventRetinaImage),
00200   SIMCALLBACK_INIT(SimEventSaveOutput),
00201   SIMREQHANDLER_INIT(SimReqVCXfeatures),
00202   SIMREQHANDLER_INIT(SimReqVCXmaps),
00203   SIMREQHANDLER_INIT(SimReqVCXchanVis),
00204   itsIME(ime),
00205   itsVCX(vcx),
00206   itsPcache()
00207 {
00208 GVX_TRACE(__PRETTY_FUNCTION__);
00209  addSubComponent(itsIME);
00210  addSubComponent(itsVCX);
00211 
00212  // set some defaults:
00213  mgr.setOptionValString(&OPT_MaxNormType, "Maxnorm");
00214  mgr.setOptionValString(&OPT_DirectionChannelLowThresh, "0");
00215  mgr.setOptionValString(&OPT_IntChannelScaleBits, "16");
00216  mgr.setOptionValString(&OPT_IntMathLowPass5, "lp5optim");
00217  mgr.setOptionValString(&OPT_IntMathLowPass9, "lp9optim");
00218  mgr.setOptionValString(&OPT_RawVisualCortexOutputFactor, "1.0e-12");
00219 }
00220 
00221 // ######################################################################
00222 VisualCortexInt::~VisualCortexInt()
00223 {
00224 GVX_TRACE(__PRETTY_FUNCTION__);
00225 }
00226 
00227 // ######################################################################
00228 void VisualCortexInt::onSimEventRetinaImage(SimEventQueue& q, rutz::shared_ptr<SimEventRetinaImage>& e)
00229 {
00230   // process the retina image:
00231   IntegerInput inp = IntegerInput::fromRgb(e->frame().colorByte(), itsIME->getNbits());
00232   itsVCX->inputInt(inp, q.now(), &itsPcache);
00233 
00234   // post an event with our output:
00235   Image<float> fout = itsVCX->getOutput();
00236   q.post(rutz::make_shared(new SimEventVisualCortexOutput(this, fout)));
00237 }
00238 
00239 // ######################################################################
00240 void VisualCortexInt::handleSimReqVCXfeatures(SimEventQueue& q, rutz::shared_ptr<SimReqVCXfeatures>& r)
00241 {
00242   itsVCX->getFeatures(r->loc(), r->features());
00243 }
00244 
00245 // ######################################################################
00246 void VisualCortexInt::handleSimReqVCXmaps(SimEventQueue& q, rutz::shared_ptr<SimReqVCXmaps>& r)
00247 {
00248   r->populateChannelMaps(itsVCX.get());
00249 }
00250 
00251 // ######################################################################
00252 void VisualCortexInt::handleSimReqVCXchanVis(SimEventQueue& q, rutz::shared_ptr<SimReqVCXchanVis>& r)
00253 {
00254   LFATAL("Not supported yet...");
00255   /*
00256   r->preProcessing(itsVCX.get());
00257   itsVCX->accept(*(r->visitor()));
00258   r->postProcessing(itsVCX.get());
00259   */
00260 }
00261 
00262 // ######################################################################
00263 void VisualCortexInt::onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00264 {
00265   // get the OFS to save to, assuming sinfo is of type
00266   // SimModuleSaveInfo (will throw a fatal exception otherwise):
00267   nub::ref<FrameOstream> ofs = dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00268 
00269   itsVCX->saveResults(ofs);
00270 }
00271 
00272 // ######################################################################
00273 // ######################################################################
00274 VisualCortexEnv::VisualCortexEnv(OptionManager& mgr,
00275                            const std::string& descrName,
00276                            const std::string& tag) :
00277   VisualCortex(mgr, descrName, tag),
00278   SIMCALLBACK_INIT(SimEventRetinaImage),
00279   SIMCALLBACK_INIT(SimEventSaveOutput),
00280   SIMREQHANDLER_INIT(SimReqVCXfeatures),
00281   SIMREQHANDLER_INIT(SimReqVCXmaps),
00282   SIMREQHANDLER_INIT(SimReqVCXchanVis),
00283   itsVCX(new EnvVisualCortexFloat(mgr))
00284 {
00285 GVX_TRACE(__PRETTY_FUNCTION__);
00286  addSubComponent(itsVCX);
00287 }
00288 
00289 // ######################################################################
00290 VisualCortexEnv::~VisualCortexEnv()
00291 {
00292 GVX_TRACE(__PRETTY_FUNCTION__);
00293 }
00294 
00295 // ######################################################################
00296 void VisualCortexEnv::onSimEventRetinaImage(SimEventQueue& q, rutz::shared_ptr<SimEventRetinaImage>& e)
00297 {
00298   // process the retina image:
00299   itsVCX->input(e->frame().colorByte());
00300 
00301   // post an event with our output:
00302   q.post(rutz::make_shared(new SimEventVisualCortexOutput(this, itsVCX->getVCXmap())));
00303 }
00304 
00305 // ######################################################################
00306 void VisualCortexEnv::handleSimReqVCXfeatures(SimEventQueue& q, rutz::shared_ptr<SimReqVCXfeatures>& r)
00307 {
00308   LFATAL("Not supported yet...");
00309   /*
00310   itsVCX->getFeatures(r->loc(), r->features());
00311   */
00312 }
00313 
00314 // ######################################################################
00315 void VisualCortexEnv::handleSimReqVCXmaps(SimEventQueue& q, rutz::shared_ptr<SimReqVCXmaps>& r)
00316 {
00317   r->populateChannelMaps(itsVCX.get());
00318 }
00319 
00320 // ######################################################################
00321 void VisualCortexEnv::handleSimReqVCXchanVis(SimEventQueue& q, rutz::shared_ptr<SimReqVCXchanVis>& r)
00322 {
00323   LFATAL("Not supported yet...");
00324   /*
00325   r->preProcessing(itsVCX.get());
00326   itsVCX->accept(*(r->visitor()));
00327   r->postProcessing(itsVCX.get());
00328   */
00329 }
00330 
00331 // ######################################################################
00332 void VisualCortexEnv::onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00333 {
00334   // get the OFS to save to, assuming sinfo is of type
00335   // SimModuleSaveInfo (will throw a fatal exception otherwise):
00336   nub::ref<FrameOstream> ofs = dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00337   /*
00338   itsVCX->saveResults(ofs);
00339   */
00340 }
00341 
00342 // ######################################################################
00343 /* So things look consistent in everyone's emacs... */
00344 /* Local Variables: */
00345 /* indent-tabs-mode: nil */
00346 /* End: */
Generated on Sun May 8 08:05:26 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3