V4.H

Go to the documentation of this file.
00001 /*!@file SceneUnderstanding/V4.H  */
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/V4.H $
00035 // $Id: V4.H 13765 2010-08-06 18:56:17Z lior $
00036 //
00037 
00038 #ifndef V4_H_DEFINED
00039 #define V4_H_DEFINED
00040 
00041 //#include "Image/OpenCVUtil.H"  // must be first to avoid conflicting defs of int64, uint64
00042 
00043 #include "Image/Image.H"
00044 #include "Image/ImageSet.H"
00045 #include "Image/Pixels.H"
00046 #include "Image/Layout.H"
00047 #include "Image/Point3D.H"
00048 #include "plugins/SceneUnderstanding/Camera.H"
00049 #include "plugins/SceneUnderstanding/V4d.H"
00050 #include "plugins/SceneUnderstanding/V2.H"
00051 #include "Simulation/SimEvents.H"
00052 #include "Simulation/SimModule.H"
00053 #include "Util/WorkThreadServer.H"
00054 
00055 #include <vector>
00056 #include <string>
00057 #include <google/dense_hash_map>
00058 using google::dense_hash_map;
00059 
00060 class V4 : public SimModule
00061 {
00062 public:
00063 
00064   enum GEON_TYPE  {TRIANGLE,SQUARE, CIRCLE};
00065 
00066   struct GeonState
00067   {
00068     Point3D<float> pos;
00069     float rot;
00070     double prob;
00071     double weight;
00072     GEON_TYPE geonType;
00073 
00074     Point3D<float> posSigma;
00075     float rotSigma;
00076 
00077     double distance(const GeonState &geonState)
00078     {
00079       double dist = 1.0e100;
00080       if (geonState.geonType == geonType)
00081       {
00082         double dPoint = pos.squdist(geonState.pos);
00083         double dRot = geonState.rot - rot;
00084         dRot = acos(cos(geonState.rot - rot));
00085 
00086         dist = sqrt(dPoint + (dRot*dRot));
00087       } else {
00088         dist =  1.0e100; //can not compare the distance across features for now
00089       }
00090 
00091       return dist;
00092     }
00093   };
00094 
00095   struct RTableEntry
00096   {
00097     Point2D<int> loc;
00098     float rot;
00099     V4d::NAF_TYPE featureType;
00100   };
00101 
00102   struct GHTAcc
00103   {
00104     Point2D<int> pos;
00105     int ang;
00106     int scale;
00107     GEON_TYPE geonType;
00108     float sum;
00109 
00110     bool operator<(const GHTAcc& acc) const
00111     {
00112       return sum < acc.sum;
00113     }
00114 
00115   };
00116 
00117   struct GeonOutline
00118   {
00119     std::vector<Point3D<float> > outline;
00120     std::vector<V4d::NAFState> NAFTemplate;
00121     std::vector<RTableEntry> rTable;
00122     GEON_TYPE geonType;
00123   };
00124 
00125   V4(OptionManager& mgr, const std::string& descrName = "V4",
00126       const std::string& tagName = "V4");
00127 
00128   //! Destructor
00129   ~V4();
00130 
00131   void buildRTables();
00132   void init(Dims dims);
00133 
00134   void setInput(const std::vector<V1::EdgeState> &edgesState);
00135   void setInput(const std::vector<V4d::NAFState> &nafStates);
00136 
00137   std::vector<V4::GeonState> getOutput();
00138   std::vector<V4d::NAFState> getBias();
00139 
00140   void showGeon(GeonOutline& object);
00141 
00142   void evolve();
00143 
00144   //! Align template so that center of mass is at 0,0
00145   void alignToCenterOfMass(GeonOutline& featureTemplate);
00146 
00147   Layout<PixRGB<byte> > getDebugImage();
00148 
00149   void getOutlineLikelihood(GeonState& geon);
00150   void getGeonLikelihood(GeonState& geon);
00151 
00152   double getLineProbability(const Point2D<int>& p1, const Point2D<int>& p2);
00153 
00154   void resampleParticles(std::vector<GeonState>& geonParticles);
00155   void resampleParticles2(std::vector<GeonState>& geonParticles);
00156   void proposeParticles(std::vector<GeonState>& geonParticles, const double Neff);
00157 
00158   std::vector<Point2D<int> > getImageGeonOutline(GeonState& geon);
00159 
00160   float evaluateGeonParticles(std::vector<GeonState>& geonParticles);
00161 
00162 
00163   void GHT(std::vector<GHTAcc>& accRet, GeonOutline& geonOutline);
00164   void normalizeAcc(std::vector<GHTAcc>& acc);
00165   void voteForFeature(Image<float>& acc, int angIdx, std::vector<RTableEntry>& rTable);
00166 
00167 
00168   Image<PixRGB<byte> > showParticles(const std::vector<GeonState>& geonParticles);
00169 
00170 protected:
00171   //! Callback for when a new ganglion output is ready
00172   SIMCALLBACK_DECLARE(V4, SimEventV2Output);
00173 
00174   //! Callback for when a new ganglion output is ready
00175   SIMCALLBACK_DECLARE(V4, SimEventV4dOutput);
00176 
00177 
00178   //! Callback for every time we have a user event
00179   SIMCALLBACK_DECLARE(V4, SimEventUserInput);
00180 
00181   //! Callback for every time we should save our outputs
00182   SIMCALLBACK_DECLARE(V4, SimEventSaveOutput);
00183 
00184   //! Should we show our debug info
00185   OModelParam<bool> itsShowDebug;
00186 
00187 private:
00188 
00189   std::vector<V1::EdgeState> itsEdgesState;
00190   dense_hash_map<int, V1::EdgeState> itsHashedEdgesState;
00191 
00192   std::vector<V4d::NAFState> itsNAFParticles;
00193 
00194   float itsMaxVal;
00195   int itsGHTAngStep;
00196   double itsBestProb;
00197   float itsObjectsDist; //Objects distance from the camera from the camera
00198 
00199 
00200   Camera itsCamera;
00201   std::vector<GeonState> itsGeonsParticles;
00202 
00203   std::vector<GeonOutline> itsGeons;
00204 
00205   //Images for debug
00206   Image<float> itsHoughSpaceImg;
00207 
00208   rutz::shared_ptr<WorkThreadServer> itsThreadServer;
00209 
00210   Image<PixRGB<byte> > itsDebugImg;
00211 
00212 };
00213 
00214 /* ############################### V4d sim events ######################## */
00215 class SimEventV4Output : public SimEvent
00216 {
00217 public:
00218   SimEventV4Output(SimModule* src, std::vector<V4::GeonState>& cellsOutput) :
00219     SimEvent(src), itsCells(cellsOutput)
00220   {}
00221 
00222   virtual ~SimEventV4Output(){}
00223   std::vector<V4::GeonState> getCells() { return itsCells; }
00224 
00225 private:
00226   const std::vector<V4::GeonState>& itsCells;
00227 };
00228 
00229 //class SimEventV4BiasOutput : public SimEvent
00230 //{
00231 //public:
00232 //  SimEventV4BiasOutput(SimModule* src, std::vector<V4d::NAFState>& cellsOutput) :
00233 //    SimEvent(src), itsCells(cellsOutput)
00234 //  {}
00235 //
00236 //  virtual ~SimEventV4BiasOutput(){}
00237 //  std::vector<V4d::NAFState> getCells() { return itsCells; }
00238 //
00239 //private:
00240 //  const std::vector<V4d::NAFState>& itsCells;
00241 //};
00242 
00243 
00244 
00245 // ######################################################################
00246 /* So things look consistent in everyone's emacs... */
00247 /* Local Variables: */
00248 /* indent-tabs-mode: nil */
00249 /* End: */
00250 
00251 #endif //
Generated on Sun May 8 08:05:32 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3