GHough.H

00001 /*!@file SceneUnderstanding/GHough.H  Generalized Hough */
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/FeatureMatching/GHough.H $
00035 // $Id: GHough.H 13815 2010-08-22 17:58:48Z lior $
00036 //
00037 
00038 #ifndef GHough_H_DEFINED
00039 #define GHough_H_DEFINED
00040 
00041 #include "Util/Types.H"
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "Image/ImageSet.H"
00045 #include "Image/Point2D.H"
00046 #include "Image/Point3D.H"
00047 #include "Util/CpuTimer.H"
00048 #include "Util/JobServer.H"
00049 #include "Util/JobWithSemaphore.H"
00050 #include "Util/WorkThreadServer.H"
00051 #include "Learn/SOFM.H"
00052 
00053 #include <vector>
00054 #include <map>
00055 
00056 class GHough
00057 {
00058 public:
00059 
00060   struct Feature
00061   {
00062     Point2D<float> loc;
00063     std::vector<float> values;
00064   };
00065 
00066   struct RTable
00067   {
00068     std::map<int, std::vector<Point2D<float> > > entries; 
00069     std::map<long, std::vector<Feature> > featureEntries;
00070   };
00071 
00072   struct Model
00073   {
00074     int id;
00075     int type;
00076     Point2D<int> center;
00077     std::map<int,std::vector<double> > invRTable; 
00078     std::vector<RTable> rTables;
00079     Point2D<float> imgPos;
00080     Point3D<float> pos;
00081     Point3D<float> rot;
00082     int numFeatures;
00083   };
00084 
00085 
00086 
00087   struct Acc
00088   {
00089     int id;
00090     Point2D<int> pos;
00091     float votes;
00092     float prob;
00093 
00094     Acc(int i, Point2D<int> p, float w) :
00095       id(i), pos(p), votes(w), prob(0)
00096     {}
00097     Acc(int i, int x, int y, float w) :
00098       id(i), pos(x,y), votes(w), prob(0)
00099     {}
00100 
00101     Acc() {}
00102   };
00103 
00104 
00105   struct AccCmp
00106   {
00107     bool operator()(const Acc& c1, const Acc& c2)
00108     {
00109       return c1.prob > c2.prob;
00110     }
00111   };
00112 
00113 
00114   GHough();
00115 
00116   //! Destructor
00117   virtual ~GHough();
00118 
00119   //! Create an Inverient RTable
00120   void createInvRTable(const Image<byte>& img, const Image<float>& ang);
00121   
00122   //! Create an RTable
00123   RTable createRTable(const Image<byte>& img, const Image<float>& ang, Point2D<float>& imgPos, int& numFeatures,
00124       Point2D<float>& imgLoc);
00125 
00126   RTable createRTable(const std::vector<Feature>& features, Point2D<float>& imgPos, Point2D<float>& imgLoc);
00127 
00128   RTable createRTable(const Image<byte>& img, const Image<float>& ang,
00129       Point2D<int>& center, int& numFeatures);
00130   
00131 
00132   //! Get votes from an RTable
00133   Image<float> getInvVotes(const Image<byte>& img, const Image<float>& ang);
00134 
00135   //The hash function
00136   long getIndex(const std::vector<float>& values);
00137   
00138 
00139   std::vector<Acc> getVotes(const Image<byte>& img, const Image<float>& ang);
00140 
00141   std::vector<Acc> getVotes(const std::vector<Feature>& features);
00142 
00143   std::vector<Acc> getVotes(int id, const RTable& rTable, const Image<byte>& img, const Image<float>& ang);
00144 
00145   std::vector<Acc> getVotes(int id, const RTable& rTable, const std::vector<Feature>& features, float& maxVotes);
00146   std::vector<Acc> getVotes2(int id, const RTable& rTable, const std::vector<Feature>& features, float& maxVotes);
00147 
00148   std::vector<Acc> getVotes(const Image<float>& img);
00149 
00150   std::vector<Acc> getVotes(const Image<float>& img, const Image<float>& ori);
00151 
00152 
00153   Point2D<float> addModel(int& id, const Image<byte>& img, const Image<float>& ang,
00154       Point3D<float> pos, Point3D<float> rot);
00155 
00156   Point2D<float> addModel(int& id, int type, const std::vector<Feature>& features,
00157       Point3D<float> pos, Point3D<float> rot);
00158 
00159 
00160   void addModel(int id, const Image<float>& img);
00161 
00162   Point2D<int> addModel(int id, const std::vector<Point2D<int> >& polygon);
00163   
00164   void setPosOffset(int id, Point3D<float> pos);
00165 
00166   void writeTable(const char* filename);
00167 
00168   void readTable(const char* filename);
00169 
00170   Point2D<float> getModelImgPos(const int id);
00171 
00172   Point3D<float> getModelRot(const int id);
00173 
00174   Point3D<float> getModelPosOffset(const int id);
00175 
00176   int getModelType(const int id);
00177   
00178   Image<PixRGB<byte> > getRTableImg(const int id);
00179 
00180   uint getNumFeatures(const int id);
00181   
00182   Image<float> getAccImg(std::vector<GHough::Acc>& acc);
00183  
00184   Point2D<int> findInvFeature(const int x, const int y,
00185       const Image<float>& img, const Image<float>& ang);
00186   
00187   void trainSOFM();
00188  
00189 private:
00190   std::vector<Model> itsModels; //A list of models
00191   int itsNumEntries;
00192   rutz::shared_ptr<WorkThreadServer> itsThreadServer;
00193  
00194   SOFM* itsSOFM; 
00195   
00196 };
00197 
00198 
00199 // ######################################################################
00200 /* So things look consistent in everyone's emacs... */
00201 /* Local Variables: */
00202 /* indent-tabs-mode: nil */
00203 /* End: */
00204 
00205 #endif //
Generated on Sun May 8 08:40:38 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3