mexopencv  3.4.1
MEX interface for OpenCV library
VideoWriter_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/videoio.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 namespace {
14 // Persistent objects
16 int last_id = 0;
19 
22  ("Any", cv::CAP_ANY)
23  ("VfW", cv::CAP_VFW)
24  ("QuickTime", cv::CAP_QT)
25  ("AVFoundation", cv::CAP_AVFOUNDATION)
26  ("MediaFoundation", cv::CAP_MSMF)
27  ("GStreamer", cv::CAP_GSTREAMER)
28  ("FFMPEG", cv::CAP_FFMPEG)
29  ("Images", cv::CAP_IMAGES)
30  ("MotionJPEG", cv::CAP_OPENCV_MJPEG)
31  ("MediaSDK", cv::CAP_INTEL_MFX);
32 
35  ("Quality", cv::VIDEOWRITER_PROP_QUALITY)
36  ("FrameBytes", cv::VIDEOWRITER_PROP_FRAMEBYTES)
37  ("NStripes", cv::VIDEOWRITER_PROP_NSTRIPES)
38  ("Images", cv::CAP_PROP_IMAGES_BASE);
39 
47 
50  //("Int", cv::IMWRITE_EXR_TYPE_UNIT)
52  ("Float", cv::IMWRITE_EXR_TYPE_FLOAT);
53 
59  ("GrayscaleAlpha", cv::IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA)
62 
65 {
69  int fourcc;
71  double fps;
73  bool isColor;
74 
81  : apiPreference(cv::CAP_ANY),
82  fourcc(CV_FOURCC('M','J','P','G')),
83  fps(25),
84  isColor(true)
85  {
86  nargchk((std::distance(first, last) % 2) == 0);
87  for (; first != last; first += 2) {
88  string key((*first).toString());
89  const MxArray& val = *(first + 1);
90  if (key == "API")
91  apiPreference = ApiPreferenceMap[val.toString()];
92  else if (key == "FourCC") {
93  if (val.isChar() && val.numel()==4) {
94  string cc(val.toString());
95  fourcc = VideoWriter::fourcc(cc[0], cc[1], cc[2], cc[3]);
96  }
97  else
98  fourcc = val.toInt();
99  }
100  else if (key == "FPS")
101  fps = val.toDouble();
102  else if (key == "Color")
103  isColor = val.toBool();
104  else
105  mexErrMsgIdAndTxt("mexopencv:error",
106  "Unrecognized option %s", key.c_str());
107  }
108  }
109 };
110 
113 {
116 
123  {
124  nargchk((std::distance(first, last) % 2) == 0);
125  for (; first != last; first += 2) {
126  string key((*first).toString());
127  const MxArray& val = *(first + 1);
128  if (key == "JpegQuality") {
131  params.push_back(val.toInt());
132  }
133  else if (key == "JpegProgressive") {
136  params.push_back(val.toBool() ? 1 : 0);
137  }
138  else if (key == "JpegOptimize") {
141  params.push_back(val.toBool() ? 1 : 0);
142  }
143  else if (key == "JpegResetInterval") {
146  params.push_back(val.toInt());
147  }
148  else if (key == "JpegLumaQuality") {
151  params.push_back(val.toInt());
152  }
153  else if (key == "JpegChromaQuality") {
156  params.push_back(val.toInt());
157  }
158  else if (key == "PngCompression") {
161  params.push_back(val.toInt());
162  }
163  else if (key == "PngStrategy") {
166  params.push_back(PngStrategyMap[val.toString()]);
167  }
168  else if (key == "PngBilevel") {
171  params.push_back(val.toBool() ? 1 : 0);
172  }
173  else if (key == "PxmBinary") {
176  params.push_back(val.toBool() ? 1 : 0);
177  }
178  else if (key == "ExrType") {
181  params.push_back(ExrTypeMap[val.toString()]);
182  }
183  else if (key == "WebpQuality") {
186  params.push_back(val.toInt());
187  }
188  else if (key == "PamTupleType") {
191  params.push_back(PamFormatMap[val.toString()]);
192  }
193  else
194  mexErrMsgIdAndTxt("mexopencv:error",
195  "Unrecognized option %s", key.c_str());
196  }
197  }
198 };
199 } // anonymous namespace
200 
208 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
209 {
210  // Check the number of arguments
211  nargchk(nrhs>=2 && nlhs<=1);
212 
213  // Argument vector
214  vector<MxArray> rhs(prhs, prhs+nrhs);
215  int id = rhs[0].toInt();
216  string method(rhs[1].toString());
217 
218  // Constructor is called. Create a new object from arguments
219  if (method == "new") {
220  nargchk(nrhs==2 && nlhs<=1);
221  obj_[++last_id] = makePtr<VideoWriter>();
222  plhs[0] = MxArray(last_id);
223  mexLock();
224  return;
225  }
226 
227  // Big operation switch
228  Ptr<VideoWriter> obj = obj_[id];
229  if (obj.empty())
230  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
231  if (method == "delete") {
232  nargchk(nrhs==2 && nlhs==0);
233  obj_.erase(id);
234  mexUnlock();
235  }
236  else if (method == "open") {
237  nargchk(nrhs>=4 && nlhs<=1);
238  string filename(rhs[2].toString());
239  Size frameSize(rhs[3].toSize());
240  OptionsParser opts(rhs.begin() + 4, rhs.end());
241  bool b = obj->open(filename, opts.apiPreference,
242  opts.fourcc, opts.fps, frameSize, opts.isColor);
243  plhs[0] = MxArray(b);
244  }
245  else if (method == "isOpened") {
246  nargchk(nrhs==2 && nlhs<=1);
247  bool b = obj->isOpened();
248  plhs[0] = MxArray(b);
249  }
250  else if (method == "release") {
251  nargchk(nrhs==2 && nlhs==0);
252  obj->release();
253  }
254  else if (method == "write") {
255  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
256  bool flip = true;
257  for (int i=3; i<nrhs; i+=2) {
258  string key(rhs[i].toString());
259  if (key == "FlipChannels")
260  flip = rhs[i+1].toBool();
261  else
262  mexErrMsgIdAndTxt("mexopencv:error",
263  "Unrecognized option %s", key.c_str());
264  }
265  Mat frame(rhs[2].toMat());
266  if (flip && frame.channels() == 3)
267  cvtColor(frame, frame, cv::COLOR_RGB2BGR);
268  obj->write(frame);
269  }
270  else if (method == "get") {
271  nargchk(nrhs==3 && nlhs<=1);
272  int propId = (rhs[2].isChar()) ?
273  VidWriterProp[rhs[2].toString()] : rhs[2].toInt();
274  double value = obj->get(propId);
275  plhs[0] = MxArray(value);
276  }
277  else if (method == "set") {
278  nargchk(nrhs==4 && nlhs==0);
279  int propId = (rhs[2].isChar()) ?
280  VidWriterProp[rhs[2].toString()] : rhs[2].toInt();
281  if (propId == cv::CAP_PROP_IMAGES_BASE) {
282  vector<MxArray> args(rhs[3].toVector<MxArray>());
283  ImwriteOptionsParser opts(args.begin(), args.end());
284  nargchk((opts.params.size() % 2) == 0);
285  for (size_t i = 0; i < opts.params.size(); i+=2) {
286  bool success = obj->set(opts.params[i], opts.params[i+1]);
287  if (!success)
288  mexWarnMsgIdAndTxt("mexopencv:error",
289  "Error setting property %d", opts.params[i]);
290  }
291  }
292  else {
293  double value = rhs[3].toDouble();
294  bool success = obj->set(propId, value);
295  if (!success)
296  mexWarnMsgIdAndTxt("mexopencv:error",
297  "Error setting property %d", propId);
298  }
299  }
300  else
301  mexErrMsgIdAndTxt("mexopencv:error",
302  "Unrecognized operation %s", method.c_str());
303 }
int fourcc
4-character code of codec used to compress the frames.
virtual bool isOpened() const
COLOR_RGB2BGR
IMWRITE_PAM_TUPLETYPE
virtual void release()
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
const ConstMap< string, int > PamFormatMap
PAM tuple types for option processing.
T distance(T... args)
IMWRITE_JPEG_QUALITY
IMWRITE_PNG_STRATEGY_FILTERED
vector< int > params
vector of parameters as key/value pairs
const ConstMap< string, int > ExrTypeMap
EXR storage types for option processing.
LIBMWMEX_API_EXTERN_C void mexWarnMsgIdAndTxt(const char *identifier, const char *warn_msg,...)
Invoke a warning with message identifier &#39;identifier&#39; and message derived from &#39;fmt&#39; and subsequent a...
CAP_IMAGES
CAP_GSTREAMER
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
IMWRITE_JPEG_OPTIMIZE
CAP_FFMPEG
IMWRITE_PNG_COMPRESSION
STL namespace.
IMWRITE_PNG_STRATEGY_FIXED
CAP_INTEL_MFX
VIDEOWRITER_PROP_QUALITY
IMWRITE_WEBP_QUALITY
T end(T... args)
virtual double get(int propId) const
IMWRITE_PAM_FORMAT_RGB_ALPHA
IMWRITE_PNG_BILEVEL
bool isColor
Flag to indicate whether to expect color or grayscale frames.
IMWRITE_EXR_TYPE_FLOAT
virtual void write(const Mat &image)
virtual bool open(const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
CAP_MSMF
T push_back(T... args)
double fps
Framerate of the created video stream.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
IMWRITE_EXR_TYPE_HALF
Option arguments parser used by constructor and open method.
Option arguments parser for imwrite options used by set method.
IMWRITE_JPEG_RST_INTERVAL
#define true
Definition: tmwtypes.h:817
VIDEOWRITER_PROP_FRAMEBYTES
CAP_PROP_IMAGES_BASE
const ConstMap< string, int > VidWriterProp
Capture Property map for option processing.
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
IMWRITE_PAM_FORMAT_RGB
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
IMWRITE_PNG_STRATEGY
IMWRITE_PXM_BINARY
virtual bool set(int propId, double value)
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY
int CV_FOURCC(char c1, char c2, char c3, char c4)
IMWRITE_JPEG_LUMA_QUALITY
IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA
const ConstMap< string, int > ApiPreferenceMap
API backends map for option processing.
int last_id
Last object id to allocate.
IMWRITE_PAM_FORMAT_GRAYSCALE
STL class.
bool empty() const
ImwriteOptionsParser(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Parse input arguments.
IMWRITE_PNG_STRATEGY_DEFAULT
Global constant definitions.
T begin(T... args)
IMWRITE_JPEG_PROGRESSIVE
map< int, Ptr< VideoWriter > > obj_
Object container.
VIDEOWRITER_PROP_NSTRIPES
T c_str(T... args)
IMWRITE_PNG_STRATEGY_RLE
const ConstMap< string, int > PngStrategyMap
PNG encoding strategies for option processing.
CAP_OPENCV_MJPEG
OptionsParser(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Parse input arguments.
IMWRITE_PAM_FORMAT_BLACKANDWHITE
void flip(InputArray src, OutputArray dst, int flipCode)
IMWRITE_PAM_FORMAT_NULL
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const
int channels() const
IMWRITE_EXR_TYPE
CAP_AVFOUNDATION
IMWRITE_JPEG_CHROMA_QUALITY