MTF
KLD.h
1 #ifndef MTF_KLD_H
2 #define MTF_KLD_H
3 
4 #include "AppearanceModel.h"
5 
6 _MTF_BEGIN_NAMESPACE
7 
8 struct KLDParams : AMParams{
9  int n_bins;
10  bool partition_of_unity;
15  double pre_seed;
17  bool debug_mode;
19 
22  KLDParams(const AMParams *am_params,
23  int _n_bins, double _pre_seed,
24  bool _partition_of_unity, bool _debug_mode);
26  KLDParams(const KLDParams *params = nullptr);
27 };
28 
29 struct KLDDist : AMDist{
30  typedef bool is_kdtree_distance;
31  typedef double ElementType;
32  typedef double ResultType;
33  KLDDist(const string &_name, const unsigned int _feat_size) :
34  AMDist(_name), feat_size(_feat_size){}
35  double operator()(const double* a, const double* b,
36  size_t size, double worst_dist = -1) const override;
37  inline double accum_dist(const double& a, const double& b, int) const{
38  double ratio = a / b;
39  return ratio > 0 ? a * log(ratio) : 0;
40  }
41 private:
42  const unsigned int feat_size;
43 };
44 
45 
46 // (nagative) Kullback–Leibler Divergence
47 class KLD : public AppearanceModel{
48 public:
49  typedef KLDParams ParamType;
50  typedef KLDDist DistType;
51 
52  KLD(const ParamType *kld_params = nullptr);
53 
54  void initializeSimilarity() override;
55  void updateSimilarity(bool prereq_only = 1) override;
56  void initializeGrad() override;
57  void initializeHess() override;
58 
59  void updateInitGrad() override;
60  void updateCurrGrad() override;
61 
62  void cmptInitHessian(MatrixXd &hessian, const MatrixXd &curr_pix_jacobian) override;
63  void cmptCurrHessian(MatrixXd &hessian, const MatrixXd &curr_pix_jacobian) override;
64 
65  void cmptInitHessian(MatrixXd &init_hessian, const MatrixXd &init_pix_jacobian,
66  const MatrixXd &init_pix_hessian) override;
67  void cmptCurrHessian(MatrixXd &curr_hessian, const MatrixXd &curr_pix_jacobian,
68  const MatrixXd &curr_pix_hessian) override;
69 
73  const DistType* getDistFunc() override{
74  return new DistType(name, feat_size);
75  }
76  void initializeDistFeat() override;
77  unsigned int getDistFeatSize() override { return feat_size; }
78  void updateDistFeat() override {
79  updateDistFeat(feat_vec.data());
80  }
81  const double* getDistFeat() override{ return feat_vec.data(); }
82  void updateDistFeat(double* feat_addr) override;
83 
84 private:
85  ParamType params;
86 
87  char *log_fname;
88  char *time_fname;
89 
91  double hist_norm_mult;
92 
93  // let A = err_vec_size = n_bins*n_bins and N = n_pix = no. of pixels
94  VectorXd init_hist, curr_hist;
95  MatrixXd init_hist_mat, curr_hist_mat;
96 
98  MatrixXd init_hist_grad, curr_hist_grad;
99  MatrixXd init_hist_hess, curr_hist_hess;
100  VectorXd init_grad_factor, curr_grad_factor;
101  VectorXd init_hist_log, curr_hist_log;
102 
103  int feat_size;
104  VectorXd feat_vec;
105 
106  // only used internally to increase speed by offlining as many computations as possible;
107  MatrixX2i _std_bspl_ids;
108  MatrixX2i _init_bspl_ids;
109  MatrixX2i _curr_bspl_ids;
110 };
111 
112 _MTF_END_NAMESPACE
113 
114 #endif
const DistType * getDistFunc() override
Support for FLANN library.
Definition: KLD.h:73
KLDParams(const AMParams *am_params, int _n_bins, double _pre_seed, bool _partition_of_unity, bool _debug_mode)
decides whether logging data will be printed for debugging purposes;
Definition: KLD.h:8
Definition: AMParams.h:12
Definition: KLD.h:47
void updateDistFeat() override
computes a "distance" vector using the current image patch such that, when the distance vectors corre...
Definition: KLD.h:78
bool debug_mode
initial value with which each bin of the joint histogram is pre-seeded
Definition: KLD.h:18
Distance functor for FLANN.
Definition: AppearanceModel.h:40
double pre_seed
decides whether the partition of unity constraint has to be strictly observed for border bins; ...
Definition: KLD.h:16
unsigned int getDistFeatSize() override
returns the size of the distance vector
Definition: KLD.h:77
Similarity function that indicates how well a candidate warped patch matches the template.
Definition: AppearanceModel.h:63
Definition: KLD.h:29
bool partition_of_unity
no.
Definition: KLD.h:14