MTF
inputUtils.h
1 #ifndef MTF_INPUT_UTILS_H
2 #define MTF_INPUT_UTILS_H
3 
4 #include "mtf/Utilities/excpUtils.h"
5 #include "mtf/Macros/common.h"
6 
7 #include "opencv2/core/core.hpp"
8 #include "opencv2/highgui/highgui.hpp"
9 #include "boost/filesystem/operations.hpp"
10 
11 #ifndef DISABLE_VISP
12 #include <visp3/core/vpImage.h>
13 #include <visp3/core/vpFrameGrabber.h>
14 #endif
15 #ifndef DISABLE_XVISION
16 #include <XVVideo.h>
17 #include <XVMpeg.h>
18 #include <XVAVI.h>
19 #include <XVV4L2.h>
20 #include <XVDig1394.h>
21 #include <XVImageRGB.h>
22 #include <XVImageSeq.h>
23 #endif
24 
25 using namespace std;
26 namespace fs = boost::filesystem;
27 
28 _MTF_BEGIN_NAMESPACE
29 namespace utils{
30 
31  enum FrameType{ MUTABLE };
32 
33  class InputParams{
34  public:
35  InputParams(const InputParams *_params = nullptr);
37  char _img_source,
38  string _dev_name,
39  string _dev_fmt,
40  string _dev_path,
41  int _n_buffers,
42  bool _invert_seq);
43  char img_source;
44  string dev_name;
45  string dev_fmt;
46  string dev_path;
47  int n_buffers;
48  bool invert_seq;
49 
50  protected:
51  void setDefaults();
52  };
53 
54  class InputBase {
55  public:
56  bool destroy;
57 
58  InputBase(const InputParams *_params = nullptr);
59  virtual ~InputBase(){}
60  virtual bool initialize() = 0;
61  virtual bool update() = 0;
62 
63  virtual void remapBuffer(unsigned char **new_addr);
64 
65  virtual int getFrameID() const{ return frame_id; }
66  virtual const cv::Mat& getFrame() const{
67  return cv_buffer[buffer_id];
68  }
70  virtual cv::Mat& getFrame(FrameType frame_type){
71  return cv_buffer[buffer_id];
72  }
73  virtual int getNFrames() const{ return n_frames; }
74  virtual int getHeight() const{ return img_height; }
75  virtual int getWidth() const{ return img_width; }
80  virtual bool constBuffer(){ return const_buffer; }
81 #ifndef DISABLE_VISP
82  virtual void getFrame(vpImage<vpRGBa> &vp_img) const {
83  convert(getFrame(), vp_img);
84  }
85  virtual void convert(const vpImage<vpRGBa> &vp_img, cv::Mat &cv_img) const;
86  virtual void convert(const cv::Mat &cv_img, vpImage<vpRGBa> &vp_img) const;
87 #endif
88 
89  protected:
90  int n_frames, img_width, img_height, n_channels, frame_id;
91  int n_buffers, buffer_id, invert_seq;
92 
93  string file_path;
94  bool const_buffer;
95  vector<cv::Mat> cv_buffer;
96  };
97 
98  class InputCV : public InputBase {
99  public:
100  InputCV(const InputParams *_params = nullptr,
101  int _img_type = CV_8UC3);
102  ~InputCV();
103  bool initialize() override;
104  bool update() override;
105 
106  private:
107  InputParams params;
108  cv::VideoCapture cap_obj;
109  const int img_type;
110  };
111 
112 #ifndef DISABLE_VISP
113  class InputVPParams : public InputParams {
114  public:
115  enum class VpResUSB{
116  Default, res640x480, res800x600, res1024x768,
117  res1280x720, res1920x1080
118  };
119  enum class VpResFW{
120  Default, res640x480, res800x600, res1024x768,
121  res1280x960, res1600x1200
122  };
123  enum class VpDepthFW{
124  Default, RGB, YUV422, MONO8, MONO16
125  };
126  enum class VpFpsUSB{
127  Default, fps25, fps50
128  };
129  enum class VpFpsFW{
130  Default, fps15, fps30, fps60, fps120, fps240,
131  fps7_5, fps3_75, fps1_875
132  };
133  enum class VpISOFW{
134  Default, iso100, iso200, iso400,
135  iso800, iso1600, iso3200
136  };
137  InputVPParams(const InputParams *_params,
138  int _usb_n_buffers,
139  VpResUSB _usb_res,
140  VpFpsUSB _usb_fps,
141  VpResFW _fw_res,
142  VpFpsFW _fw_fps,
143  VpDepthFW _fw_depth,
144  VpISOFW _fw_iso,
145  bool _pg_fw_print_info,
146  float _pg_fw_shutter_ms,
147  float _pg_fw_gain,
148  float _pg_fw_exposure,
149  float _pg_fw_brightness);
150  InputVPParams(const InputVPParams *_params = nullptr);
151 
152  int usb_n_buffers;
153  VpResUSB usb_res;
154  VpFpsUSB usb_fps;
155  VpResFW fw_res;
156  VpFpsFW fw_fps;
157  VpDepthFW fw_depth;
158  VpISOFW fw_iso;
159  bool fw_print_info;
160  float pg_fw_shutter_ms;
161  float pg_fw_gain;
162  float pg_fw_exposure;
163  float pg_fw_brightness;
164  };
166  class InputVP : public InputBase {
167  public:
168  typedef InputVPParams::VpResUSB VpResUSB;
169  typedef InputVPParams::VpResFW VpResFW;
170  typedef InputVPParams::VpDepthFW VpDepthPGFW;
171  typedef InputVPParams::VpFpsUSB VpFpsUSB;
172  typedef InputVPParams::VpFpsFW VpFpsFW;
173  typedef InputVPParams::VpISOFW VpISOFW;
174 
175  InputVP(const InputVPParams *_params = nullptr);
176  ~InputVP();
177 
178  bool initialize() override;
179  bool update() override;
180 
181  bool constBuffer() override{ return true; }
182 
183  private:
184  InputVPParams params;
185  typedef vpImage<vpRGBa> VPImgType;
186  VPImgType vp_img;
187  std::unique_ptr<vpFrameGrabber> cap_obj;
188  };
189 #endif
190 #ifndef DISABLE_XVISION
191  /*
192  parameter format for XVDig1394::set_params is a string of "XnXn..."
193  where X is the parameter character and n is the corresponding value
194  valid characters and their values:
195  B : number of buffers ( same as defined in XVVideo.h )
196  R : pixel format from camera, 0 = YUV422, 1 = RGB, 2 = MONO8,
197  3 = MONO16, 4 = YUV411, 5 = YUV444
198  default = 0
199  S : scale, 0 = any, 1 = 640x480, 2 = 320x200, 3 = 160x120,
200  4 = 800x600, 5 = 1024x768, 6 = 1280x960, 7 = 1600x1200
201  default = 0
202  M : scale (obsolete), M0 = S4, M1 = S5, M2 = S6, M3 = S7
203  C : whether to grab center of the image or not, 0 = no, 1 = yes, default = 0
204  note that C1 implies format 7
205  T : external trigger, 0 = off, 1-4 = mode 0-3, or directly supply the
206  number to the camera. default = 0
207  f : directly set IEEE 1394 camera format (as oppose to set R and S/M)
208  if the value is 7, S values are still used.
209  m : directly set IEEE 1394 camera mode (as oppose to set R and S/M)
210  note that this parameter should not to set for format 7
211  r : frame rate, 0 = 1.875, 1 = 3.75, 2 = 7.5, 3 = 15, 4 = 30, 5 = 60 (fps)
212  default = fastest under selected format and mode
213  g : gain
214  u : u component for the white balance
215  v : v component for the white balance
216  s : saturation
217  A : sharpness
218  h : shutter
219  a : gamma
220  x : exposure
221  o : optical filter
222  i : bus init (resets the IEEE bus)
223  */
224  /*
225  parameter format for XVV4L2::set_params is a string of "XnXn..."
226  where X is the parameter character and n is the corresponding value
227  valid characters and their values:
228 
229  I: input
230  */
232  public:
233  typedef XV_RGB24 PIX_TYPE24;
234  typedef XVImageRGB< PIX_TYPE24 > IMAGE_TYPE24;
235  typedef XV_RGB PIX_TYPE32;
236  typedef XVImageRGB< PIX_TYPE32 > IMAGE_TYPE32;
237 
238  typedef XVVideo< IMAGE_TYPE24 > VID24;
239  typedef XVVideo< IMAGE_TYPE32 > VID32;
240 
241  typedef XVV4L2< IMAGE_TYPE24 > V4L2;
242  typedef XVDig1394< IMAGE_TYPE24 > DIG1394;
243  typedef XVImageSeq< IMAGE_TYPE24 > IMG;
244  typedef XVMpeg< IMAGE_TYPE32 > MPG;
245 
246  InputXVSource(){}
247  virtual ~InputXVSource(){}
248 
249  int img_height, img_width;
250  virtual void initFrame(int) = 0;
251  virtual void updateFrame(int) = 0;
252  virtual void initBuffer(PIX_TYPE24**, IMAGE_TYPE24*) = 0;
253  virtual void updateBuffer(PIX_TYPE24**) = 0;
254  };
255  class InputXV24 : public InputXVSource{
256  public:
257  InputXV24(char img_source,
258  string dev_path, string dev_fmt, string file_path,
259  int n_buffers_in, int n_frames);
260  void initFrame(int init_buffer_id) override;
261  void initBuffer(PIX_TYPE24 **init_addrs,
262  IMAGE_TYPE24 *buffer_addrs) override;
263  void updateFrame(int buffer_id) override;
264  void updateBuffer(PIX_TYPE24 **new_addrs) override;
265  private:
266  std::unique_ptr<VID24> vid;
267  IMAGE_TYPE24 *vid_buffers;
268  int n_buffers;
269  };
270  class InputXV32 : public InputXVSource{
271  public:
272  InputXV32(int img_source, string file_path);
273  void initFrame(int init_buffer_id) override;
274  void initBuffer(PIX_TYPE24 **init_addrs,
275  IMAGE_TYPE24 *buffer_addrs) override;
276  // XVMpeg does not support 24 bit images so it has to be used to
277  // capture 32 bit images and then copy data to the 24 bit buffers
278  void updateFrame(int buffer_id24) override;
279  void updateBuffer(PIX_TYPE24 **new_addrs) override{}
280  // copy image data from 32 bit source image to 24 bit buffer image
281  private:
282  std::unique_ptr<VID32> vid;
283  IMAGE_TYPE24 *buffer24;
284  int buffer_id32;
285  void copyFrame32ToBuffer24(IMAGE_TYPE32 &src_frame, int buffer_id24);
286  };
287 
288  class InputXV : public InputBase {
289 
290  public:
291  typedef float PIX_TYPE_GS;
292  typedef XV_RGB24 PIX_TYPE;
293  typedef XVImageRGB<PIX_TYPE> IMAGE_TYPE;
294  typedef XVImageScalar<PIX_TYPE_GS> IMAGE_TYPE_GS;
295 
296  InputXV(const InputParams *_params = nullptr) : InputBase(_params),
297  params(_params), src(nullptr){}
298 
299  ~InputXV(){
300  cv_buffer.clear();
301  xv_buffer.clear();
302  }
303  bool initialize() override;
304  bool update() override;
305  void remapBuffer(unsigned char** new_addr) override;
306  private:
307  InputParams params;
308  std::unique_ptr<InputXVSource> src;
309  vector<IMAGE_TYPE> xv_buffer;
310  void copyXVToCV(IMAGE_TYPE_GS &xv_img, cv::Mat &cv_img);
311  void copyXVToCVIter(IMAGE_TYPE_GS &xv_img, cv::Mat &cv_img);
312  void copyXVToCVIter(IMAGE_TYPE &xv_img, cv::Mat &cv_img);
313  };
314 #endif
315 
319  class InputDummy : public InputBase {
320  cv::Mat curr_img;
321  public:
322  InputDummy(const cv::Mat &img) : InputBase(), curr_img(img){}
323  ~InputDummy(){}
324  bool initialize() override{ return true; }
325  bool update() override{ return true; }
326  void remapBuffer(unsigned char** new_addr) override{}
327  const cv::Mat& getFrame() const override{ return curr_img; }
328  cv::Mat& getFrame(FrameType) override{ return curr_img; }
329  int getFrameID() const override{ return 0; }
330  int getHeight() const override{ return curr_img.rows; }
331  int getWidth() const override{ return curr_img.cols; }
332  };
333  int getNumberOfFrames(const char *file_template);
334  int getNumberOfVideoFrames(const char *file_name);
335 }
336 _MTF_END_NAMESPACE
337 #endif
Definition: inputUtils.h:288
dummy input pipeline to always provide a fixed image; needed to ensure API uniformity of the interact...
Definition: inputUtils.h:319
Definition: inputUtils.h:33
Definition: inputUtils.h:270
bool constBuffer() override
return true if the memory address of the output image buffer is constant, i.e.
Definition: inputUtils.h:181
virtual cv::Mat & getFrame(FrameType frame_type)
apparently a const function cannot return a non const object by reference
Definition: inputUtils.h:70
Definition: inputUtils.h:255
Definition: inputUtils.h:231
Definition: inputUtils.h:98
Definition: inputUtils.h:113
Definition: inputUtils.h:54
virtual bool constBuffer()
return true if the memory address of the output image buffer is constant, i.e.
Definition: inputUtils.h:80
cv::Mat & getFrame(FrameType) override
apparently a const function cannot return a non const object by reference
Definition: inputUtils.h:328
ViSP input pipeline.
Definition: inputUtils.h:166
basic functions for preprocessing the raw input image using filtering, resizing and histogram equaliz...
Definition: histUtils.h:20