contrib/mul/mbl/mbl_thin_plate_spline_2d.h

Go to the documentation of this file.
00001 // This is mul/mbl/mbl_thin_plate_spline_2d.h
00002 #ifndef mbl_thin_plate_spline_2d_h_
00003 #define mbl_thin_plate_spline_2d_h_
00004 //:
00005 // \file
00006 // \brief Construct thin plate spline to map 2D to 2D
00007 // \author Tim Cootes
00008 
00009 #include <vgl/vgl_point_2d.h>
00010 #include <vcl_vector.h>
00011 #include <vnl/vnl_vector.h>
00012 #include <vnl/vnl_matrix.h>
00013 #include <vsl/vsl_binary_io.h>
00014 
00015 //=======================================================================
00016 //: Construct thin plate spline to map 2D to 2D.
00017 // I.e. does some mapping (x',y') = f(x,y). (See Booksteins work, e.g. IPMI 1993)
00018 // The warp is `guided' by a set of
00019 // landmarks p(0) .. p(n-1) in the source plane which are to be
00020 // mapped to a (possibly deformed) set q(0)..q(n-1) in the destination.
00021 // Thus the mapping is constrained so that f(p(i)) = q(i) for i = 0..n-1.
00022 // The points are given to the build() function to set up the object.
00023 //
00024 // If one wishes to map a set of source points to multiple target points,
00025 // use set_source_pts(src_pts);  then build(target_pts); for each target set.
00026 //
00027 // \verbatim
00028 // vcl_vector<vgl_point_2d<double> > src_pts(n_points),dest_pts(n_points);
00029 //
00030 // Fill src_pts and dest_pts
00031 // .....
00032 //
00033 // Construct spline object
00034 // mbl_thin_plate_spline_2d tps;
00035 // tps.build(src_pts,dest_pts);
00036 //
00037 // // Apply to point p:
00038 // vgl_point_3d<double> p(1,2);
00039 //
00040 // vgl_point_2d<double> new_p = tps(p);
00041 // \endverbatim
00042 class mbl_thin_plate_spline_2d
00043 {
00044   vnl_vector<double> Wx_,Wy_;
00045   double Ax0_, AxX_, AxY_;
00046   double Ay0_, AyX_, AyY_;
00047   double energy_x_,energy_y_;
00048 
00049   bool return_pure_affine_;
00050 
00051   vcl_vector<vgl_point_2d<double> > src_pts_;
00052 
00053     //: Used to estimate weights in set_source_points()
00054   vnl_matrix<double> L_inv_;
00055 
00056     //: Build from small number of points
00057   void build_pure_affine(const vcl_vector<vgl_point_2d<double> >& source_pts,
00058                          const vcl_vector<vgl_point_2d<double> >& dest_pts);
00059 
00060    //: Set parameters from vectors
00061   void set_params(const vnl_vector<double>& W1,
00062                   const vnl_vector<double>& W2);
00063 
00064   void set_up_rhs(vnl_vector<double>& Bx,
00065                   vnl_vector<double>& By,
00066                   const vcl_vector<vgl_point_2d<double> >& dest_pts);
00067 
00068    //: Compute spline-bending energy
00069   void compute_energy(vnl_vector<double>& W1,
00070                       vnl_vector<double>& W2,
00071                       const vnl_matrix<double>& L);
00072 
00073  public:
00074 
00075     //: Dflt ctor
00076   mbl_thin_plate_spline_2d();
00077 
00078     //: Destructor
00079   virtual ~mbl_thin_plate_spline_2d();
00080 
00081     //: Sets up internal transformation to map source_pts onto dest_pts
00082   void build(const vcl_vector<vgl_point_2d<double> >& source_pts,
00083              const vcl_vector<vgl_point_2d<double> >& dest_pts,
00084              bool compute_the_energy=false);
00085 
00086     //: Define source point positions
00087     //  Performs pre-computations so that build(dest_points) can be
00088     //  called multiple times efficiently
00089   void set_source_pts(const vcl_vector<vgl_point_2d<double> >& source_pts);
00090 
00091     //: Sets up internal transformation to map source_pts onto dest_pts
00092   void build(const vcl_vector<vgl_point_2d<double> >& dest_pts);
00093 
00094        //: Return transformed version of (x,y)
00095   vgl_point_2d<double>  operator()(double x, double y) const;
00096 
00097        //: Return transformed version of (x,y)
00098   vgl_point_2d<double>  operator()(const vgl_point_2d<double>&  p) const
00099   { return operator()(p.x(),p.y()); }
00100 
00101     //: Bending energy of X component (zero for pure affine)
00102     //  A measure of total amount of non-linear deformation
00103   double bendingEnergyX() const { return energy_x_; }
00104 
00105     //: Bending energy of X component (zero for pure affine)
00106     //  A measure of total amount of non-linear deformation
00107   double bendingEnergyY() const { return energy_y_; }
00108 
00109   //:
00110   // If this parameter is set to true, then only global affine part
00111   // of the currently computed transformation is used
00112   void set_pure_affine(bool val) { return_pure_affine_ = val; }
00113 
00114     //: Version number for I/O
00115   short version_no() const;
00116 
00117     //: Print class to os
00118   void print_summary(vcl_ostream& os) const;
00119 
00120     //: Save class to binary file stream
00121   void b_write(vsl_b_ostream& bfs) const;
00122 
00123     //: Load class from binary file stream
00124   void b_read(vsl_b_istream& bfs);
00125 
00126     //: Comparison operator
00127   bool operator==(const mbl_thin_plate_spline_2d& tps) const;
00128 };
00129 
00130   //: Binary file stream output operator for class reference
00131 void vsl_b_write(vsl_b_ostream& bfs, const mbl_thin_plate_spline_2d& b);
00132 
00133   //: Binary file stream input operator for class reference
00134 void vsl_b_read(vsl_b_istream& bfs, mbl_thin_plate_spline_2d& b);
00135 
00136   //: Stream output operator for class reference
00137 vcl_ostream& operator<<(vcl_ostream& os,const mbl_thin_plate_spline_2d& b);
00138 
00139 #endif
00140 
00141 

Generated on Wed Oct 8 05:10:52 2008 for contrib/mul/mbl by  doxygen 1.5.1