contrib/mul/mfpf/mfpf_grad_corr2d.h
Go to the documentation of this file.
00001 #ifndef mfpf_grad_corr2d_h_
00002 #define mfpf_grad_corr2d_h_
00003 //:
00004 // \file
00005 // \brief Searches over a grid using normalised correlation
00006 // \author Tim Cootes
00007 
00008 #include <mfpf/mfpf_point_finder.h>
00009 #include <vgl/vgl_fwd.h>
00010 #include <vcl_iosfwd.h>
00011 
00012 //: Searches over a grid using normalised correlation.
00013 class mfpf_grad_corr2d : public mfpf_point_finder
00014 {
00015  private:
00016   //: Kernel reference point (in kni_ x knj_ grid)
00017   double ref_x_;
00018   //: Kernel reference point (in kni_ x knj_ grid)
00019   double ref_y_;
00020 
00021   //: Relative size of region used for estimating overlap
00022   //  If 0.5, then overlap requires pt inside central 50% of region.
00023   double overlap_f_;
00024 
00025   //: Filter x-gradient kernel to search with
00026   vil_image_view<double> kernel_x_;
00027 
00028   //: Filter y-gradient kernel to search with
00029   vil_image_view<double> kernel_y_;
00030 
00031   //: Define default values
00032   void set_defaults();
00033 
00034     //: Compute gradient image around a patch
00035   void diff_image(const vimt_image_2d_of<float>& image,
00036                   const vgl_point_2d<double>& p,
00037                   const vgl_vector_2d<double>& u,
00038                   vil_image_view<double>& grad_x,
00039                   vil_image_view<double>& grad_y,
00040                   int search_ni=0, int search_nj=0);
00041 
00042  public:
00043 
00044   // Dflt ctor
00045   mfpf_grad_corr2d();
00046 
00047   // Destructor
00048   virtual ~mfpf_grad_corr2d();
00049 
00050   //: Define filter kernel to search with.
00051   //  Reference point set to the centre
00052   void set(const vil_image_view<double>& kx, const vil_image_view<double>& ky);
00053 
00054   //: Define filter kernel to search with
00055   void set(const vil_image_view<double>& kx, const vil_image_view<double>& ky,
00056            double ref_x, double ref_y);
00057 
00058   //: Define filter kernel to search with, expressed as a vector
00059   virtual bool set_model(const vcl_vector<double>& v);
00060 
00061   //: Number of dimensions in the model
00062   virtual unsigned model_dim();
00063 
00064   //: Relative size of region used for estimating overlap
00065   //  If 0.5, then overlap requires pt inside central 50% of region.
00066   void set_overlap_f(double);
00067 
00068   //: Filter x-gradient kernel to search with
00069   const vil_image_view<double>& kernel_x() const { return kernel_x_; }
00070 
00071   //: Filter y-gradient kernel to search with
00072   const vil_image_view<double>& kernel_y() const { return kernel_y_; }
00073 
00074   //: Filter kernel to search with, expressed as a vector
00075   void get_kernel_vector(vcl_vector<double>& v) const;
00076 
00077   //: Get sample of region around specified point in image
00078   virtual void get_sample_vector(const vimt_image_2d_of<float>& image,
00079                                  const vgl_point_2d<double>& p,
00080                                  const vgl_vector_2d<double>& u,
00081                                  vcl_vector<double>& v);
00082 
00083   //: Radius of circle containing modelled region
00084   virtual double radius() const;
00085 
00086   //: Evaluate match at p, using u to define scale and orientation
00087   // Returns -1*edge strength at p along direction u
00088   virtual double evaluate(const vimt_image_2d_of<float>& image,
00089                           const vgl_point_2d<double>& p,
00090                           const vgl_vector_2d<double>& u);
00091 
00092   //: Evaluate match at in a region around p
00093   // Returns a quality of fit at a set of positions.
00094   // response image (whose size and transform is set inside the
00095   // function), indicates the points at which the function was
00096   // evaluated.  response(i,j) is the fit at the point
00097   // response.world2im().inverse()(i,j).  The world2im() transformation
00098   // may be affine.
00099   virtual void evaluate_region(const vimt_image_2d_of<float>& image,
00100                                const vgl_point_2d<double>& p,
00101                                const vgl_vector_2d<double>& u,
00102                                vimt_image_2d_of<double>& response);
00103 
00104   //: Search given image around p, using u to define scale and angle
00105   //  On exit, new_p defines position of the best nearby match.
00106   //  Returns a quality of fit measure at that
00107   //  point (the smaller the better).
00108   virtual double search_one_pose(const vimt_image_2d_of<float>& image,
00109                                  const vgl_point_2d<double>& p,
00110                                  const vgl_vector_2d<double>& u,
00111                                  vgl_point_2d<double>& new_p);
00112 
00113   // Returns true if p is inside region at given pose
00114   // Actually only checks if p is inside bounding box,
00115   // scaled by a factor f about the reference point.
00116   bool is_inside(const mfpf_pose& pose,
00117                  const vgl_point_2d<double>& p,
00118                  double f=1.0) const;
00119 
00120   //: Return true if modelled regions at pose1 and pose2 overlap
00121   //  Checks if reference point of one is inside region of other
00122   virtual bool overlap(const mfpf_pose& pose1,
00123                        const mfpf_pose& pose2) const;
00124 
00125   //: Generate points in ref frame that represent boundary
00126   //  Points of a contour around the shape.
00127   //  Used for display purposes.
00128   virtual void get_outline(vcl_vector<vgl_point_2d<double> >& pts) const;
00129 
00130   //: Return an image of the kernel
00131   virtual void get_image_of_model(vimt_image_2d_of<vxl_byte>& image) const;
00132 
00133   //: Version number for I/O
00134   short version_no() const;
00135 
00136   //: Name of the class
00137   virtual vcl_string is_a() const;
00138 
00139   //: Create a copy on the heap and return base class pointer
00140   virtual mfpf_point_finder* clone() const;
00141 
00142   //: Print class to os
00143   virtual void print_summary(vcl_ostream& os) const;
00144 
00145   //: Save class to binary file stream
00146   virtual void b_write(vsl_b_ostream& bfs) const;
00147 
00148   //: Load class from binary file stream
00149   virtual void b_read(vsl_b_istream& bfs);
00150 
00151   //: Test equality
00152   bool operator==(const mfpf_grad_corr2d& nc) const;
00153 };
00154 
00155 #endif