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