vivalib library
Concurrent C++11 OpenCV library for Computer Vision applications
viva.h
1 /**************************************************************************************************
2  **************************************************************************************************
3 
4  BSD 3-Clause License (https://www.tldrlegal.com/l/bsd3)
5 
6  Copyright (c) 2015 Andrés Solís Montero <http://www.solism.ca>, All rights reserved.
7 
8 
9  Redistribution and use in source and binary forms, with or without modification,
10  are permitted provided that the following conditions are met:
11 
12  1. Redistributions of source code must retain the above copyright notice,
13  this list of conditions and the following disclaimer.
14  2. Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17  3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32  **************************************************************************************************
33  **************************************************************************************************/
34 
35 #ifndef __viva__h
36 #define __viva__h
37 
38 #include <thread>
39 #include <functional>
40 #include <vector>
41 
42 #include "input.h"
43 #include "listener.h"
44 #include "output.h"
45 #include "channel.h"
46 
47 
48 using namespace std;
49 using namespace cv;
50 
51 namespace viva {
52 
58  {
59  std::thread& t;
60  public:
61  explicit thread_guard(std::thread& t_): t(t_)
62  {}
63  ~thread_guard()
64  {
65  if(t.joinable())
66  {
67  t.join();
68  }
69  }
70  thread_guard(thread_guard const&)=delete;
71  thread_guard& operator=(thread_guard const&)=delete;
72  };
73 
74  /*
75  * Parallel Processing Input and communicating through the channel
76  */
78  {
79  private:
80  Ptr<Input> _input;
81  Ptr<BufferedImageChannel> _channel;
82 
83  public:
84  ProcessInput(Ptr<Input> &input,
85  Ptr<BufferedImageChannel> &channel):
86  _input(input), _channel(channel)
87  {}
88 
89  void operator()();
90  };
91 
96  {
97  public:
98  virtual ~ProcessFrame(){}
99  virtual void operator()(const size_t frameN, const Mat &frame, Mat &output)
100  {
101  output = frame.clone();
102  };
103 
107  virtual void mouseInput(int event, int x, int y, int flags){};
111  virtual void leftButtonDown(int x, int y, int flags){};
115  virtual void rightButtonDown(int x, int y, int flags){};
119  virtual void middleButtonDown(int x, int y, int flags){};
123  virtual void mouseMove(int x, int y, int flags){};
124 
128  virtual void keyboardInput(int key){};
129 
130  };
131 
136  {
137  protected:
138  size_t _count;
139  public:
140  BatchProcessFrame(const size_t count = 10): _count(count){}
141  virtual ~BatchProcessFrame(){}
142 
143  virtual size_t batchProcessSize()
144  {
145  return _count;
146  }
147 
148  virtual void operator()(size_t frameN, const vector<Mat> &frames, Mat &output)
149  {
150  output = frames[0].clone();
151  };
152 
153  //Inherited from MouseListener. Check MouseListener class for details
154  virtual void mouseInput(int event, int x, int y, int flags){};
155  virtual void leftButtonDown(int x, int y, int flags){};
156  virtual void rightButtonDown(int x, int y, int flags){};
157  virtual void middleButtonDown(int x, int y, int flags){};
158  virtual void mouseMove(int x, int y, int flags){};
159 
160  //Inherited from KeyboardListerner. Check KeyboardListener class for details.
161  virtual void keyboardInput(int key){};
162 
163  };
164 
165 
170  {
171  private:
172  Ptr<Output> _output;
173  Ptr<BufferedImageChannel> _channel;
174 
175  public:
176  ProcessOutput(Ptr<Output> &output,
177  Ptr<BufferedImageChannel> &channel):
178  _output(output), _channel(channel)
179  {}
180 
181  void operator()();
182  };
183 
189  class Processor
190  {
191  private:
192  Ptr<Input> _input;
193  Ptr<ProcessFrame> _process;
194  Ptr<Output> _output;
195 
196  function<void(const size_t frameN, const Mat &frame, Mat &output)> _functor;
197 
198  string _inputWindowName;
199  string _outputWindowName;
200 
201  bool _showInput;
202  bool _showOutput;
203 
204  bool _mListener;
205  bool _kListener;
206 
207  size_t _inputBufferSize;
208  size_t _outputBufferSize;
209 
210  bool _showTimeInfo;
211  bool _pause;
212 
213  static void mouseCallback(int event, int x, int y, int flags, void *ptr);
214 
215  public:
216 
217  Processor():
218  _input(new CameraInput(0)),
219  _process(new ProcessFrame()),
220  _output(Ptr<Output>()),
221  _functor(nullptr),
222  _inputWindowName("Input"),
223  _outputWindowName("Process Output"),
224  _showInput(false),
225  _showOutput(true),
226  _mListener(false),
227  _kListener(false),
228  _inputBufferSize(10),
229  _outputBufferSize(10),
230  _showTimeInfo(false),
231  _pause(false)
232  {}
233 
234  Processor(int argc, const char * argv[]) : Processor()
235  {
236 
237  }
238  void setInputBufferSize(size_t size)
239  {
240  _inputBufferSize = size;
241  }
242 
243  void setOutputBufferSize(size_t size)
244  {
245  _outputBufferSize = size;
246  }
247 
248  void showInput(bool show = true)
249  {
250  _showInput = show;
251  }
252  void showTimeInfo(bool show = true)
253  {
254  _showTimeInfo = show;
255  }
256  void showOutput(bool show = true)
257  {
258  _showOutput = show;
259  }
260  void setInputWindowName(const string &name)
261  {
262  _inputWindowName = name;
263  }
264  void setOutputWindowName(const string &name)
265  {
266  _outputWindowName = name;
267  }
268  void setInput(Ptr<Input> &input)
269  {
270  _input = input;
271  }
272  void setOutput(Ptr<Output> &output)
273  {
274  _output = output;
275  }
276  void listenToMouseEvents()
277  {
278  _mListener = true;
279  }
280  void listenToKeyboardEvents()
281  {
282  _kListener = true;
283  }
284  void setProcess(Ptr<ProcessFrame> &process)
285  {
286  _process = process;
287  }
293  void startPaused()
294  {
295  _pause = true;
296  }
297 
301  void setProcess(function<void(const size_t frameN,const Mat &frame, Mat &output)> functor)
302  {
303  _functor = functor;
304  }
305 
313  void run();
314  };
315 
321  {
322  private:
323  size_t _batchSize;
324  Ptr<Input> _input;
325  Ptr<BatchProcessFrame> _batch_process;
326  Ptr<Output> _output;
327 
328  function<void(const size_t frameN, const vector<Mat> &frames, Mat &output)> _batch_functor;
329 
330  string _inputWindowName;
331  string _outputWindowName;
332 
333  bool _showInput;
334  bool _showOutput;
335 
336  bool _mListener;
337  bool _kListener;
338 
339  size_t _inputBufferSize;
340  size_t _outputBufferSize;
341 
342  bool _showTimeInfo;
343 
344  static void mouseCallback(int event, int x, int y, int flags, void *ptr);
345 
346  public:
347 
348  BatchProcessor(size_t batchSize = 10):
349  _batchSize(batchSize),
350  _input(new CameraInput(0)),
351  _batch_process(new BatchProcessFrame(_batchSize)),
352  _output(Ptr<Output>()),
353  _batch_functor(nullptr),
354  _inputWindowName("Input"),
355  _outputWindowName("Process Output"),
356  _showInput(false),
357  _showOutput(true),
358  _mListener(false),
359  _kListener(false),
360  _inputBufferSize(10),
361  _outputBufferSize(10),
362  _showTimeInfo(false)
363  {}
364 
365  void setInputBufferSize(size_t size)
366  {
367  _inputBufferSize = size;
368  }
369 
370  void setOutputBufferSize(size_t size)
371  {
372  _outputBufferSize = size;
373  }
374 
375  void showInput(bool show = true)
376  {
377  _showInput = show;
378  }
379  void showTimeInfo(bool show = true)
380  {
381  _showTimeInfo = show;
382  }
383  void showOutput(bool show = true)
384  {
385  _showOutput = show;
386  }
387  void setInputWindowName(const string &name)
388  {
389  _inputWindowName = name;
390  }
391  void setOutputWindowName(const string &name)
392  {
393  _outputWindowName = name;
394  }
398  void setInput(Ptr<Input> &input)
399  {
400  _input = input;
401  }
405  void setOutput(Ptr<Output> &output)
406  {
407  _output = output;
408  }
413  {
414  _mListener = true;
415  }
420  {
421  _kListener = true;
422  }
423 
427  void setBatchProcess(Ptr<BatchProcessFrame> &process)
428  {
429  _batch_process = process;
430  }
431 
435  void setBatchProcess(function<void(const size_t frameN, const vector<Mat> &frames, Mat &output)> functor)
436  {
437  _batch_functor = functor;
438  }
439 
443  void run();
444  };
445 
446 
447 
448 
449 
450 }
451 
452 
453 #endif
virtual void mouseInput(int event, int x, int y, int flags)
Definition: viva.h:154
Definition: viva.h:189
void startPaused()
Definition: viva.h:293
virtual void mouseMove(int x, int y, int flags)
Definition: viva.h:158
void setOutput(Ptr< Output > &output)
Definition: viva.h:405
virtual void mouseInput(int event, int x, int y, int flags)
Definition: viva.h:107
virtual void keyboardInput(int key)
Definition: viva.h:128
Definition: listener.h:47
virtual void keyboardInput(int key)
Definition: viva.h:161
void listenToMouseEvents()
Definition: viva.h:412
Definition: channel.h:45
void setBatchProcess(function< void(const size_t frameN, const vector< Mat > &frames, Mat &output)> functor)
Definition: viva.h:435
virtual void mouseMove(int x, int y, int flags)
Definition: viva.h:123
Definition: listener.h:60
Definition: viva.h:77
virtual void leftButtonDown(int x, int y, int flags)
Definition: viva.h:111
Definition: viva.h:135
Definition: viva.h:169
void setInput(Ptr< Input > &input)
Definition: viva.h:398
void setBatchProcess(Ptr< BatchProcessFrame > &process)
Definition: viva.h:427
Definition: viva.h:95
virtual void leftButtonDown(int x, int y, int flags)
Definition: viva.h:155
Definition: viva.h:320
Definition: input.h:127
virtual void rightButtonDown(int x, int y, int flags)
Definition: viva.h:156
void listenToKeyboardEvents()
Definition: viva.h:419
void setProcess(function< void(const size_t frameN, const Mat &frame, Mat &output)> functor)
Definition: viva.h:301
Definition: viva.h:57
virtual void middleButtonDown(int x, int y, int flags)
Definition: viva.h:119
virtual void rightButtonDown(int x, int y, int flags)
Definition: viva.h:115
virtual void middleButtonDown(int x, int y, int flags)
Definition: viva.h:157