contrib/brl/bseg/strk/strk_tracker.h
Go to the documentation of this file.
00001 // This is brl/bseg/strk/strk_tracker.h
00002 #ifndef strk_tracker_h_
00003 #define strk_tracker_h_
00004 //---------------------------------------------------------------------
00005 //:
00006 // \file
00007 // \brief a processor for tracking a face_2d based on intensity matching
00008 //
00009 //  The tracker operates by randomly generating a set of hypotheses in the
00010 //  vicinity of the previous best n matches. These new hypotheses are tested,
00011 //  (for now by normalized cross-correlation) and ranked to select the best
00012 //  matches for the next iteration.  The current algorithm only adjusts the
00013 //  translation each tracked face.
00014 //
00015 // \author
00016 //  J.L. Mundy - August 20, 2003
00017 //
00018 // \verbatim
00019 //  Modifications
00020 //   <none>
00021 // \endverbatim
00022 //
00023 //-------------------------------------------------------------------------
00024 #include <vcl_vector.h>
00025 #include <vil1/vil1_image.h>
00026 #include <vil1/vil1_memory_image_of.h>
00027 #include <vtol/vtol_face_2d_sptr.h>
00028 #include <vtol/vtol_intensity_face_sptr.h>
00029 #include <strk/strk_tracker_params.h>
00030 
00031 //: A container to support sorting of samples
00032 //  Will result in descending order according to correlation
00033 struct strk_correlated_face
00034 {
00035   strk_correlated_face ();
00036   ~strk_correlated_face();
00037   void set_face(vtol_intensity_face_sptr const& f);
00038   void set_correlation(const float c) {c_ = c;}
00039   vtol_intensity_face_sptr face() {return f_;}
00040   float correlation() const {return c_;}
00041   float Ix(int i){return Ix_[i];}
00042   float Iy(int i){return Iy_[i];}
00043   void set_Ix(int i, const float Ix){Ix_[i] = Ix;}
00044   void set_Iy(int i, const float Iy){Iy_[i] = Iy;}
00045  private:
00046   float c_;
00047   vtol_intensity_face_sptr f_;
00048   float* Ix_;
00049   float* Iy_;
00050 };
00051 
00052 class strk_tracker : public strk_tracker_params
00053 {
00054  public:
00055   //Constructors/destructor
00056   strk_tracker(strk_tracker_params& tp);
00057 
00058   ~strk_tracker();
00059   //Accessors
00060   void set_image_0(vil1_image& image);
00061   void set_image_i(vil1_image& image);
00062   void set_initial_model(vtol_face_2d_sptr const& face);
00063   vtol_face_2d_sptr get_best_sample();
00064   void get_samples(vcl_vector<vtol_face_2d_sptr> & samples);
00065   //Utility Methods
00066   void init();
00067   void generate_samples();
00068   void cull_samples();
00069   void track();
00070   void clear();
00071 
00072  protected:
00073   //protected methods
00074   //: translate the input face by tx and ty
00075   vtol_intensity_face_sptr
00076     transform_face(vtol_intensity_face_sptr const& face,
00077                    double tx, double ty, double theta, double scale);
00078 
00079   //: fill the pixels in the input face from the input image
00080   void fill_face(vtol_intensity_face_sptr const& face,
00081                  vil1_memory_image_of<float> const& image);
00082 
00083   void set_gradient(strk_correlated_face* cf,
00084                     vil1_memory_image_of<float> const& Ix,
00085                     vil1_memory_image_of<float> const& Iy);
00086   //: compute a distance between a face and the underlying image
00087   void correlate_face(strk_correlated_face* cf);
00088 
00089   //: Generate a random sample for face translation
00090   vtol_intensity_face_sptr
00091     generate_sample(vtol_intensity_face_sptr const& seed);
00092 
00093   //: Generate a more informative face using the previous method
00094   strk_correlated_face*
00095   generate_cf_sample(strk_correlated_face* seed);
00096 
00097   //: Form a new sample from the current image
00098   strk_correlated_face*
00099     regenerate_cf_sample(strk_correlated_face* sample);
00100 
00101   //: Form a new sample from the current image
00102   void transform_sample_in_place(strk_correlated_face* sample,
00103                                  double tx, double ty,
00104                                  double theta, double scale);
00105 
00106   //: Compute the optical flow motion
00107   bool compute_motion(strk_correlated_face* cf,
00108                       double& tx, double& ty,
00109                       double& theta, double& scale);
00110 
00111   double compute_correlation(strk_correlated_face* cf);
00112 
00113   double compute_gradient_angle(strk_correlated_face* cf);
00114 
00115   double compute_angle_motion(strk_correlated_face* cf);
00116   bool compute_scale_motion(strk_correlated_face* cf, double& sx, double& sy);
00117   //members
00118   vil1_memory_image_of<float> image_0_;  //frame 0
00119   vil1_memory_image_of<float> image_i_;  //frame i
00120   vil1_memory_image_of<float> Ix_0_;  //x derivative of image_0
00121   vil1_memory_image_of<float> Iy_0_;  //y derivative of image_0
00122   vil1_memory_image_of<float> Ix_i_;  //x derivative of image_i
00123   vil1_memory_image_of<float> Iy_i_;  //y derivative of image_i
00124   vtol_face_2d_sptr initial_model_;//initial model position
00125   vcl_vector<strk_correlated_face*> current_samples_;
00126   vcl_vector<strk_correlated_face*> hypothesized_samples_;
00127   vcl_vector<double> sample_scores_;
00128 };
00129 
00130 #endif // strk_tracker_h_