contrib/mul/mfpf/mfpf_mr_point_finder.h
Go to the documentation of this file.
00001 #ifndef mfpf_mr_point_finder_h_
00002 #define mfpf_mr_point_finder_h_
00003 //:
00004 // \file
00005 // \author Tim Cootes
00006 // \brief Multi-res point finder.  Searches at range of scales.
00007 
00008 #include <mbl/mbl_cloneable_ptr.h>
00009 #include <mfpf/mfpf_point_finder.h>
00010 #include <vcl_cassert.h>
00011 #include <vcl_iosfwd.h>
00012 
00013 class vimt_image_pyramid;
00014 
00015 //: Multi-res point finder.
00016 // Contains a set of mfpf_point_finders, each trained at a different
00017 // resolution.  Contains search algorithms to take advantage of this.
00018 class mfpf_mr_point_finder
00019 {
00020  protected:
00021 
00022   //: Set of cost function objects.
00023   vcl_vector<mbl_cloneable_ptr<mfpf_point_finder> > finders_;
00024 
00025   //: Maximum number of candidates to retain during multi_search_and_prune
00026   //  If zero, then refine all.
00027   unsigned max_after_pruning_;
00028 
00029  public:
00030 
00031   //: Dflt ctor
00032   mfpf_mr_point_finder();
00033 
00034   //: Destructor
00035   virtual ~mfpf_mr_point_finder();
00036 
00037   //: Number of finders
00038   unsigned size() const { return finders_.size(); }
00039 
00040   //: Point finder at level L
00041   const mfpf_point_finder& finder(unsigned L) const
00042   { assert (L<finders_.size()); return *finders_[L]; }
00043 
00044   //: Point finder at level L
00045   mfpf_point_finder& finder(unsigned L)
00046   { assert (L<finders_.size()); return *finders_[L]; }
00047 
00048   //: Define point finders.  Clone of each taken
00049   void set(const vcl_vector<mfpf_point_finder*>& finders);
00050 
00051 
00052   //: Maximum number of candidates to retain during multi_search_and_prune
00053   //  If zero, then refine all.
00054   unsigned max_after_pruning() const { return max_after_pruning_; }
00055 
00056   //: Maximum number of candidates to retain during multi_search_and_prune
00057   //  If zero, then refine all.
00058   void set_max_after_pruning(unsigned max_n);
00059 
00060 
00061   //: Select best level for searching around pose with finder \p i
00062   //  Selects pyramid level with pixel sizes best matching
00063   //  the model pixel size at given pose.
00064   unsigned image_level(unsigned i, const mfpf_pose& pose,
00065                        const vimt_image_pyramid& im_pyr) const;
00066 
00067   //: Get sample image at specified point for level L of the point_finder hierarchy
00068   void get_sample_vector(const vimt_image_pyramid& image_pyr,
00069                          const vgl_point_2d<double>& p,
00070                          const vgl_vector_2d<double>& u,
00071                          unsigned L,
00072                          vcl_vector<double>& v);
00073 
00074   //: Searches around given pose, starting at coarsest model.
00075   //  Searches with coarsest model, and feeds best result into
00076   //  search for next model.  Result can be further improved
00077   //  by a call to refine_match().
00078   //
00079   //  Returns match fit with finder(0) at best_pose.
00080   double search(const vimt_image_pyramid& im_pyr,
00081                 const mfpf_pose& pose0,
00082                 mfpf_pose& best_pose);
00083 
00084   //: Searches around given pose, starting at coarsest model.
00085   //  Searches with finder(L_hi) and feeds best result into
00086   //  search for next model, until level L_lo.
00087   //  Result can be further improved by a call to refine_match()
00088   double mr_search(const vimt_image_pyramid& im_pyr,
00089                    mfpf_pose& pose, int L_lo, int L_hi);
00090 
00091   //: Perform local optimisation to refine position,scale and angle
00092   //  Uses finder(L) to do refinement.
00093   void refine_match(const vimt_image_pyramid& im_pyr,
00094                     mfpf_pose& pose, double& fit, unsigned L=0);
00095 
00096   //: Find all local optima at coarsest scale and search around each
00097   //  Runs search at coarsest resolution, to find all local optima.
00098   //  If multiple angles/scales considered, the there may be many
00099   //  nearby responses.
00100   //  Each candidate is then localised by searching at finer and
00101   //  finer resolutions.
00102   //  Final responses may be further improved with refine_match()
00103   void multi_search(const vimt_image_pyramid& im_pyr,
00104                     const mfpf_pose& pose0,
00105                     vcl_vector<mfpf_pose>& poses,
00106                     vcl_vector<double>& fits);
00107 
00108   //: Find all non-overlapping local optima.
00109   //  Runs search at coarsest resolution, to find all local optima.
00110   //  If multiple angles/scales considered, the there may be many
00111   //  nearby responses.
00112   //  Each candidate is then localised by searching at finer and
00113   //  finer resolutions.
00114   //  After searching to level prune_level, overlapping responses
00115   //  are pruned (best match in any overlapping group is retained).
00116   //  prune_level is defined modulo size(), so -1 is equivalent to
00117   //  pruning at the coarsest level (size()-1).
00118   //  Final responses may be further improved with refine_match().
00119   void multi_search_and_prune(const vimt_image_pyramid& im_pyr,
00120                               const mfpf_pose& pose0,
00121                               vcl_vector<mfpf_pose>& poses,
00122                               vcl_vector<double>& fits,
00123                               int prune_level=-1);
00124 
00125   //: Save an image summarising each model in the hierarchy
00126   //  Saves images to basepath_L0.png, basepath_L1.png ...
00127   void save_images_of_models(const vcl_string& basepath) const;
00128 
00129   //: Version number for I/O
00130   short version_no() const;
00131 
00132   //: Name of the class
00133   virtual vcl_string is_a() const;
00134 
00135   //: Print class to os
00136   virtual void print_summary(vcl_ostream& os) const;
00137 
00138   //: Save class to binary file stream
00139   virtual void b_write(vsl_b_ostream& bfs) const;
00140 
00141   //: Load class from binary file stream
00142   virtual void b_read(vsl_b_istream& bfs);
00143 };
00144 
00145 //: Stream output operator for class reference
00146 vcl_ostream& operator<<(vcl_ostream& os,const mfpf_mr_point_finder& b);
00147 
00148 //: Binary file stream output operator for class reference
00149 void vsl_b_write(vsl_b_ostream& bfs, const mfpf_mr_point_finder& b);
00150 
00151 //: Binary file stream input operator for class reference
00152 void vsl_b_read(vsl_b_istream& bfs, mfpf_mr_point_finder& b);
00153 
00154 #endif // mfpf_mr_point_finder_h_