MTF
CCRE.h
1 #ifndef MTF_CCRE_H
2 #define MTF_CCRE_H
3 
4 #include "AppearanceModel.h"
5 
6 _MTF_BEGIN_NAMESPACE
7 
9 
15  int n_bins;
21  double pre_seed;
22  // decides if the model is to be symmetrical
23  // with respect to initial and current pixel values
24  // as far as the gradient and hessian computations are concerned;
25  bool symmetrical_grad;
26  // no. of blocks in which to divide pixel level computations to get better performance
27  // with parallelization libraries like TBB and OpenMP; only matters if these are enabled during compilation;
28  // if set to 0 (default), this is set equal to the no. of pixels so that each block contains a single pixel
29  int n_blocks;
30 
31  bool debug_mode;
33  CCREParams(const AMParams *am_params,
34  int _n_bins, bool _partition_of_unity,
35  double _pre_seed, bool _symmetrical_grad,
36  int _n_blocks,
37  bool _debug_mode);
39  CCREParams(const CCREParams *params = nullptr);
40 };
41 
42 struct CCREDist : AMDist{
43  typedef double ElementType;
44  typedef double ResultType;
45  CCREDist(const string &_name, const int _n_bins,
46  const unsigned int _feat_size, const unsigned int _patch_size,
47  const double _hist_pre_seed, const double _pre_seed,
48  const MatrixX2i *_std_bspl_ids, const double _hist_norm_mult,
49  const double _log_hist_norm_mult);
50  double operator()(const double* a, const double* b,
51  size_t size, double worst_dist = -1) const override;
52 private:
53  const int n_bins;
54  const unsigned int feat_size;
55  const unsigned int patch_size;
56  const double pre_seed, hist_pre_seed;
57  const MatrixX2i *std_bspl_ids;
58  const double hist_norm_mult, log_hist_norm_mult;
59 };
60 
61 
63 class CCRE : public AppearanceModel{
64 
65 public:
66 
67  typedef CCREParams ParamType;
68  typedef CCREDist DistType;
69 
70  CCRE(const ParamType *ccre_params = nullptr, int _n_channels = 1);
71 
72  double getLikelihood() const override{
73  double d = (likelihood_numr / f) - 1;
74  return exp(-params.likelihood_alpha * d*d);
75  }
76 
77  bool isSymmetrical() const override{ return false; }
78 
79  void initializeSimilarity() override;
80  void initializeGrad() override;
81  void initializeHess() override;
82 
83  void updateSimilarity(bool prereq_only = 1) override;
84  void updateInitGrad() override;
85  void updateCurrGrad() override;
86 
87  void cmptInitHessian(MatrixXd &hessian, const MatrixXd &curr_pix_jacobian) override;
88  void cmptCurrHessian(MatrixXd &hessian, const MatrixXd &curr_pix_jacobian) override;
89 
90  void cmptInitHessian(MatrixXd &init_hessian, const MatrixXd &init_pix_jacobian,
91  const MatrixXd &init_pix_hessian) override;
92  void cmptCurrHessian(MatrixXd &curr_hessian, const MatrixXd &curr_pix_jacobian,
93  const MatrixXd &curr_pix_hessian) override;
94 
95  void cmptSelfHessian(MatrixXd &self_hessian, const MatrixXd &curr_pix_jacobian) override;
96  void cmptSelfHessian(MatrixXd &self_hessian, const MatrixXd &curr_pix_jacobian,
97  const MatrixXd &curr_pix_hessian) override;
98 
99  //-----------------------------------FLANN support-----------------------------------//
100  const DistType* getDistFunc() override{
101  return new DistType(name, params.n_bins, feat_size,
102  patch_size, hist_pre_seed, params.pre_seed,
103  &std_bspl_ids, hist_norm_mult, log_hist_norm_mult);
104  }
105  void initializeDistFeat() override{
106  feat_vec.resize(feat_size);
107  }
108  void updateDistFeat() override{
109  updateDistFeat(feat_vec.data());
110  }
111  const double* getDistFeat() override{ return feat_vec.data(); }
112  void updateDistFeat(double* feat_addr) override;
113  unsigned int getDistFeatSize() override{ return feat_size; }
114 
115 protected:
116  ParamType params;
117 
118  double max_similarity, likelihood_numr;
119 
124  int joint_hist_size;
125 
126  double log_hist_norm_mult;
127 
128  // let A = joint_hist_size = n_bins*n_bins and N = n_pix = no. of pixels
130  VectorXd init_hist, curr_hist;
131  VectorXd init_cum_hist, curr_cum_hist;
132  MatrixXd init_hist_mat, curr_hist_mat;
133  MatrixXd init_cum_hist_mat, curr_cum_hist_mat;
134  MatrixXd cum_joint_hist;
135 
137  MatrixXd init_hist_grad, curr_hist_grad;
138  MatrixXd init_cum_hist_grad, curr_cum_hist_grad;
139  MatrixXd init_hist_hess, curr_hist_hess;
140  MatrixXd init_cum_hist_hess, curr_cum_hist_hess;
141 
143  MatrixXd init_cum_joint_hist_grad, curr_cum_joint_hist_grad;
144 
145  MatrixXd ccre_log_term;
146  MatrixXd init_hess_factor, cum_hess_factor;
147 
148  MatrixXd init_hist_grad_ratio, cum_hist_grad_ratio;
149  MatrixXd init_hist_hess_ratio, cum_hist_hess_ratio;
150 
151  VectorXd init_hist_log, curr_hist_log;
152  VectorXd init_cum_hist_log, curr_cum_hist_log;
153  MatrixXd cum_joint_hist_log;
154 
155  VectorXd cum_joint_hist_sum;
156 
157  MatrixXd self_cum_joint_hist, self_cum_joint_hist_log;
158  MatrixXd self_ccre_log_term;
159 
160  int feat_size;
161  VectorXd feat_vec;
162 
164  MatrixX2i std_bspl_ids;
165  MatrixX2i init_bspl_ids;
166  MatrixX2i curr_bspl_ids;
167  MatrixXi linear_idx, linear_idx2;
168 
169  MatrixX2i block_extents;
170  char *log_fname;
171  char *time_fname;
172 
173  void updateSymSimilarity(bool prereq_only);
174  void updateSymInitGrad();
175  void cmptSymInitHessian(MatrixXd &hessian, const MatrixXd &curr_pix_jacobian);
176  void cmptCumSelfHist();
177 
178 };
179 
180 _MTF_END_NAMESPACE
181 
182 #endif
CCREParams(const AMParams *am_params, int _n_bins, bool _partition_of_unity, double _pre_seed, bool _symmetrical_grad, int _n_blocks, bool _debug_mode)
value constructor
void updateDistFeat() override
computes a "distance" vector using the current image patch such that, when the distance vectors corre...
Definition: CCRE.h:108
double hist_pre_seed
value with which to preseed the individual histograms
Definition: CCRE.h:121
MatrixXd init_cum_joint_hist_grad
(n_bins*n_bins) X N gradients of the (flattened) current joint histogram w.r.t. initial and current p...
Definition: CCRE.h:143
double hist_norm_mult
multiplicative factor for normalizing histograms
Definition: CCRE.h:123
Cross Cumulative Residual Entropy.
Definition: CCRE.h:63
double pre_seed
initial value with which each bin of the joint histogram is pre-seeded to avoid numerical instabiliti...
Definition: CCRE.h:21
Definition: AMParams.h:12
Definition: CCRE.h:8
Distance functor for FLANN.
Definition: AppearanceModel.h:40
Definition: CCRE.h:42
double getLikelihood() const override
returns a normalized version of the similarity that lies between 0 and 1 and can be interpreted as th...
Definition: CCRE.h:72
int n_bins
no.
Definition: CCRE.h:15
MatrixX2i std_bspl_ids
only used internally to increase speed by offlining as many computations as possible; ...
Definition: CCRE.h:164
MatrixXd init_hist_grad
n_bins X N gradients of the marginal histograms w.r.t. pixel values
Definition: CCRE.h:137
bool partition_of_unity
decides whether the partition of unity constraint has to be strictly observed for border bins; if ena...
Definition: CCRE.h:18
VectorXd init_hist
n_bins x n_bins joint histograms;
Definition: CCRE.h:130
unsigned int getDistFeatSize() override
returns the size of the distance vector
Definition: CCRE.h:113
bool isSymmetrical() const override
return false if the similarity function f is not symmetrical, i.e.
Definition: CCRE.h:77
void initializeDistFeat() override
to be called once during initialization if any of the distance feature functionality is to be used ...
Definition: CCRE.h:105
Similarity function that indicates how well a candidate warped patch matches the template.
Definition: AppearanceModel.h:63