contrib/mul/mfpf/mfpf_region_finder_builder.h
Go to the documentation of this file.
00001 #ifndef mfpf_region_finder_builder_h_
00002 #define mfpf_region_finder_builder_h_
00003 //:
00004 // \file
00005 // \brief Builder for mfpf_region_finder objects.
00006 // \author Tim Cootes
00007 
00008 #include <mfpf/mfpf_point_finder_builder.h>
00009 #include <mfpf/mfpf_vec_cost_builder.h>
00010 #include <mbl/mbl_cloneable_ptr.h>
00011 #include <mbl/mbl_chord.h>
00012 #include <vcl_iosfwd.h>
00013 #include <mfpf/mfpf_region_form.h>
00014 #include <vgl/vgl_fwd.h>
00015 
00016 //: Builder for mfpf_region_finder objects.
00017 // Text for configuring:
00018 // \verbatim
00019 // mfpf_region_finder_builder { shape: ellipse { ri: 5 rj: 3  }
00020 //  vec_cost_builder: mfpf_sad_vec_cost_builder { min_mad: 1.0 }
00021 //  norm: linear
00022 //  search_ni: 5 search_nj: 4
00023 // }
00024 // Alternative for shape:
00025 //   shape: box { ni: 5 nj: 3 ref_x: 2.5 ref_y: 1.5 }
00026 // \endverbatim
00027 class mfpf_region_finder_builder : public mfpf_point_finder_builder
00028 {
00029  private:
00030   //: Kernel reference point (in roi_ni_ x roi_nj_ grid)
00031   double ref_x_;
00032   //: Kernel reference point (in roi_ni_ x roi_nj_ grid)
00033   double ref_y_;
00034 
00035   //: String defining shape of region, eg "box" or "ellipse"
00036   vcl_string shape_;
00037 
00038   //: Chords defining the region of interest
00039   vcl_vector<mbl_chord> roi_;
00040 
00041   //: Size of bounding box of region of interest
00042   unsigned roi_ni_;
00043   //: Size of bounding box of region of interest
00044   unsigned roi_nj_;
00045 
00046   //: Number of pixels in region
00047   unsigned n_pixels_;
00048 
00049   //: Builder for cost model
00050   mbl_cloneable_ptr<mfpf_vec_cost_builder> cost_builder_;
00051 
00052   //: Which normalisation to use (0=none, 1=linear)
00053   short norm_method_;
00054 
00055   //: Number of angles either side of 0 to sample at
00056   unsigned nA_;
00057 
00058   //: Angle displacement
00059   double dA_;
00060 
00061   //: Relative size of region used for estimating overlap
00062   //  If 0.5, then overlap requires pt inside central 50% of region.
00063   double overlap_f_;
00064 
00065   //: lower bound on variance used in normalisation
00066   double var_min_;
00067 
00068   //: lowest variance found so far in training set 
00069   double tvar_min_;
00070 
00071   //: If true reset var_min based on min in training set
00072   bool estimate_var_min_;
00073 
00074     //: Number of examples added
00075   unsigned num_examples_;
00076 
00077   //: Define default values
00078   void set_defaults();
00079 
00080   //: Define model region as an ni x nj box
00081   void set_as_box(unsigned ni, unsigned nj,
00082                   double ref_x, double ref_y);
00083 
00084   //: Define model region as an ellipse with radii ri, rj
00085   //  Ref. point in centre.
00086   void set_as_ellipse(double ri, double rj);
00087 
00088   //: Parse stream to set up as a box shape.
00089   // Expects: "{ ni: 3 nj: 5 ref_x: 1.0 ref_y: 2.0 }
00090   void config_as_box(vcl_istream &is);
00091 
00092   //: Parse stream to set up as an ellipse shape.
00093   // Expects: "{ ri: 2.1 rj: 5.2 }
00094   void config_as_ellipse(vcl_istream &is);
00095 
00096   //: Add one example to the model
00097   void add_one_example(const vimt_image_2d_of<float>& image,
00098                        const vgl_point_2d<double>& p,
00099                        const vgl_vector_2d<double>& u);
00100 
00101  public:
00102 
00103   // Dflt ctor
00104   mfpf_region_finder_builder();
00105 
00106   // Destructor
00107   virtual ~mfpf_region_finder_builder();
00108 
00109   //: Define model region as an ni x nj box
00110   void set_as_box(unsigned ni, unsigned nj,
00111                   double ref_x, double ref_y,
00112                   const mfpf_vec_cost_builder& builder);
00113 
00114   //: Define model region as an ni x nj box
00115   //  Ref. point in centre.
00116   void set_as_box(unsigned ni, unsigned nj,
00117                   const mfpf_vec_cost_builder& builder);
00118 
00119   //: Define model region as an ellipse with radii ri, rj
00120   //  Ref. point in centre.
00121   void set_as_ellipse(double ri, double rj,
00122                       const mfpf_vec_cost_builder& builder);
00123 
00124   //: Define model region using description in form
00125   //  Assumes form defined in world-coords.
00126   //  Sets step_size() to form.pose().scale().
00127   void set_region(const mfpf_region_form& form);
00128 
00129   //: Define region size in world co-ordinates
00130   //  Sets up ROI to cover given box (with samples at step_size()),
00131   //  with ref point at centre.
00132   //  Currently just defines as a box
00133   virtual void set_region_size(double wi, double wj);
00134 
00135 
00136   //: Number of pixels in region
00137   unsigned n_pixels() const { return n_pixels_; }
00138 
00139   //: String defining shape of region, eg "box" or "ellipse"
00140   const vcl_string& shape() const { return shape_; }
00141 
00142   //: Builder for PDF
00143   mfpf_vec_cost_builder& cost_builder() { return cost_builder_; }
00144 
00145   //: Number of dimensions in the model
00146   virtual unsigned model_dim();
00147 
00148   //: Create new mfpf_region_finder on heap
00149   virtual mfpf_point_finder* new_finder() const;
00150 
00151   //: Initialise building
00152   // Must be called before any calls to add_example(...)
00153   virtual void clear(unsigned n_egs);
00154 
00155   //: Add one example to the model
00156   virtual void add_example(const vimt_image_2d_of<float>& image,
00157                            const vgl_point_2d<double>& p,
00158                            const vgl_vector_2d<double>& u);
00159 
00160   //: Build object from the data supplied in add_example()
00161   virtual void build(mfpf_point_finder&);
00162 
00163   //: Initialise from a string stream
00164   virtual bool set_from_stream(vcl_istream &is);
00165 
00166   //: Name of the class
00167   virtual vcl_string is_a() const;
00168 
00169   //: Create a copy on the heap and return base class pointer
00170   virtual mfpf_point_finder_builder* clone() const;
00171 
00172   //: Print class to os
00173   virtual void print_summary(vcl_ostream& os) const;
00174 
00175   //: Prints ASCII representation of shape to os
00176   void print_shape(vcl_ostream& os) const;
00177 
00178   //: Version number for I/O
00179   short version_no() const;
00180 
00181   //: Save class to binary file stream
00182   virtual void b_write(vsl_b_ostream& bfs) const;
00183 
00184   //: Load class from binary file stream
00185   virtual void b_read(vsl_b_istream& bfs);
00186 };
00187 
00188 #endif