MTF
ImageBase.h
1 #ifndef MTF_IMAGE_BASE_H
2 #define MTF_IMAGE_BASE_H
3 
4 #define mc_not_implemeted(func_name, n_channels) \
5  throw mtf::utils::FunctonNotImplemented(cv::format("%s :: %d channel images are not supported yet", #func_name,n_channels))
6 
7 #define WARPED_GRAD_EPS 1e-8
8 #define GRAD_EPS 1e-8
9 #define HESS_EPS 1
10 #define UCHAR_INPUT false
11 
12 
13 #define PIX_MAX 255.0
14 #define PIX_MIN 0.0
15 
16 #include "mtf/Macros/common.h"
17 #include "mtf/Utilities/excpUtils.h"
18 
19 _MTF_BEGIN_NAMESPACE
20 
21 struct ImgParams{
23  int resx, resy;
26  double grad_eps, hess_eps;
27  bool uchar_input;
28  ImgParams(int _resx, int _resy,
29  double _grad_eps = GRAD_EPS,
30  double _hess_eps = HESS_EPS,
31  bool _uchar_input = UCHAR_INPUT);
32  ImgParams(const ImgParams *img_params = nullptr);
33 };
34 
35 // set of indicator variables to keep track of which dependent state variables need updating and executing
36 // the update expression for any variable only when at least one of the other variables it depends on has
37 // been updated; this can help to increase speed by avoiding repeated computations
38 struct ImgStatus{
39  bool pix_vals, pix_grad, pix_hess;
40 
41  ImgStatus(){ clearPixState(); }
42 
43  void setPixState(){
44  pix_vals = pix_grad = pix_hess = true;
45  }
46  void clearPixState(){
47  pix_vals = pix_grad = pix_hess = false;
48  }
49 };
50 
51 class ImageBase{
52 
53 public:
55  enum class InputType{
56  MTF_32FC1 = CV_32FC1, MTF_32FC3 = CV_32FC3,
57  MTF_8UC1 = CV_8UC1, MTF_8UC3 = CV_8UC3
58  };
60  typedef EigImgT ImageT;
61 
62  explicit ImageBase(const ImgParams *img_params = nullptr,
63  const int _n_channels = 1);
64  virtual ~ImageBase(){}
65 
68  virtual int inputType() const { return static_cast<int>(input_type); }
69 
73  virtual const cv::Mat& getCurrImg() const{ return curr_img_cv; }
74  virtual unsigned int getImgHeight() const{ return img_height; }
75  virtual unsigned int getImgWidth() const{ return img_width; }
76  virtual unsigned int getResX() const{ return resx; }
77  virtual unsigned int getResY() const{ return resy; }
78  virtual unsigned int getNPix() const{ return n_pix; }
79  virtual unsigned int getNChannels() const{ return n_channels; }
80  virtual unsigned int getPatchSize() const{ return patch_size; }
81  virtual double getGradOffset() const{ return grad_eps; }
82  virtual double getHessOffset() const{ return hess_eps; }
83  virtual const PixValT& getInitPixVals() const{ return I0; }
84  virtual const PixGradT& getInitPixGrad() const{ return dI0_dx; }
85  virtual const PixHessT& getInitPixHess() const{ return d2I0_dx2; }
86 
87  virtual const PixValT& getCurrPixVals() const{ return It; }
88  virtual const PixGradT& getCurrPixGrad() const{ return dIt_dx; }
89  virtual const PixHessT& getCurrPixHess() const{ return d2It_dx2; }
90 
92  virtual void setCurrImg(const cv::Mat &cv_img);
93  virtual void setInitPixVals(const PixValT &pix_vals){ I0 = pix_vals; }
94  virtual void setInitPixGrad(const PixGradT &pix_grad){ dI0_dx = pix_grad; }
95  virtual void setInitPixHess(const PixHessT &pix_hess){ d2I0_dx2 = pix_hess; }
96 
97  virtual void setCurrPixVals(const PixValT &pix_vals){ It = pix_vals; }
98  virtual void setCurrPixGrad(const PixGradT &pix_grad){ dIt_dx = pix_grad; }
99  virtual void setCurrPixHess(const PixHessT &pix_hess){ d2It_dx2 = pix_hess; }
100 
102  virtual void initializePixVals(const PtsT& init_pts);
107  virtual void initializePixGrad(const GradPtsT &warped_offset_pts);
109  virtual void initializePixGrad(const PtsT &init_pts);
110 
112  virtual void initializePixHess(const PtsT& init_pts, const HessPtsT &warped_offset_pts);
114  virtual void initializePixHess(const PtsT &init_pts);
115 
116  // -------- functions for updating state variables when a new image arrives -------- //
117  virtual void updatePixVals(const PtsT& curr_pts);
118 
119  virtual void updatePixGrad(const GradPtsT &warped_offset_pts);
120  virtual void updatePixGrad(const PtsT &curr_pts);
121 
122  virtual void updatePixHess(const PtsT &curr_pts);
123  virtual void updatePixHess(const PtsT& curr_pts, const HessPtsT &warped_offset_pts);
124 
127  virtual void extractPatch(VectorXd &pix_vals, const PtsT& curr_pts);
129  virtual VectorXd getPatch(const PtsT& curr_pts);
130 
131  virtual ImgStatus* isInitialized() = 0;
132 
133  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
134 
135 protected:
137  const unsigned int resx, resy;
139  const unsigned int n_pix;
142  const unsigned int n_channels;
144  const unsigned int patch_size;
146  const double grad_eps, hess_eps;
147  const InputType input_type;
148 
150  EigImgT curr_img;
152  cv::Mat curr_img_cv;
153 
155  unsigned int img_height, img_width;
156 
159  PixValT I0, It;
162  PixGradT dI0_dx, dIt_dx;
165  PixHessT d2I0_dx2, d2It_dx2;
167  double pix_norm_add, pix_norm_mult;
169  unsigned int frame_count;
170 
171 private:
172  InputType getInputType(const ImgParams *img_params){
173  bool uchar_input = img_params ? img_params->uchar_input : UCHAR_INPUT;
174  return uchar_input ?
175  n_channels == 1 ? InputType::MTF_8UC1 : InputType::MTF_8UC3 :
176  n_channels == 1 ? InputType::MTF_32FC1 : InputType::MTF_32FC3;
177  }
178  unsigned int getResX(const ImgParams *img_params){
179  return img_params ? static_cast<unsigned int>(img_params->resx) : MTF_RES;
180  }
181  unsigned int getResY(const ImgParams *img_params){
182  return img_params ? static_cast<unsigned int>(img_params->resy) : MTF_RES;
183  }
184  double getGradEps(const ImgParams *img_params){
185  return img_params ? img_params->grad_eps : GRAD_EPS;
186  }
187  double getHessEps(const ImgParams *img_params){
188  return img_params ? img_params->hess_eps : HESS_EPS;
189  }
190 
191 };
192 
193 _MTF_END_NAMESPACE
194 
195 #endif
196 
197 
198 
cv::Mat curr_img_cv
OpenCV image used by default with multi channel inputs.
Definition: ImageBase.h:152
EigImgT ImageT
convenience type if floating point grayscale image is used as input by the AM
Definition: ImageBase.h:60
int resx
horizontal and vertical sampling resolutions
Definition: ImageBase.h:23
double grad_eps
numerical increment/decrement used for computing image hessian and gradient using the method of finit...
Definition: ImageBase.h:26
unsigned int img_height
height and width of the input images
Definition: ImageBase.h:155
const unsigned int resx
horizontal and vertical sampling resolutions for the object patch
Definition: ImageBase.h:137
const unsigned int n_channels
no.
Definition: ImageBase.h:142
EigImgT curr_img
Eigen structure shaing memory with the OpenCV image used by default with grayscale inputs...
Definition: ImageBase.h:150
double pix_norm_add
additive and multiplicative factors for normalizing pixel values
Definition: ImageBase.h:167
PixGradT dI0_dx
(N*C) x 2 jacobian of pixel values in the warped image w.r.t.
Definition: ImageBase.h:162
virtual int inputType() const
return the type of OpenCV Mat image the AM requires as input; typically either CV_32FC3 or CV_32FC1 ...
Definition: ImageBase.h:68
const unsigned int n_pix
no. of pixels in the sampled image patch to be tracked
Definition: ImageBase.h:139
unsigned int frame_count
incremented once during initialization and thereafter everytime the template is updated ...
Definition: ImageBase.h:169
PixHessT d2I0_dx2
4 x (N*C) Hessian of pixel values in the warped image w.r.t.
Definition: ImageBase.h:165
Definition: ImageBase.h:51
Definition: ImageBase.h:21
PixValT I0
let N = n_pix = no.
Definition: ImageBase.h:159
InputType
supported input image types
Definition: ImageBase.h:55
virtual const cv::Mat & getCurrImg() const
accessor methods; these are not defined as &#39;const&#39; since an appearance model may like to make some la...
Definition: ImageBase.h:73
Definition: ImageBase.h:38
const unsigned int patch_size
size of the vector that represents the object patch in the image, typically n_pix*n_channels ...
Definition: ImageBase.h:144
const double grad_eps
offsets to use for computing the numerical image gradient and hessian
Definition: ImageBase.h:146