contrib/mul/mfpf/mfpf_region_pdf.h
Go to the documentation of this file.
00001 #ifndef mfpf_region_pdf_h_
00002 #define mfpf_region_pdf_h_
00003 //:
00004 // \file
00005 // \brief Searches with a PDF of an arbitrary region
00006 // \author Tim Cootes
00007 
00008 #include <mfpf/mfpf_point_finder.h>
00009 #include <vpdfl/vpdfl_pdf_base.h>
00010 #include <mbl/mbl_cloneable_ptr.h>
00011 #include <mbl/mbl_chord.h>
00012 #include <vgl/vgl_fwd.h>
00013 #include <vcl_iosfwd.h>
00014 
00015 //: Searches with a PDF of an arbitrary region.
00016 //  Records a PDF of the normalised intensities in a
00017 //  region of interest, defined by the set of mbl_chords roi_.
00018 //  These are in the bounding box [0,roi_ni_)x[0,roi_nj_).
00019 class mfpf_region_pdf : public mfpf_point_finder
00020 {
00021  private:
00022   //: Kernel reference point (in roi_ni_ x roi_nj_ grid)
00023   double ref_x_;
00024   //: Kernel reference point (in roi_ni_ x roi_nj_ grid)
00025   double ref_y_;
00026 
00027   //: Chords defining the region of interest
00028   vcl_vector<mbl_chord> roi_;
00029 
00030   //: Size of bounding box of region of interest
00031   unsigned roi_ni_;
00032   //: Size of bounding box of region of interest
00033   unsigned roi_nj_;
00034 
00035   //: Number of pixels in region
00036   unsigned n_pixels_;
00037 
00038   //: PDf for vector sampled over ROI
00039   mbl_cloneable_ptr<vpdfl_pdf_base> pdf_;
00040 
00041   //: Which normalisation to use (0=none, 1=linear)
00042   short norm_method_;
00043 
00044   //: Relative size of region used for estimating overlap
00045   //  If 0.5, then overlap requires pt inside central 50% of region.
00046   double overlap_f_;
00047 
00048   //: Define default values
00049   void set_defaults();
00050 
00051  public:
00052 
00053   // Dflt ctor
00054   mfpf_region_pdf();
00055 
00056   // Destructor
00057   virtual ~mfpf_region_pdf();
00058 
00059   //: Define region and PDF of region
00060   void set(const vcl_vector<mbl_chord>& roi,
00061            double ref_x, double ref_y,
00062            const vpdfl_pdf_base& pdf,
00063            short norm_method=1);
00064 
00065   //: Relative size of region used for estimating overlap
00066   //  If 0.5, then overlap requires pt inside central 50% of region.
00067   void set_overlap_f(double);
00068 
00069   //: Radius of circle containing modelled region
00070   virtual double radius() const;
00071 
00072   //: PDf for region vector
00073   const vpdfl_pdf_base& pdf() const { return pdf_; }
00074 
00075   //: Evaluate match at p, using u to define scale and orientation
00076   // Returns -1*log(p(region)) at p along direction u
00077   virtual double evaluate(const vimt_image_2d_of<float>& image,
00078                           const vgl_point_2d<double>& p,
00079                           const vgl_vector_2d<double>& u);
00080 
00081   //: Evaluate match at in a region around p
00082   // Returns a quality of fit at a set of positions.
00083   // response image (whose size and transform is set inside the
00084   // function), indicates the points at which the function was
00085   // evaluated.  response(i,j) is the fit at the point
00086   // response.world2im().inverse()(i,j).  The world2im() transformation
00087   // may be affine.
00088   virtual void evaluate_region(const vimt_image_2d_of<float>& image,
00089                                const vgl_point_2d<double>& p,
00090                                const vgl_vector_2d<double>& u,
00091                                vimt_image_2d_of<double>& response);
00092 
00093   //: Search given image around p, using u to define scale and angle
00094   //  On exit, new_p defines position of the best nearby match.
00095   //  Returns a quality of fit measure at that
00096   //  point (the smaller the better).
00097   virtual double search_one_pose(const vimt_image_2d_of<float>& image,
00098                                  const vgl_point_2d<double>& p,
00099                                  const vgl_vector_2d<double>& u,
00100                                  vgl_point_2d<double>& new_p);
00101 
00102   // Returns true if p is inside region at given pose
00103   // Actually only checks if p is inside bounding box,
00104   // scaled by a factor f about the reference point.
00105   bool is_inside(const mfpf_pose& pose,
00106                  const vgl_point_2d<double>& p,
00107                  double f=1.0) const;
00108 
00109   //: Return true if modelled regions at pose1 and pose2 overlap
00110   //  Checks if reference point of one is inside region of other
00111   virtual bool overlap(const mfpf_pose& pose1,
00112                        const mfpf_pose& pose2) const;
00113 
00114   //: Generate points in ref frame that represent boundary
00115   //  Points of a contour around the shape.
00116   //  Used for display purposes.
00117   virtual void get_outline(vcl_vector<vgl_point_2d<double> >& pts) const;
00118 
00119   //: Create an image summarising the average model (where possible)
00120   //  Creates an image of the mean template used for search.
00121   //  image.world2im() gives mapping from reference frame
00122   //  into raw image co-ords (including the step size).
00123   virtual void get_image_of_model(vimt_image_2d_of<vxl_byte>& image) const;
00124 
00125   //: Version number for I/O
00126   short version_no() const;
00127 
00128   //: Name of the class
00129   virtual vcl_string is_a() const;
00130 
00131   //: Create a copy on the heap and return base class pointer
00132   virtual mfpf_point_finder* clone() const;
00133 
00134   //: Print class to os
00135   virtual void print_summary(vcl_ostream& os) const;
00136 
00137   //: Prints ASCII representation of shape to os
00138   void print_shape(vcl_ostream& os) const;
00139 
00140   //: Save class to binary file stream
00141   virtual void b_write(vsl_b_ostream& bfs) const;
00142 
00143   //: Load class from binary file stream
00144   virtual void b_read(vsl_b_istream& bfs);
00145 
00146   //: Test equality
00147   bool operator==(const mfpf_region_pdf& nc) const;
00148 };
00149 
00150 #endif