V4d.H

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