contrib/mul/msm/msm_shape_model_builder.h
Go to the documentation of this file.
00001 #ifndef msm_shape_model_builder_h_
00002 #define msm_shape_model_builder_h_
00003 //:
00004 // \file
00005 // \brief Object to build a msm_shape_model
00006 // \author Tim Cootes
00007 
00008 #include <vcl_cassert.h>
00009 #include <vcl_iosfwd.h>
00010 #include <vcl_string.h>
00011 #include <vsl/vsl_fwd.h>
00012 #include <msm/msm_shape_model.h>
00013 #include <msm/msm_param_limiter.h>
00014 
00015 //: Object to build a msm_shape_model
00016 class msm_shape_model_builder
00017 {
00018  private:
00019   //: Object used to deal with global transformations
00020   mbl_cloneable_ptr<msm_aligner> aligner_;
00021 
00022   //: Default choice of parameter limiter
00023   mbl_cloneable_ptr<msm_param_limiter> param_limiter_;
00024 
00025   //: Choose n.modes to explain this proportion of variance
00026   double var_prop_;
00027 
00028   //: Min. number of modes to select
00029   unsigned min_modes_;
00030 
00031   //: Max. number of modes to use
00032   unsigned max_modes_;
00033 
00034  public:
00035 
00036   // Dflt ctor
00037   msm_shape_model_builder();
00038 
00039   // Destructor
00040   ~msm_shape_model_builder();
00041 
00042   //: Set up aligner to be used
00043   void set_aligner(const msm_aligner& aligner);
00044 
00045   //: Define parameter limiter.
00046   void set_param_limiter(const msm_param_limiter&);
00047 
00048   //: Define limits on number of parameters to use in model
00049   // \param var_proportion  Proportion of variance in data to explain
00050   void set_mode_choice(unsigned min, unsigned max,
00051                        double var_proportion);
00052 
00053   //: Object used to deal with global transformations
00054   const msm_aligner& aligner() const { return aligner_; }
00055 
00056   //: Current object which limits parameters
00057   const msm_param_limiter& param_limiter() const
00058   { return param_limiter_; }
00059 
00060   //: Builds the model from the supplied examples
00061   void build_model(const vcl_vector<msm_points>& shapes,
00062                    msm_shape_model& shape_model);
00063 
00064   //: Builds the model, using subsets of elements for some modes
00065   //  Builds a shape model, allowing control of which elements may
00066   //  be varied in some of the modes.  This allows construction
00067   //  of models where some groups of points are semi-independent
00068   //  of the others.
00069   //  \param pts_used[i] indicates the set of points to be used for
00070   //  mode i (or all if \p pts_used[i] is empty).
00071   //  Modes beyond \p pts_used.size() will use all elements.
00072   //  Builds at least \p pts_used.size() modes. Number defined by
00073   //  max_modes and var_prop.
00074   void build_model(const vcl_vector<msm_points>& shapes,
00075                    const vcl_vector<vcl_vector<unsigned> >& pts_used,
00076                    msm_shape_model& shape_model);
00077 
00078   //: Builds shape model from within-class variation
00079   //  \param shapes[i] belongs to class \p id[i].
00080   //  Aligns all shapes to a common mean.
00081   //  Computes the average covariance about each class mean,
00082   //  and builds shape modes from this.
00083   //
00084   //  If \p id[i]<0, then shape is
00085   //  used for building global mean, but not for within class model.
00086   //
00087   //  \param pts_used[i] indicates which points will be controlled by mode i.
00088   void build_within_class_model(
00089                    const vcl_vector<msm_points>& shapes,
00090                    const vcl_vector<int>& id,
00091                    const vcl_vector<vcl_vector<unsigned> >& pts_used,
00092                    msm_shape_model& shape_model);
00093 
00094   //: Builds shape model from within-class variation
00095   //  \param shapes[i] belongs to class \p id[i].
00096   //  Aligns all shapes to a common mean.
00097   //  Computes the average covariance about each class mean,
00098   //  and builds shape modes from this.
00099   //
00100   //  If \p id[i]<0, then shape is
00101   //  used for building global mean, but not for within class model.
00102   void build_within_class_model(
00103                    const vcl_vector<msm_points>& shapes,
00104                    const vcl_vector<int>& id,
00105                    msm_shape_model& shape_model);
00106 
00107   //: Builds the model from the points loaded from given files
00108   //  Loads from \p points_dir/filenames[i].
00109   //  throws a mbl_exception_parse_error if fails to load in
00110   //  any of the files.
00111   void build_from_files(const vcl_string& points_dir,
00112                         const vcl_vector<vcl_string>& filenames,
00113                         msm_shape_model& shape_model);
00114 
00115   //: Version number for I/O
00116   short version_no() const;
00117 
00118   //: Name of the class
00119   vcl_string is_a() const;
00120 
00121   //: Print class to os
00122   void print_summary(vcl_ostream& os) const;
00123 
00124   //: Save class to binary file stream
00125   void b_write(vsl_b_ostream& bfs) const;
00126 
00127   //: Load class from binary file stream
00128   void b_read(vsl_b_istream& bfs);
00129 };
00130 
00131 //: Loads all shapes from \p points_dir/filenames[i].
00132 //  Throws mbl_exception_parse_error if fails.
00133 void msm_load_shapes(const vcl_string& points_dir,
00134                      const vcl_vector<vcl_string>& filenames,
00135                      vcl_vector<msm_points>& shapes);
00136 
00137 //: Binary file stream output operator for class reference
00138 void vsl_b_write(vsl_b_ostream& bfs, const msm_shape_model_builder& pts);
00139 
00140 //: Binary file stream input operator for class reference
00141 void vsl_b_read(vsl_b_istream& bfs, msm_shape_model_builder& pts);
00142 
00143 //: Stream output operator for class reference
00144 vcl_ostream& operator<<(vcl_ostream& os,const msm_shape_model_builder& pts);
00145 
00146 //: Stream output operator for class reference
00147 void vsl_print_summary(vcl_ostream& os,const msm_shape_model_builder& pts);
00148 
00149 #endif // msm_shape_model_builder_h_