MTF
preprocUtils.h
1 #ifndef MTF_PRE_PROC_UTILS_H
2 #define MTF_PRE_PROC_UTILS_H
3 
10 #include "mtf/Utilities/excpUtils.h"
11 #include "mtf/Utilities/imgUtils.h"
12 #include "opencv2/imgproc/imgproc.hpp"
13 #include <memory>
14 
15 _MTF_BEGIN_NAMESPACE
16 namespace utils{
17 
18  const vector<int> supported_output_types = { CV_32FC3, CV_32FC1, CV_8UC3, CV_8UC1 };
19 
20  struct PreProcBase{
21  typedef std::shared_ptr<PreProcBase> Ptr;
26  Ptr next;
27  PreProcBase(const std::string &name, int _output_type = CV_32FC1,
28  double _resize_factor = 1, bool _hist_eq = false);
29  virtual ~PreProcBase(){
30  release();
31  }
32  virtual void initialize(const cv::Mat &frame_raw, int _frame_id = -1, bool print_types = true);
33  virtual void update(const cv::Mat &frame_raw, int _frame_id = -1){
34  if(_frame_id > 0 && frame_id == _frame_id){ return; }// this frame has already been processed
35  frame_id = _frame_id;
36  processFrame(frame_raw);
37  if(next.get()){ next->update(frame_raw, _frame_id); }
38  }
39  virtual const cv::Mat& getFrame(){
40  return resize_images ? frame_resized : rgb_output ? frame_rgb : frame_gs;
41  }
42  virtual std::string type(){ return _type; }
43  virtual void showFrame(std::string window_name);
44  virtual int outputType() const{ return output_type; }
45  virtual void setFrameID(int _frame_id){ frame_id = _frame_id; }
46  virtual int getFrameID() const{ return frame_id; }
47  virtual int getWidth() { return getFrame().cols; }
48  virtual int getHeight() { return getFrame().rows; }
49 
50  protected:
51  cv::Mat frame_rgb, frame_gs, frame_rgb_uchar;
52  cv::Mat frame_resized;
53  int output_type;
54  int frame_id;
55  bool rgb_input, rgb_output;
56  double resize_factor;
57  bool resize_images;
58  const bool hist_eq;
60  std::string _type;
61 
62  virtual void apply(cv::Mat &img_gs) const = 0;
63  virtual void processFrame(const cv::Mat &frame_raw);
64  void release();
65  };
66 
67  struct GaussianSmoothing : public PreProcBase{
69  int _output_type = CV_32FC1, double _resize_factor = 1, bool _hist_eq = false,
70  int _kernel_size = 5, double _sigma_x = 3.0, double _sigma_y = 0);
71  void apply(cv::Mat &img_gs) const override{
72  cv::GaussianBlur(img_gs, img_gs, kernel_size, sigma_x, sigma_y);
73  }
74  private:
75  cv::Size kernel_size;
76  double sigma_x;
77  double sigma_y;
78  };
79  struct MedianFiltering : public PreProcBase{
81  int _output_type = CV_32FC1, double _resize_factor = 1, bool _hist_eq = false,
82  int _kernel_size = 5);
83  void apply(cv::Mat &img_gs) const override{
84  cv::medianBlur(img_gs, img_gs, kernel_size);
85  }
86  private:
87  int kernel_size;
88  };
91  int _output_type = CV_32FC1, double _resize_factor = 1, bool _hist_eq = false,
92  int _kernel_size = 5);
93  void apply(cv::Mat &img_gs) const override{
94  cv::blur(img_gs, img_gs, kernel_size);
95  }
96  private:
97  cv::Size kernel_size;
98  };
101  int _output_type = CV_32FC1, double _resize_factor = 1, bool _hist_eq = false,
102  int _diameter = 5, double _sigma_col = 15, double _sigma_space = 15);
103  void apply(cv::Mat &img_gs) const override{
104  // OpenCV bilateral filterig does not work in place so a copy has to be made
105  cv::Mat orig_img = img_gs.clone();
106  //img_gs.copyTo(orig_img);
107  cv::bilateralFilter(orig_img, img_gs, diameter, sigma_col, sigma_space);
108  }
109  private:
110  int diameter;
111  double sigma_col;
112  double sigma_space;
113  };
114  struct SobelFltering : public PreProcBase{
115  SobelFltering(int _output_type = CV_32FC1, double _resize_factor = 1,
116  bool _hist_eq = false, int _kernel_size = 3, bool _normalize=false);
117  void initialize(const cv::Mat &frame_raw,
118  int _frame_id = -1, bool print_types = true) override;
119  void processFrame(const cv::Mat &frame_raw) override;
120  void apply(cv::Mat &img_gs) const override;
121  const cv::Mat& getFrame() override{
122  return frame_out;
123  }
124  void showFrame(std::string window_name) override;
125 
126  private:
127  cv::Mat frame_out, frame_in;
128  cv::Mat grad_x, grad_y, grad;
129  cv::Mat abs_grad_x, abs_grad_y, abs_grad;
130  const int kernel_size;
131  const bool normalize;
132  };
135  int _output_type = CV_32FC1, double _resize_factor = 1, bool _hist_eq = false,
136  double _lambda = 0.14285714285, double _k = 30, unsigned int _n_iters = 15);
137  void apply(cv::Mat &img_gs) const override{
138  mtf::utils::anisotropicDiffusion(img_gs, lambda, k, n_iters);
139  }
140  private:
141  double lambda;
142  double k;
143  unsigned int n_iters;
144  };
145  struct NoFiltering : public PreProcBase{
146  NoFiltering(int _output_type = CV_32FC1,
147  double _resize_factor = 1, bool _hist_eq = false) :
148  PreProcBase("NoFiltering",_output_type, _resize_factor, _hist_eq){
149  printf("Filtering is disabled\n");
150  }
151  void apply(cv::Mat &img_gs) const override{}
152  };
153  struct NoPreProcessing : public PreProcBase{
154  NoPreProcessing(int _output_type = CV_32FC1) :
155  PreProcBase("NoPreProcessing", _output_type){
156  printf("Pre processing is disabled\n");
157  }
158  void initialize(const cv::Mat &frame_raw,
159  int _frame_id = -1, bool print_types = true) override;
160  void update(const cv::Mat &frame_raw, int _frame_id = -1) override{
161  if(frame_raw.data != curr_frame.data){
162  throw mtf::utils::InvalidArgument("NoPreProcessing::update : Input image location in memory has changed");
163  }
164  frame_id = _frame_id;
165  }
166  void apply(cv::Mat &img_gs) const override{}
167  const cv::Mat& getFrame() override{
168  return curr_frame;
169  }
170  int outputType() const override{ return output_type; }
171  private:
172  cv::Mat curr_frame;
173  };
174 }
175 _MTF_END_NAMESPACE
176 #endif
Definition: preprocUtils.h:79
Definition: preprocUtils.h:89
Ptr next
linked list of shared PreProc pointers to deal with heterogeneous output types and multiple trackers ...
Definition: preprocUtils.h:26
Definition: excpUtils.h:38
Definition: preprocUtils.h:67
Definition: preprocUtils.h:20
Definition: preprocUtils.h:153
Definition: preprocUtils.h:145
std::string _type
unique ID to prevent creating duplicate pre processors with identical processing
Definition: preprocUtils.h:60
Definition: preprocUtils.h:114
Definition: preprocUtils.h:99
basic functions for preprocessing the raw input image using filtering, resizing and histogram equaliz...
Definition: histUtils.h:20
Definition: preprocUtils.h:133