Geons2D.C

Go to the documentation of this file.
00001 /*!@file SceneUnderstanding/Geons2D.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: Lior Elazary <elazary@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/plugins/SceneUnderstanding/Geons2D.C $
00035 // $Id: Geons2D.C 13551 2010-06-10 21:56:32Z itti $
00036 //
00037 
00038 #ifndef Geons2D_C_DEFINED
00039 #define Geons2D_C_DEFINED
00040 
00041 #include "plugins/SceneUnderstanding/Geons2D.H"
00042 
00043 #include "Image/DrawOps.H"
00044 #include "Image/MathOps.H"
00045 #include "Image/Kernels.H"
00046 #include "Image/FilterOps.H"
00047 #include "Image/Convolutions.H"
00048 #include "Image/fancynorm.H"
00049 #include "Image/Point3D.H"
00050 #include "Simulation/SimEventQueue.H"
00051 #include "Neuro/EnvVisualCortex.H"
00052 #include "GUI/DebugWin.H"
00053 #include <math.h>
00054 #include <fcntl.h>
00055 #include <limits>
00056 #include <string>
00057 
00058 const ModelOptionCateg MOC_Geons2D = {
00059   MOC_SORTPRI_3,   "Geons2D-Related Options" };
00060 
00061 // Used by: SimulationViewerEyeMvt
00062 const ModelOptionDef OPT_Geons2DShowDebug =
00063   { MODOPT_ARG(bool), "Geons2DShowDebug", &MOC_Geons2D, OPTEXP_CORE,
00064     "Show debug img",
00065     "Geons2D-debug", '\0', "<true|false>", "false" };
00066 
00067 
00068 // ######################################################################
00069 Geons2D::Geons2D(OptionManager& mgr, const std::string& descrName,
00070     const std::string& tagName) :
00071   SimModule(mgr, descrName, tagName),
00072   SIMCALLBACK_INIT(SimEventGanglionOutput),
00073   SIMCALLBACK_INIT(SimEventSaveOutput),
00074   itsShowDebug(&OPT_Geons2DShowDebug, this)
00075 
00076 {
00077 
00078   itsMemStorage = cvCreateMemStorage(0);
00079 }
00080 
00081 // ######################################################################
00082 Geons2D::~Geons2D()
00083 {
00084   cvReleaseMemStorage(&itsMemStorage);
00085 
00086 }
00087 
00088 // ######################################################################
00089 void Geons2D::onSimEventGanglionOutput(SimEventQueue& q,
00090                                   rutz::shared_ptr<SimEventGanglionOutput>& e)
00091 {
00092   itsGeons2DCellsInput = e->getCells()[0];
00093   evolve();
00094 
00095   q.post(rutz::make_shared(new SimEventGeons2DOutput(this, itsGeons2DState)));
00096 
00097 }
00098 
00099 // ######################################################################
00100 void Geons2D::onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00101 {
00102   if (itsShowDebug.getVal())
00103     {
00104       // get the OFS to save to, assuming sinfo is of type
00105       // SimModuleSaveInfo (will throw a fatal exception otherwise):
00106       nub::ref<FrameOstream> ofs =
00107         dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00108       Layout<PixRGB<byte> > disp = getDebugImage();
00109       ofs->writeRgbLayout(disp, "Geons2D", FrameInfo("Geons2D", SRC_POS));
00110     }
00111 }
00112 
00113 
00114 void Geons2D::setBias(const Image<float> &biasImg)
00115 {
00116   //itsGeons2DCellsBias[0] = biasImg;
00117 
00118 }
00119 
00120 // ######################################################################
00121 void Geons2D::evolve()
00122 {
00123 
00124   Image<byte> in = itsGeons2DCellsInput;
00125   cvSmooth( img2ipl(in), img2ipl(in), CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected
00126   CvSeq* circles = cvHoughCircles( img2ipl(in),
00127       itsMemStorage,
00128       CV_HOUGH_GRADIENT,
00129       2, //2 times smaller resolution
00130       in.getHeight()/6, //min distance between centers of detected circles
00131       100, //higher thresh for canny
00132       10 ); //accum threshold
00133 
00134   itsGeons2DState.clear();
00135   for(int i = 0; i < circles->total; i++ )
00136   {
00137     float* p = (float*)cvGetSeqElem( circles, i );
00138     Geons2DState state(Point2D<int>(cvRound(p[0]),(int)cvRound(p[1])), cvRound(p[2]));
00139     itsGeons2DState.push_back(state);
00140   }
00141 
00142   cvClearMemStorage(itsMemStorage);
00143 }
00144 
00145 Layout<PixRGB<byte> > Geons2D::getDebugImage()
00146 {
00147   Layout<PixRGB<byte> > outDisp;
00148 
00149   Image<float> in = itsGeons2DCellsInput;
00150   inplaceNormalize(in, 0.0F, 255.0F);
00151 
00152   Image<float> perc = in; //(in.getDims(), ZEROS);
00153   LINFO("%lu circles", itsGeons2DState.size());
00154   for(uint i=0; i<itsGeons2DState.size(); i++)
00155   {
00156     Geons2DState geons2DState = itsGeons2DState[i];
00157     drawCircle(perc, geons2DState.pos, (int)geons2DState.radius, 255.0F, 2);
00158 
00159     //perc.setVal(edgeState.pos, edgeState.prob);
00160   }
00161 
00162   inplaceNormalize(perc, 0.0F, 255.0F);
00163 
00164 
00165   Layout<PixRGB<byte> > disp;
00166 
00167   outDisp = hcat(toRGB(Image<byte>(in)), toRGB(Image<byte>(perc)));
00168 
00169   return outDisp;
00170 
00171 }
00172 
00173 // ######################################################################
00174 /* So things look consistent in everyone's emacs... */
00175 /* Local Variables: */
00176 /* indent-tabs-mode: nil */
00177 /* End: */
00178 
00179 #endif
00180 
Generated on Sun May 8 08:05:31 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3