MTF
SSDBase.h
1 #ifndef MTF_SSD_BASE_H
2 #define MTF_SSD_BASE_H
3 
4 #include "AppearanceModel.h"
5 
6 #include <boost/random/linear_congruential.hpp>
7 #include <boost/random/normal_distribution.hpp>
8 
9 _MTF_BEGIN_NAMESPACE
10 
17  typedef bool is_kdtree_distance;
18  typedef double ElementType;
19  typedef double ResultType;
20  SSDBaseDist(const string &_name) : AMDist(_name){}
33  double operator()(const double* a, const double* b,
34  size_t size, double worst_dist = -1) const override;
41  double accum_dist(const double& a, const double& b, int) const{
42  return (a - b)*(a - b);
43  }
44 };
45 class SSDBase : public AppearanceModel{
46 
47 public:
48  typedef SSDBaseDist DistType;
49  typedef IlluminationModel::PixHessType ILMPixHessT;
50 
51  SSDBase(const AMParams *am_params = nullptr,
52  const int _n_channels = 1);
53  int getStateSize() const override{ return state_size; }
54  void initializeSimilarity() override;
55  void initializeGrad() override;
56  void initializeHess() override{
57  if(ilm){
58  d2f_dpam2.resize(state_size, state_size);
59  d2f_dpam_dIt.resize(state_size, patch_size);
60  }
61  }
67  void updateInitGrad() override{
68  if(ilm){
69  df_dg0 = df_dI0;
70  ilm->cmptPixJacobian(df_dI0.data(), df_dg0.data(), I0.data(), p_am.data());
71  }
72  }
73  double getLikelihood() const override;
74  //-----------------------------------------------------------------------------------//
75  //-------------------------------update functions------------------------------------//
76  //-----------------------------------------------------------------------------------//
77  void updateSimilarity(bool prereq_only = true) override;
78  void updateState(const VectorXd& state_update) override;
79  void invertState(VectorXd& inv_p, const VectorXd& p) override;
80  void updateCurrGrad() override;
81  void cmptInitJacobian(RowVectorXd &df_dp,
82  const MatrixXd &dI0_dpssm) override;
83  void cmptCurrJacobian(RowVectorXd &df_dp,
84  const MatrixXd &dIt_dpssm) override;
85  void cmptDifferenceOfJacobians(RowVectorXd &diff_of_jacobians,
86  const MatrixXd &dI0_dpssm, const MatrixXd &dIt_dpssm) override;
87  void cmptInitHessian(MatrixXd &init_hessian,
88  const MatrixXd &init_pix_jacobian) override;
89  void cmptCurrHessian(MatrixXd &curr_hessian,
90  const MatrixXd &curr_pix_jacobian) override;
91  void cmptSelfHessian(MatrixXd &self_hessian,
92  const MatrixXd &curr_pix_jacobian) override{
93  cmptCurrHessian(self_hessian, curr_pix_jacobian);
94  }
95  void cmptSelfHessian(MatrixXd &self_hessian, const MatrixXd &curr_pix_jacobian,
96  const MatrixXd &curr_pix_hessian) override{
97  cmptSelfHessian(self_hessian, curr_pix_jacobian);
98  }
99  void cmptSumOfHessians(MatrixXd &sum_of_hessians,
100  const MatrixXd &init_pix_jacobian,
101  const MatrixXd &curr_pix_jacobian) override;
102  void cmptInitHessian(MatrixXd &init_hessian, const MatrixXd &init_pix_jacobian,
103  const MatrixXd &init_pix_hessian) override;
104  void cmptCurrHessian(MatrixXd &curr_hessian, const MatrixXd &curr_pix_jacobian,
105  const MatrixXd &curr_pix_hessian) override;
106  void cmptSumOfHessians(MatrixXd &sum_of_hessians,
107  const MatrixXd &init_pix_jacobian, const MatrixXd &curr_pix_jacobian,
108  const MatrixXd &init_pix_hessian, const MatrixXd &curr_pix_hessian) override;
109 
113  const DistType* getDistFunc() override{
114  return new DistType(name);
115  }
116  void updateDistFeat(double* feat_addr) override{
117  unsigned int feat_size = getDistFeatSize();
118  for(size_t pix = 0; pix < feat_size; ++pix) {
119  *feat_addr++ = It(pix);
120  }
121  }
122  void initializeDistFeat() override{}
123  void updateDistFeat() override{}
124  const double* getDistFeat() override{ return getCurrPixVals().data(); }
125  unsigned int getDistFeatSize() override{ return n_pix*n_channels; }
126 
127  // -------------------------------------------------------------------------- //
128  // --------------------------- Stochastic Sampler --------------------------- //
129  // -------------------------------------------------------------------------- //
130 
131  typedef boost::minstd_rand SampleGenT;
132  typedef boost::normal_distribution<double> SampleDistT;
133  typedef SampleDistT::param_type DistParamT;
134 
135  std::vector<SampleGenT> rand_gen;
136  std::vector<SampleDistT> rand_dist;
137  VectorXd state_perturbation;
138 
139  void initializeSampler(const VectorXd &state_sigma,
140  const VectorXd &state_mean) override;
141  void setSampler(const VectorXd &state_sigma,
142  const VectorXd &state_mean) override;
143  void setSamplerMean(const VectorXd &mean) override;
144  void setSamplerSigma(const VectorXd &std) override;
145  void getSamplerMean(VectorXd &mean) override;
146  void getSamplerSigma(VectorXd &std) override;
147  void generatePerturbation(VectorXd &perturbation) override;
148 
149 #ifndef DISABLE_SPI
150  bool supportsSPI() const override{ return n_channels == 1; }
151 #endif
152 
153 protected:
158  AMParams::ILM ilm;
159 
164 
165  VectorXdM I_diff;
166  PixValT It_orig;
167  ILMPixHessT ilm_d2f_dIt_type;
168 
169  void cmptILMHessian(MatrixXd &d2f_dp2, const MatrixXd &dI_dpssm,
170  const double* I, const double* df_dg = nullptr);
171  // functions to provide support for SPI
172  virtual void getJacobian(RowVectorXd &jacobian, const bool *pix_mask,
173  const RowVectorXd &curr_grad, const MatrixXd &pix_jacobian);
174  virtual void getHessian(MatrixXd &hessian, const bool *pix_mask, const MatrixXd &pix_jacobian);
175  virtual void getDifferenceOfJacobians(RowVectorXd &diff_of_jacobians, const bool *pix_mask,
176  const MatrixXd &init_pix_jacobian, const MatrixXd &curr_pix_jacobian);
177  virtual void getSumOfHessians(MatrixXd &sum_of_hessians, const bool *pix_mask,
178  const MatrixXd &init_pix_jacobian, const MatrixXd &curr_pix_jacobian);
179 };
180 
181 _MTF_END_NAMESPACE
182 
183 #endif
double likelihood_alpha
multiplicative factor for the exponent in the likelihood
Definition: SSDBase.h:163
double operator()(const double *a, const double *b, size_t size, double worst_dist=-1) const override
Squared Euclidean distance functor, optimized version.
bool supportsSPI() const override
should be overridden by an implementing class once it implements SPI functionality for all functions ...
Definition: SSDBase.h:150
void initializeDistFeat() override
to be called once during initialization if any of the distance feature functionality is to be used ...
Definition: SSDBase.h:122
Definition: SSDBase.h:45
const DistType * getDistFunc() override
Support for FLANN library.
Definition: SSDBase.h:113
void initializeHess() override
even though the Hessian of the error norm w.r.t.
Definition: SSDBase.h:56
AMParams::ILM ilm
optional pointer to an illumination model if such a one is to be used as a photometric function ...
Definition: SSDBase.h:158
void updateInitGrad() override
nothing is done here since init_grad is same as and shares memory with curr_pix_diff that is updated ...
Definition: SSDBase.h:67
Definition: AMParams.h:12
base class for appearance models that use the negative sum of squared differences ("SSD") or L2 norm ...
Definition: SSDBase.h:16
int getStateSize() const override
accessor methods
Definition: SSDBase.h:53
Distance functor for FLANN.
Definition: AppearanceModel.h:40
void updateDistFeat(double *feat_addr) override
overloaded version to write the distance feature directly to a row (or column) of a matrix storing th...
Definition: SSDBase.h:116
double accum_dist(const double &a, const double &b, int) const
Partial euclidean distance, using just one dimension.
Definition: SSDBase.h:41
void updateDistFeat() override
computes a "distance" vector using the current image patch such that, when the distance vectors corre...
Definition: SSDBase.h:123
unsigned int getDistFeatSize() override
returns the size of the distance vector
Definition: SSDBase.h:125
Similarity function that indicates how well a candidate warped patch matches the template.
Definition: AppearanceModel.h:63