MTF
PF.h
1 #ifndef MTF_PF_H
2 #define MTF_PF_H
3 
4 #include "SearchMethod.h"
5 #include <boost/random/mersenne_twister.hpp>
6 #include <boost/random/normal_distribution.hpp>
7 #include <boost/random/uniform_real_distribution.hpp>
8 #include "PFParams.h"
9 
10 #ifdef ENABLE_PARALLEL
11 #include <memory>
12 #endif
13 
14 _MTF_BEGIN_NAMESPACE
15 
19 struct Particle{
20  VectorXd state[2];
21  VectorXd ar[2];
22  double wt;
23  double cum_wt;
24  int curr_set_id;
25  void resize(int state_size){
26  state[0].resize(state_size);
27  state[1].resize(state_size);
28  ar[0].resize(state_size);
29  ar[1].resize(state_size);
30  }
31 };
32 
33 // Particle Filter
34 template<class AM, class SSM>
35 class PF : public SearchMethod < AM, SSM > {
36 
37 public:
38 
39  typedef PFParams ParamType;
40  typedef ParamType::DynamicModel DynamicModel;
41  typedef ParamType::UpdateType UpdateType;
42  typedef ParamType::LikelihoodFunc LikelihoodFunc;
43  typedef ParamType::ResamplingType ResamplingType;
44  typedef ParamType::MeanType MeanType;
45 
46  typedef boost::mt11213b RandGenT;
47  typedef boost::normal_distribution<double> MeasureDistT;
48  //typedef boost::variate_generator<RandGenT&, MeasureDistT> MeasureGenT;
49  typedef boost::random::uniform_real_distribution<double> ResampleDistT;
50  //typedef boost::variate_generator<RandGenT&, ResampleDistT> ResampleGenT;
51  typedef ResampleDistT::param_type ResampleDistParamT;
52 
53  using SearchMethod<AM, SSM> ::am;
54  using SearchMethod<AM, SSM> ::ssm;
55  using typename SearchMethod<AM, SSM> ::AMParams;
56  using typename SearchMethod<AM, SSM> ::SSMParams;
57  using SearchMethod<AM, SSM> ::cv_corners_mat;
58  using SearchMethod<AM, SSM> ::name;
59  using SearchMethod<AM, SSM> ::initialize;
60  using SearchMethod<AM, SSM> ::update;
61 
62  PF(const ParamType *pf_params = nullptr,
63  const AMParams *am_params = nullptr,
64  const SSMParams *ssm_params = nullptr);
65  ~PF(){}
66 
67  void initialize(const cv::Mat &corners) override;
68  void update() override;
69  void setRegion(const cv::Mat& corners) override;
70 
71 protected:
72 
73 #ifdef ENABLE_PARALLEL
74  typedef std::unique_ptr<AM> AMPTr;
75  typedef std::unique_ptr<SSM> SSMPTr;
76  std::vector<AMPTr> am_vec;
77  std::vector<SSMPTr> ssm_vec;
78 #endif
79 
80  ParamType params;
81 
82  RandGenT measurement_gen;
83  MeasureDistT measurement_dist;
84 
85  RandGenT resample_gen;
86  ResampleDistT resample_dist;
87 
90 
91  unsigned int ssm_state_size;
92  int frame_id;
93 
94  Matrix3d warp_update;
95 
96  CornersT mean_corners;
97  CornersT prev_corners;
98 
99  VectorXd mean_state;
100  // SSM states for all particles
101  // 2 sets of particles are stored for efficient resampling
102  std::vector<VectorXd> particle_states[2];
103  // Update history for Auto Regression
104  std::vector<VectorXd> particle_ar[2];
105  int curr_set_id;
106 
109 
110  VectorXd particle_wts;
111  VectorXd particle_cum_wts;
112 
113  VectorXd perturbed_state;
114  VectorXd perturbed_ar;
115 
116  VectorXd state_sigma, state_mean;
117  VectorXi resample_ids;
118  VectorXd uniform_rand_nums;
119  bool using_pix_sigma;
120 
121  double measurement_likelihood;
122  double measurement_factor;
123  cv::Mat curr_img_uchar;
124 
125  bool enable_adaptive_resampling;
126  double min_eff_particles;
127 
128  char *log_fname;
129  char *time_fname;
130 
131  void initializeParticles();
132  void linearMultinomialResampling();
133  void binaryMultinomialResampling();
134  void residualResampling();
135  void updateMeanCorners();
136 };
137 
138 _MTF_END_NAMESPACE
139 
140 #endif
141 
Definition: StateSpaceModel.h:35
Definition: PFParams.h:8
Definition: PF.h:35
structure with all information about a particle might be used for a possible future migration from th...
Definition: PF.h:19
Definition: AMParams.h:12
int max_wt_id
ID of the particle with the maximum weight.
Definition: PF.h:108
double max_similarity
similarity of the initial patch (or template) with itself
Definition: PF.h:89
Definition: SearchMethod.h:10