vivalib library
Concurrent C++11 OpenCV library for Computer Vision applications
output.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__output__
36 #define __viva__output__
37 
38 #include "opencv2/opencv.hpp"
39 #include <iomanip>
40 #include "utils.h"
41 
42 using namespace cv;
43 using namespace std;
44 
45 namespace viva
46 {
50  class Output
51  {
52  protected:
53  Size _size;
54  bool _convert;
55  int _conversionFlag;
56  public:
61  Output(const Size &size = Size(-1, -1),
62  int conversionFlag = -1):
63  _size(size),
64  _convert(false),
65  _conversionFlag(conversionFlag)
66  {
67  if (conversionFlag != -1)
68  _convert = true;
69  }
73  virtual ~Output(){}
78  virtual bool writeFrame(Mat &frame)= 0;
79 
83  virtual void close() {}
84  };
85 
89  class NoneOutput : public Output
90  {
91  public:
92  NoneOutput(const Size &size = Size(-1, -1), int colorFlag = -1):
93  Output(size,colorFlag)
94  {}
95  bool writeFrame(Mat &frame)
96  {
97  return true;
98  }
99  };
100 
105  class ImageOutput: public Output
106  {
107  private:
108  string _base;
109  string _ext;
110  int _sSize;
111  size_t _internalCount;
112  int _suffix;
113 
114  public:
115 
121  ImageOutput(const string &directory,
122  const Size &size = Size(-1, -1),
123  int suffixSize = 5,
124  int conversionFlag = -1);
125 
129  virtual bool writeFrame(Mat &frame);
130 
131  };
132 
133 
134 
141  enum class CODEC : int {
142  LIST = CV_FOURCC_PROMPT,
143  MPG1 = CV_FOURCC_MACRO('P','I','M','1'), //MPEG-1
144  MJPEG = CV_FOURCC_MACRO('M','J','P','G'), //Motion-jpeg
145  MPEG42= CV_FOURCC_MACRO('M', 'P', '4', '2'), //MPEG-4.2
146  MPEG43= CV_FOURCC_MACRO('D', 'I', 'V', '3'), //MPEG-4.3
147  MPEG4 = CV_FOURCC_MACRO('D', 'I', 'V', 'X'), //MPEG-4
148  H263 = CV_FOURCC_MACRO('U', '2', '6', '3'), //H263
149  H263I = CV_FOURCC_MACRO('I', '2', '6', '3'), //H263I
150  FLV1 = CV_FOURCC_MACRO('F', 'L', 'V', '1'), //FLV1
151  H264 = CV_FOURCC_MACRO('X','2','6','4') //H.264
152  };
153 
159  class VideoOutput: public Output
160  {
161  VideoWriter output;
162 
163  bool _opened;
164  int _fps;
165  string _filename;
166  CODEC _codec;
167  void createOutput();
168 
169  public:
178  VideoOutput(const string &filename,
179  Size size = Size(-1,-1),
180  int fps = 30,
181  CODEC codec = CODEC::MPEG4,
182  int codeFlag = -1);
183 
187  virtual bool writeFrame(Mat &frame);
188 
192  void setCodec(CODEC codec)
193  {
194  _codec = codec;
195  }
196 
201  {
202  output.release();
203  }
204  };
205 
206 
207 }
208 
209 
210 #endif /* defined(__viva__output__) */
void setCodec(CODEC codec)
Definition: output.h:192
bool writeFrame(Mat &frame)
Definition: output.h:95
~VideoOutput()
Definition: output.h:200
Definition: channel.h:45
Definition: output.h:105
Definition: output.h:159
virtual void close()
Definition: output.h:83
Output(const Size &size=Size(-1,-1), int conversionFlag=-1)
Definition: output.h:61
Definition: output.h:50
virtual ~Output()
Definition: output.h:73
Definition: output.h:89