MTF
NCC.h
1 #ifndef MTF_NCC_H
2 #define MTF_NCC_H
3 
4 #include "AppearanceModel.h"
5 
6 _MTF_BEGIN_NAMESPACE
7 
8 struct NCCParams : AMParams{
9  bool fast_hess;
10 
12  NCCParams(const AMParams *am_params,
13  bool _fast_hess);
15  NCCParams(const NCCParams *params = nullptr);
16 };
17 
18 struct NCCDist : AMDist{
19  typedef bool is_kdtree_distance;
20  typedef double ElementType;
21  typedef double ResultType;
22  NCCDist(const string &_name, const unsigned int _patch_size) :
23  AMDist(_name), patch_size(_patch_size){}
24  double operator()(const double* a, const double* b,
25  size_t size, double worst_dist = -1) const override;
26  double accum_dist(const double& a, const double& b, int) const{ return -a*b; }
27 private:
28  const unsigned int patch_size;
29 };
30 
32 class NCC : public AppearanceModel{
33 public:
34  typedef NCCParams ParamType;
35  typedef NCCDist DistType;
36 
37  NCC(const ParamType *ncc_params = nullptr, const int _n_channels = 1);
38 
39  double getLikelihood() const override;
40  //-------------------------------initialize functions------------------------------------//
41  void initializeSimilarity() override;
42  void initializeGrad() override;
43  void initializeHess() override {}
44 
45  //-------------------------------update functions------------------------------------//
46  void updateSimilarity(bool prereq_only = true) override;
47  void updateInitGrad() override;
48  // nothing is done here since curr_grad is same as and shares memory with curr_pix_diff
49  void updateCurrGrad() override;
50 
51  void cmptInitJacobian(RowVectorXd &df_dp,const MatrixXd &dI0_dp) override;
52  void cmptCurrJacobian(RowVectorXd &df_dp, const MatrixXd &dIt_dp) override;
53  void cmptDifferenceOfJacobians(RowVectorXd &df_dp_diff,
54  const MatrixXd &dI0_dpssm, const MatrixXd &dIt_dpssm) override;
55 
56  void cmptInitHessian(MatrixXd &init_hessian, const MatrixXd &init_pix_jacobian) override;
57  void cmptInitHessian(MatrixXd &init_hessian, const MatrixXd &init_pix_jacobian,
58  const MatrixXd &init_pix_hessian) override;
59 
60  void cmptCurrHessian(MatrixXd &curr_hessian, const MatrixXd &curr_pix_jacobian) override;
61  void cmptCurrHessian(MatrixXd &curr_hessian, const MatrixXd &curr_pix_jacobian,
62  const MatrixXd &curr_pix_hessian) override;
63 
64  void cmptSelfHessian(MatrixXd &self_hessian, const MatrixXd &curr_pix_jacobian) override;
65  void cmptSelfHessian(MatrixXd &self_hessian, const MatrixXd &curr_pix_jacobian,
66  const MatrixXd &curr_pix_hessian) override{
67  cmptSelfHessian(self_hessian, curr_pix_jacobian);
68  }
69 
70  void estimateOpticalFlow(std::vector<cv::Point2f> &curr_pts,
71  const cv::Mat &prev_img, const std::vector<cv::Point2f> &prev_pts,
72  const cv::Size &win_size, unsigned int n_pts, int max_iters,
73  double term_eps, bool const_grad=true) const override;
74  void updateModel(const Matrix2Xd& curr_pts) override;
75 
76  /*Support for FLANN library*/
77  VectorXd curr_feat_vec;
78  const DistType* getDistFunc() override{
79  return new DistType(name, patch_size);
80  }
81  void updateDistFeat(double* feat_addr) override;
82  void updateDistFeat() override{
83  updateDistFeat(curr_feat_vec.data());
84  }
85  unsigned int getDistFeatSize() override{ return patch_size; }
86  void initializeDistFeat() override{
87  curr_feat_vec.resize(getDistFeatSize());
88  }
89  const double* getDistFeat() override{ return curr_feat_vec.data(); }
90 
91 #ifndef DISABLE_SPI
92  bool supportsSPI() const override{ return n_channels == 1; }
93 #endif
94 
95 private:
96  ParamType params;
97 
99  double I0_mean, It_mean;
100  double a, b, c;
101  double bc, b2c;
102 
103  VectorXd I0_cntr, It_cntr;
104  VectorXd I0_cntr_c, It_cntr_b;
105  VectorXd df_dI0_ncntr, df_dIt_ncntr;
106  double df_dI0_ncntr_mean, df_dIt_ncntr_mean;
107  bool use_running_avg;
108 };
109 
110 _MTF_END_NAMESPACE
111 
112 #endif
Normalized Cross Correlation.
Definition: NCC.h:32
void initializeDistFeat() override
to be called once during initialization if any of the distance feature functionality is to be used ...
Definition: NCC.h:86
void updateDistFeat() override
computes a "distance" vector using the current image patch such that, when the distance vectors corre...
Definition: NCC.h:82
bool supportsSPI() const override
should be overridden by an implementing class once it implements SPI functionality for all functions ...
Definition: NCC.h:92
void initializeHess() override
even though the Hessian of the error norm w.r.t.
Definition: NCC.h:43
Definition: AMParams.h:12
NCCParams(const AMParams *am_params, bool _fast_hess)
value constructor
Distance functor for FLANN.
Definition: AppearanceModel.h:40
Definition: NCC.h:8
Definition: NCC.h:18
unsigned int getDistFeatSize() override
returns the size of the distance vector
Definition: NCC.h:85
Similarity function that indicates how well a candidate warped patch matches the template.
Definition: AppearanceModel.h:63