contrib/mul/mfpf/mfpf_region_finder.h
Go to the documentation of this file.
00001 #ifndef mfpf_region_finder_h_
00002 #define mfpf_region_finder_h_
00003 //:
00004 // \file
00005 // \brief Searches for an arbitrary shaped region
00006 // \author Tim Cootes
00007 
00008 #include <mfpf/mfpf_point_finder.h>
00009 #include <mfpf/mfpf_vec_cost.h>
00010 #include <mbl/mbl_cloneable_ptr.h>
00011 #include <mbl/mbl_chord.h>
00012 #include <vgl/vgl_fwd.h>
00013 #include <vcl_iosfwd.h>
00014 
00015 //: Searches to minimise a cost of pixels in an arbitrary region.
00016 //  Samples intensities in a region of interest, defined by the set of mbl_chords roi_.
00017 //  These are in the bounding box [0,roi_ni_)x[0,roi_nj_).
00018 //  These are optionally normalised, then fed into a mfpf_vec_cost function to
00019 //  evaluate the quality.
00020 class mfpf_region_finder : public mfpf_point_finder
00021 {
00022  private:
00023   //: Kernel reference point (in roi_ni_ x roi_nj_ grid)
00024   double ref_x_;
00025   //: Kernel reference point (in roi_ni_ x roi_nj_ grid)
00026   double ref_y_;
00027 
00028   //: Chords defining the region of interest
00029   vcl_vector<mbl_chord> roi_;
00030 
00031   //: Size of bounding box of region of interest
00032   unsigned roi_ni_;
00033   //: Size of bounding box of region of interest
00034   unsigned roi_nj_;
00035 
00036   //: Number of pixels in region
00037   unsigned n_pixels_;
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   //: Relative size of region used for estimating overlap
00046   //  If 0.5, then overlap requires pt inside central 50% of region.
00047   double overlap_f_;
00048 
00049   //: Lower bound on variance used in normalisation
00050   double var_min_;
00051 
00052   //: When generating visual image of model with multi-planes, is only the first plane drawn or L2 norm of all planes
00053   bool draw_only_1st_plane_;
00054 
00055   //: Define default values
00056   void set_defaults();
00057 
00058  public:
00059 
00060   // Dflt ctor
00061   mfpf_region_finder();
00062 
00063   // Destructor
00064   virtual ~mfpf_region_finder();
00065 
00066   //: Define region and cost function to be used
00067   void set(const vcl_vector<mbl_chord>& roi,
00068            double ref_x, double ref_y,
00069            const mfpf_vec_cost& cost,
00070            short norm_method=1);
00071 
00072   //: Relative size of region used for estimating overlap
00073   //  If 0.5, then overlap requires pt inside central 50% of region.
00074   void set_overlap_f(double);
00075 
00076   //: Minimum variance used when normalising patch
00077   void set_var_min(double var_min) {var_min_=var_min;}
00078 
00079   //: Minimum variance used when normalising patch
00080   double var_min() const {return var_min_;}
00081 
00082   //: Radius of circle containing modelled region
00083   virtual double radius() const;
00084 
00085   //: Cost function for region vector
00086   const mfpf_vec_cost& cost() const { return cost_; }
00087 
00088   //: Cost function for region vector
00089   mfpf_vec_cost& cost() { return cost_; }
00090 
00091   //: Evaluate match at p, using u to define scale and orientation
00092   // Returns cost()(region_sample) at p along direction u
00093   virtual double evaluate(const vimt_image_2d_of<float>& image,
00094                           const vgl_point_2d<double>& p,
00095                           const vgl_vector_2d<double>& u);
00096 
00097   //: Evaluate match at in a region around p
00098   // Returns a quality of fit at a set of positions.
00099   // response image (whose size and transform is set inside the
00100   // function), indicates the points at which the function was
00101   // evaluated.  response(i,j) is the fit at the point
00102   // response.world2im().inverse()(i,j).  The world2im() transformation
00103   // may be affine.
00104   virtual void evaluate_region(const vimt_image_2d_of<float>& image,
00105                                const vgl_point_2d<double>& p,
00106                                const vgl_vector_2d<double>& u,
00107                                vimt_image_2d_of<double>& response);
00108 
00109   //: Search given image around p, using u to define scale and angle
00110   //  On exit, new_p defines position of the best nearby match.
00111   //  Returns a quality of fit measure at that
00112   //  point (the smaller the better).
00113   virtual double search_one_pose(const vimt_image_2d_of<float>& image,
00114                                  const vgl_point_2d<double>& p,
00115                                  const vgl_vector_2d<double>& u,
00116                                  vgl_point_2d<double>& new_p);
00117 
00118   // Returns true if p is inside region at given pose
00119   // Actually only checks if p is inside bounding box,
00120   // scaled by a factor f about the reference point.
00121   bool is_inside(const mfpf_pose& pose,
00122                  const vgl_point_2d<double>& p,
00123                  double f=1.0) const;
00124 
00125   //: Return true if modelled regions at pose1 and pose2 overlap
00126   //  Checks if reference point of one is inside region of other
00127   virtual bool overlap(const mfpf_pose& pose1,
00128                        const mfpf_pose& pose2) const;
00129 
00130   //: Generate points in ref frame that represent boundary
00131   //  Points of a contour around the shape.
00132   //  Used for display purposes.
00133   virtual void get_outline(vcl_vector<vgl_point_2d<double> >& pts) const;
00134 
00135   //: Create an image summarising the average model (where possible)
00136   //  Creates an image of the mean template used for search.
00137   //  image.world2im() gives mapping from reference frame
00138   //  into raw image co-ords (including the step size).
00139   virtual void get_image_of_model(vimt_image_2d_of<vxl_byte>& image) const;
00140 
00141   //: Version number for I/O
00142   short version_no() const;
00143 
00144   //: Name of the class
00145   virtual vcl_string is_a() const;
00146 
00147   //: Create a copy on the heap and return base class pointer
00148   virtual mfpf_point_finder* clone() const;
00149 
00150   //: Print class to os
00151   virtual void print_summary(vcl_ostream& os) const;
00152 
00153   //: Prints ASCII representation of shape to os
00154   void print_shape(vcl_ostream& os) const;
00155 
00156   //: Save class to binary file stream
00157   virtual void b_write(vsl_b_ostream& bfs) const;
00158 
00159   //: Load class from binary file stream
00160   virtual void b_read(vsl_b_istream& bfs);
00161 
00162   //: Test equality
00163   bool operator==(const mfpf_region_finder& nc) const;
00164 };
00165 
00166 #endif