LocalParts.C

Go to the documentation of this file.
00001 /*!@file SceneUnderstanding/LocalParts.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/LocalParts.C $
00035 // $Id: LocalParts.C 13701 2010-07-27 01:08:12Z lior $
00036 //
00037 
00038 #ifndef LocalParts_C_DEFINED
00039 #define LocalParts_C_DEFINED
00040 
00041 #include "Image/DrawOps.H"
00042 #include "Image/MathOps.H"
00043 //#include "Image/OpenCVUtil.H"
00044 #include "Image/Kernels.H"
00045 #include "Image/FilterOps.H"
00046 #include "Image/Transforms.H"
00047 #include "Image/fancynorm.H"
00048 #include "Image/Convolutions.H"
00049 #include "Image/MatrixOps.H"
00050 #include "Simulation/SimEventQueue.H"
00051 #include "plugins/SceneUnderstanding/LocalParts.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_LocalParts = {
00059   MOC_SORTPRI_3,   "LocalParts-Related Options" };
00060 
00061 // Used by: SimulationViewerEyeMvt
00062 const ModelOptionDef OPT_LocalPartsShowDebug =
00063   { MODOPT_ARG(bool), "LocalPartsShowDebug", &MOC_LocalParts, OPTEXP_CORE,
00064     "Show debug img",
00065     "localparts-debug", '\0', "<true|false>", "false" };
00066 
00067 //Define the inst function name
00068 SIMMODULEINSTFUNC(LocalParts);
00069 
00070 
00071 // ######################################################################
00072 LocalParts::LocalParts(OptionManager& mgr, const std::string& descrName,
00073     const std::string& tagName) :
00074   SimModule(mgr, descrName, tagName),
00075   SIMCALLBACK_INIT(SimEventContoursOutput),
00076   SIMCALLBACK_INIT(SimEventSaveOutput),
00077   SIMCALLBACK_INIT(SimEventUserInput),
00078   itsShowDebug(&OPT_LocalPartsShowDebug, this)
00079 {
00080   initRandomNumbers();
00081 }
00082 
00083 // ######################################################################
00084 LocalParts::~LocalParts()
00085 {
00086 }
00087 
00088 // ######################################################################
00089 void LocalParts::onSimEventUserInput(SimEventQueue& q, rutz::shared_ptr<SimEventUserInput>& e)
00090 {
00091 
00092   LINFO("Got event --%s-- %ix%i key=%i",
00093       e->getWinName(),
00094       e->getMouseClick().i,
00095       e->getMouseClick().j,
00096       e->getKey());
00097 
00098   if (strcmp(e->getWinName(), "LocalParts"))
00099     return;
00100 
00101   switch(e->getKey())
00102   {
00103     case 111: //98: //111: //up
00104       break;
00105     case 116: //104: //116: //down
00106       break;
00107     case 113: //100: //113: //left
00108       break;
00109     case 114: //102: //114: //right
00110       break;
00111     case 21: //=
00112       break;
00113     case 20: //-
00114       break;
00115     case 38: //a
00116       break;
00117     case 52: //z
00118       break;
00119     case 39: //s
00120       break;
00121     case 53: //x
00122       break;
00123     case 40: //d
00124       break;
00125     case 54: //c
00126       break;
00127     case 10: //1
00128       break;
00129     case 24: //q
00130       break;
00131     case 11: //2
00132       break;
00133     case 25: //w
00134       break;
00135     case 12: //3
00136       break;
00137     case 26: //e
00138       break;
00139     case 13: //4
00140       break;
00141     case 27: //r
00142       break;
00143     case 14: //5
00144       break;
00145     case 28: //t
00146       break;
00147     case 15: //6
00148       break;
00149     case 29: //y
00150       break;
00151   }
00152 
00153   evolve(q);
00154 
00155 }
00156 
00157 
00158 void LocalParts::onSimEventContoursOutput(SimEventQueue& q, rutz::shared_ptr<SimEventContoursOutput>& e)
00159 {
00160 
00161   itsContours = e->getContours();
00162 
00163   //for(uint i=0; i<itsContours.size(); i++)
00164   //{
00165 
00166   //  Image<PixRGB<byte> > tmp(320,240,ZEROS);
00167 
00168 
00169   //  for(uint j=0; j<itsContours[i].size(); j++)
00170   //  {
00171   //    tmp.setVal(itsContours[i].points[j], PixRGB<byte>(255,0,0));
00172   //    float ori = itsContours[i].ori[j];
00173   //    if (ori < 0) ori += M_PI;
00174   //    if (ori >= M_PI) ori -= M_PI;
00175   //  }
00176 
00177   //  std::vector<Point2D<int> > res = approxPolyDP(itsContours[i].points, 3);
00178 
00179   //  for(uint j=0; j<res.size()-2; j++)
00180   //  {
00181   //    Image<PixRGB<byte> > tmp2 = tmp;
00182   //    drawLine(tmp2, res[j+1], res[j], PixRGB<byte>(0,255,0));
00183   //    drawLine(tmp2, res[j+1], res[j+2], PixRGB<byte>(0,255,0));
00184   //    double ang = angle(res[j+1], res[j], res[j+2]);
00185   //    LINFO("Ang %f %f", acos(ang), acos(ang)*180/M_PI);
00186   //    //drawCircle(tmp, res[j], 3, PixRGB<byte>(0,255,0));
00187   //    SHOWIMG(tmp2);
00188   //  }
00189 
00190   //}
00191 
00192   /*
00193   Dims dims(320,240);
00194 
00195   itsLinesMag = Image<float>(dims, ZEROS);
00196   itsLinesOri = Image<float>(dims, ZEROS);
00197   for(uint i=0; i<itsContours.size(); i++)
00198   {
00199     for(uint j=0; j<itsContours[i].size(); j++)
00200     {
00201       itsLinesMag.setVal(itsContours[i].points[j], 255.0);
00202 
00203       float ori = itsContours[i].ori[j];
00204       if (ori < 0) ori += M_PI;
00205       if (ori >= M_PI) ori -= M_PI;
00206       itsLinesOri.setVal(itsContours[i].points[j], ori);
00207     }
00208   }
00209   inplaceNormalize(itsLinesMag, 0.0F, 100.0F);
00210   itsEdgesDT = chamfer34(itsLinesMag, 10.0F);
00211 
00212   int numOfEntries = 60;
00213   double D = M_PI/numOfEntries;
00214   //seperate each ori into its own map
00215   itsOriEdgesDT = ImageSet<float>(numOfEntries, itsLinesMag.getDims(), ZEROS);
00216   for(uint i=0; i<itsLinesMag.size(); i++)
00217   {
00218     if (itsLinesMag[i] > 0)
00219     {
00220       float ori = itsLinesOri[i];
00221       int oriIdx = (int)floor(ori/D);
00222       itsOriEdgesDT[oriIdx].setVal(i,itsLinesMag[i]);
00223     }
00224   }
00225 
00226   //Take the distance transform of each ori bin
00227   for(uint i=0; i<itsOriEdgesDT.size(); i++)
00228     itsOriEdgesDT[i] = chamfer34(itsOriEdgesDT[i], 30.0F);
00229 
00230   evolve(q);
00231   */
00232 }
00233 
00234 
00235 // ######################################################################
00236 void LocalParts::onSimEventSaveOutput(SimEventQueue& q, rutz::shared_ptr<SimEventSaveOutput>& e)
00237 {
00238   if (itsShowDebug.getVal())
00239     {
00240       // get the OFS to save to, assuming sinfo is of type
00241       // SimModuleSaveInfo (will throw a fatal exception otherwise):
00242       nub::ref<FrameOstream> ofs =
00243         dynamic_cast<const SimModuleSaveInfo&>(e->sinfo()).ofs;
00244       Layout<PixRGB<byte> > disp = getDebugImage(q);
00245       if (disp.initialized())
00246         ofs->writeRgbLayout(disp, "LocalParts", FrameInfo("LocalParts", SRC_POS));
00247     }
00248 }
00249 
00250 
00251 // ######################################################################
00252 void LocalParts::evolve(SimEventQueue& q)
00253 {
00254 
00255   /*
00256   //std::vector<SurfaceState> surfaces = proposeSurfaces();
00257   std::vector<SurfaceState> surfaces = itsSurfaces;
00258 
00259   for(uint i=0; i<surfaces.size(); i++)
00260   {
00261     calcSurfaceLikelihood(surfaces[i]);
00262   }
00263   itsProposals = surfaces;
00264 
00265   //itsSurfaces = surfaces;
00266 
00267   //sort by prob
00268   //Update the surfaces
00269   for(uint i=0; i<surfaces.size(); i++)
00270   {
00271     if (surfaces[i].prob > itsSurfaces[i].prob)
00272       itsSurfaces[i] = surfaces[i];
00273   }
00274 
00275   q.post(rutz::make_shared(new SimEventLocalPartsOutput(this, itsSurfaces)));
00276 
00277 
00278   std::vector<Contours::Contour> contourBias; 
00279 
00280   for(uint sid=0; sid<itsSurfaces.size(); sid++)
00281   {
00282     for(uint cid=0; cid<itsContours.size(); cid++)
00283     {
00284       Image<float> tmp(320,240,ZEROS);
00285 
00286 
00287       drawSuperquadric(tmp,
00288           Point2D<int>(itsSurfaces[sid].pos),
00289           itsSurfaces[sid].a, itsSurfaces[sid].b, itsSurfaces[sid].e, 
00290           2.0F,
00291           itsSurfaces[sid].rot, itsSurfaces[sid].k1, itsSurfaces[sid].k2,
00292           itsSurfaces[sid].start,itsSurfaces[sid].end,3);
00293 
00294       Contours::Contour& contour = itsContours[cid];
00295       for(uint i=0; i<contour.size(); i++)
00296         tmp.setVal(contour.points[i], tmp.getVal(contour.points[i]) - 1.0F);
00297 
00298       //Find the overlap and mark that contour with 
00299       int overCnt = 0;
00300       Contours::Contour biasContour;
00301       for(int y=0; y<tmp.getHeight(); y++)
00302         for(int x=0; x<tmp.getWidth(); x++)
00303         {
00304           if (tmp.getVal(x,y) == 1.0F)
00305             overCnt++;
00306 
00307           if (tmp.getVal(x,y) > 0)
00308             biasContour.points.push_back(Point2D<int>(x,y));
00309         }
00310 
00311       if (overCnt > 20)
00312         contourBias.push_back(biasContour);
00313 
00314     }
00315 
00316   }
00317 
00318   q.post(rutz::make_shared(new SimEventContoursBias(this, contourBias)));
00319 
00320   */
00321 
00322 }
00323 
00324 std::vector<LocalParts::PartState> LocalParts::proposeParts()
00325 {
00326   std::vector<PartState> parts;
00327 
00328   return parts;
00329 }
00330 
00331 void LocalParts::calcPartLikelihood(PartState& part)
00332 {
00333 }
00334 
00335 Layout<PixRGB<byte> > LocalParts::getDebugImage(SimEventQueue& q)
00336 {
00337   Layout<PixRGB<byte> > outDisp;
00338 
00339   return outDisp;
00340 
00341 }
00342 
00343 
00344 // ######################################################################
00345 /* So things look consistent in everyone's emacs... */
00346 /* Local Variables: */
00347 /* indent-tabs-mode: nil */
00348 /* End: */
00349 
00350 #endif
00351 
Generated on Sun May 8 08:41:09 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3