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