contrib/mul/mfpf/mfpf_pose_predictor.h
Go to the documentation of this file.
00001 #ifndef mfpf_pose_predictor_h_
00002 #define mfpf_pose_predictor_h_
00003 //:
00004 // \file
00005 // \brief Uses regression to predict new pose from current sample
00006 // \author Tim Cootes
00007 
00008 #include <mfpf/mfpf_pose.h>
00009 #include <mbl/mbl_chord.h>
00010 #include <vnl/vnl_matrix.h>
00011 #include <vnl/vnl_vector.h>
00012 #include <vimt/vimt_image_2d_of.h>
00013 #include <vgl/vgl_fwd.h>
00014 #include <vcl_iosfwd.h>
00015 
00016 
00017 //: Types of 2D transformation
00018 // translation=(tx,ty), rigid=(tx,ty,A), 
00019 // zoom=(tx,ty,s), similarity = (tx,ty,scosA,ssinA)
00020 enum mfpf_pose_type { translation,rigid,zoom,similarity };
00021 
00022 vcl_ostream& operator<<(vcl_ostream&,const mfpf_pose_type&);
00023 
00024 //: Uses regression to predict new pose from current sample.
00025 //  Samples intensities in a region of interest, defined by the set of mbl_chords roi_.
00026 //  These are in the bounding box [0,roi_ni_)x[0,roi_nj_).
00027 //  Uses a pre-trained regression function to predict a
00028 //  better position based on this sample.
00029 class mfpf_pose_predictor
00030 {
00031  private:
00032   //: Size of step between sample points
00033   double step_size_;
00034 
00035   //: Kernel reference point (in roi_ni_ x roi_nj_ grid)
00036   double ref_x_;
00037   //: Kernel reference point (in roi_ni_ x roi_nj_ grid)
00038   double ref_y_;
00039 
00040   //: Chords defining the region of interest
00041   vcl_vector<mbl_chord> roi_;
00042 
00043   //: Size of bounding box of region of interest
00044   unsigned roi_ni_;
00045   //: Size of bounding box of region of interest
00046   unsigned roi_nj_;
00047 
00048   //: Number of pixels in region
00049   unsigned n_pixels_;
00050 
00051   //: Which sort of transformation to use
00052   mfpf_pose_type pose_type_;
00053 
00054   //: Which normalisation to use (0=none, 1=linear)
00055   short norm_method_;
00056 
00057   //: Lower bound on variance used in normalisation
00058   double var_min_;
00059 
00060   //: Matrix used to predict update dp=Rv+dp0
00061   vnl_matrix<double> R_;
00062 
00063   //: Offset in regression dp=Rv+dp0
00064   vnl_vector<double> dp0_;
00065 
00066   //: Define default values
00067   void set_defaults();
00068 
00069  public:
00070 
00071   // Dflt ctor
00072   mfpf_pose_predictor();
00073 
00074   // Destructor
00075   virtual ~mfpf_pose_predictor();
00076 
00077   //: Size of step between sample points
00078   void set_step_size(double);
00079 
00080   //: Size of step between sample points
00081   double step_size() const { return step_size_; }
00082 
00083   //: Which sort of transformation to use
00084   const mfpf_pose_type& pose_type() const
00085   { return pose_type_; }
00086 
00087   //: Which sort of transformation to use
00088   void set_pose_type(const mfpf_pose_type&);
00089 
00090   //: Define region to be used 
00091   void set(const vcl_vector<mbl_chord>& roi,
00092            double ref_x, double ref_y,
00093            short norm_method=1);
00094 
00095   //: Initialise as a rectangle with ref. in centre.
00096   void set_as_box(unsigned ni, unsigned nj, short norm_method=1);
00097 
00098   //: Define model region as an ellipse with radii ri, rj
00099   //  Ref. point in centre.
00100   void set_as_ellipse(double ri, double rj,
00101                       short norm_method=1);
00102 
00103   //: Set regression matrices
00104   void set_predictor(const vnl_matrix<double>& R,
00105                      const vnl_vector<double>& dp0);
00106 
00107   //: Minimum variance used when normalising patch
00108   void set_var_min(double var_min) {var_min_=var_min;}
00109 
00110   //: Minimum variance used when normalising patch
00111   double var_min() const {return var_min_;}
00112 
00113   //: Radius of circle containing modelled region
00114   virtual double radius() const;
00115 
00116   unsigned n_pixels() const { return n_pixels_; }
00117 
00118   //: Get sample of region around specified point in image
00119   virtual void get_sample_vector(const vimt_image_2d_of<float>& image,
00120                                  const vgl_point_2d<double>& p,
00121                                  const vgl_vector_2d<double>& u,
00122                                  vnl_vector<double>& v);
00123 
00124   //: Sample at pose0 and predict a better pose (new_pose)
00125   virtual void new_pose(const vimt_image_2d_of<float>& image,
00126                           const mfpf_pose& pose0,
00127                           mfpf_pose& new_pose);
00128 
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   //: Version number for I/O
00136   short version_no() const;
00137 
00138   //: Name of the class
00139   virtual vcl_string is_a() const;
00140 
00141   //: Create a copy on the heap and return base class pointer
00142   virtual mfpf_pose_predictor* clone() const;
00143 
00144   //: Print class to os
00145   virtual void print_summary(vcl_ostream& os) const;
00146 
00147   //: Prints ASCII representation of shape to os
00148   void print_shape(vcl_ostream& os) const;
00149 
00150   //: Save class to binary file stream
00151   virtual void b_write(vsl_b_ostream& bfs) const;
00152 
00153   //: Load class from binary file stream
00154   virtual void b_read(vsl_b_istream& bfs);
00155 
00156   //: Test equality
00157   bool operator==(const mfpf_pose_predictor& nc) const;
00158 };
00159 
00160 //: Binary file stream output operator for class reference
00161 void vsl_b_write(vsl_b_ostream& bfs, const mfpf_pose_predictor& b);
00162 
00163 //: Binary file stream input operator for class reference
00164 void vsl_b_read(vsl_b_istream& bfs, mfpf_pose_predictor& b);
00165 
00166 //: Stream output operator for class reference
00167 vcl_ostream& operator<<(vcl_ostream& os,const mfpf_pose_predictor& b);
00168 
00169 
00170 #endif