contrib/mul/mfpf/mfpf_hog_box_finder.h
Go to the documentation of this file.
00001 #ifndef mfpf_hog_box_finder_h_
00002 #define mfpf_hog_box_finder_h_
00003 //:
00004 // \file
00005 // \brief Searches for rectangular region using HOG features
00006 // \author Tim Cootes
00007 
00008 #include <mfpf/mfpf_point_finder.h>
00009 #include <mipa/mipa_vector_normaliser.h>
00010 #include <mfpf/mfpf_vec_cost.h>
00011 #include <mbl/mbl_cloneable_ptr.h>
00012 #include <vcl_iosfwd.h>
00013 
00014 //: Searches for rectangular region using HOG features.
00015 //  Features are combinations of histograms of orientations of
00016 //  gradients, pooled over nested regions in a rectangle.
00017 class mfpf_hog_box_finder : public mfpf_point_finder
00018 {
00019  private:
00020   //: Kernel reference point (usually centre of sampled region [0,ni)
00021   double ref_x_;
00022   //: Kernel reference point (usually centre of sampled region) [0,nj)
00023   double ref_y_;
00024 
00025   //: Number of angle bins in histogram of orientations
00026   unsigned nA_bins_;
00027 
00028   //: When true, angles are 0-360, else 0-180
00029   bool full360_;
00030 
00031   //: Size of each cell for basic histogram is nc x nc
00032   unsigned nc_;
00033 
00034   //: Size of region is 2*ni by 2*nj cells (each cell is nc*nc)
00035   unsigned ni_;
00036   //: Size of region is 2*ni by 2*nj cells (each cell is nc*nc)
00037   unsigned nj_;
00038 
00039   //: Cost for vector sampled over ROI
00040   mbl_cloneable_ptr<mfpf_vec_cost> cost_;
00041 
00042   ////: Which normalisation to use (0=none, 1=linear)
00043   //short norm_method_;
00044 
00045     //: The normaliser
00046   mbl_cloneable_nzptr<mipa_vector_normaliser> normaliser_;
00047 
00048   //: Relative size of region used for estimating overlap
00049   //  If 0.5, then overlap requires pt inside central 50% of region.
00050   double overlap_f_;
00051 
00052   //: Define default values
00053   void set_defaults();
00054 
00055  public:
00056 
00057   // Dflt ctor
00058   mfpf_hog_box_finder();
00059 
00060   // Destructor
00061   virtual ~mfpf_hog_box_finder();
00062 
00063   //: Define region and cost of region
00064   void set(unsigned nA_bins, bool full360,
00065            unsigned ni, unsigned nj, unsigned nc,
00066            double ref_x, double ref_y,
00067            const mfpf_vec_cost& cost,
00068            const mbl_cloneable_nzptr<mipa_vector_normaliser>& normaliser);
00069 
00070   //: Relative size of region used for estimating overlap
00071   //  If 0.5, then overlap requires pt inside central 50% of region.
00072   void set_overlap_f(double);
00073 
00074 
00075   //: Radius of circle containing modelled region
00076   virtual double radius() const;
00077 
00078   //: Cost function for region vector
00079   const mfpf_vec_cost& cost() const { return cost_; }
00080 
00081   //: Cost function for region vector
00082   mfpf_vec_cost& cost() { return cost_; }
00083 
00084   //: Evaluate match at p, using u to define scale and orientation
00085   // Returns -1*log(p(region)) at p along direction u
00086   virtual double evaluate(const vimt_image_2d_of<float>& image,
00087                           const vgl_point_2d<double>& p,
00088                           const vgl_vector_2d<double>& u);
00089 
00090   //: Evaluate match at in a region around p
00091   // Returns a quality of fit at a set of positions.
00092   // response image (whose size and transform is set inside the
00093   // function), indicates the points at which the function was
00094   // evaluated.  response(i,j) is the fit at the point
00095   // response.world2im().inverse()(i,j).  The world2im() transformation
00096   // may be affine.
00097   virtual void evaluate_region(const vimt_image_2d_of<float>& image,
00098                                const vgl_point_2d<double>& p,
00099                                const vgl_vector_2d<double>& u,
00100                                vimt_image_2d_of<double>& response);
00101 
00102   //: Search given image around p, using u to define scale and angle
00103   //  On exit, new_p defines position of the best nearby match.
00104   //  Returns a quality of fit measure at that
00105   //  point (the smaller the better).
00106   virtual double search_one_pose(const vimt_image_2d_of<float>& image,
00107                                  const vgl_point_2d<double>& p,
00108                                  const vgl_vector_2d<double>& u,
00109                                  vgl_point_2d<double>& new_p);
00110 
00111   // Returns true if p is inside region at given pose
00112   // Actually only checks if p is inside bounding box,
00113   // scaled by a factor f about the reference point.
00114   bool is_inside(const mfpf_pose& pose,
00115                  const vgl_point_2d<double>& p,
00116                  double f=1.0) const;
00117 
00118   //: Return true if modelled regions at pose1 and pose2 overlap
00119   //  Checks if reference point of one is inside region of other
00120   virtual bool overlap(const mfpf_pose& pose1,
00121                        const mfpf_pose& pose2) const;
00122 
00123   //: Generate points in ref frame that represent boundary
00124   //  Points of a contour around the shape.
00125   //  Used for display purposes.
00126   virtual void get_outline(vcl_vector<vgl_point_2d<double> >& pts) const;
00127 
00128   //: Version number for I/O
00129   short version_no() const;
00130 
00131   //: Name of the class
00132   virtual vcl_string is_a() const;
00133 
00134   //: Create a copy on the heap and return base class pointer
00135   virtual mfpf_point_finder* clone() const;
00136 
00137   //: Print class to os
00138   virtual void print_summary(vcl_ostream& os) const;
00139 
00140   //: Save class to binary file stream
00141   virtual void b_write(vsl_b_ostream& bfs) const;
00142 
00143   //: Load class from binary file stream
00144   virtual void b_read(vsl_b_istream& bfs);
00145 
00146   //: Test equality
00147   bool operator==(const mfpf_hog_box_finder& nc) const;
00148 };
00149 
00150 #endif