mexopencv  0.1
mex interface for opencv library
VideoWriter_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
13 // Persistent objects
15 int last_id = 0;
17 map<int,Ptr<VideoWriter> > obj_;
18 
20 const ConstMap<string,int> VidWriterProp = ConstMap<string,int>
21  ("Quality", cv::VIDEOWRITER_PROP_QUALITY)
22  ("FrameBytes", cv::VIDEOWRITER_PROP_FRAMEBYTES)
23  ("NStripes", cv::VIDEOWRITER_PROP_NSTRIPES);
24 
26 struct OptionsParser
27 {
29  int fourcc;
31  double fps;
33  bool isColor;
34 
39  OptionsParser(vector<MxArray>::const_iterator first,
40  vector<MxArray>::const_iterator last)
41  : fourcc(CV_FOURCC('U','2','6','3')), // H263 codec
42  fps(25),
43  isColor(true)
44  {
45  nargchk(((last-first) % 2) == 0);
46  for (; first != last; first += 2) {
47  string key((*first).toString());
48  const MxArray& val = *(first + 1);
49  if (key == "FourCC") {
50  if (val.isChar() && val.numel()==4) {
51  string cc(val.toString());
52  fourcc = VideoWriter::fourcc(cc[0], cc[1], cc[2], cc[3]);
53  }
54  else
55  fourcc = val.toInt();
56  }
57  else if (key == "FPS")
58  fps = val.toDouble();
59  else if (key == "Color")
60  isColor = val.toBool();
61  else
62  mexErrMsgIdAndTxt("mexopencv:error",
63  "Unrecognized option %s", key.c_str());
64  }
65  }
66 };
67 } // anonymous namespace
68 
76 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
77 {
78  // Check the number of arguments
79  nargchk(nrhs>=2 && nlhs<=1);
80 
81  // Argument vector
82  vector<MxArray> rhs(prhs, prhs+nrhs);
83  int id = rhs[0].toInt();
84  string method(rhs[1].toString());
85 
86  // Constructor is called. Create a new object from arguments
87  if (method == "new") {
88  nargchk(nrhs==2 && nlhs==1);
89  obj_[++last_id] = makePtr<VideoWriter>();
90  plhs[0] = MxArray(last_id);
91  return;
92  }
93 
94  // Big operation switch
95  Ptr<VideoWriter> obj = obj_[id];
96  if (method == "delete") {
97  nargchk(nrhs==2 && nlhs==0);
98  obj_.erase(id);
99  }
100  else if (method == "open") {
101  nargchk(nrhs>=4 && nlhs<=1);
102  string filename(rhs[2].toString());
103  Size frameSize(rhs[3].toSize());
104  OptionsParser opts(rhs.begin() + 4, rhs.end());
105  bool b = obj->open(filename, opts.fourcc, opts.fps, frameSize,
106  opts.isColor);
107  plhs[0] = MxArray(b);
108  }
109  else if (method == "isOpened") {
110  nargchk(nrhs==2 && nlhs<=1);
111  bool b = obj->isOpened();
112  plhs[0] = MxArray(b);
113  }
114  else if (method == "release") {
115  nargchk(nrhs==2 && nlhs==0);
116  obj->release();
117  }
118  else if (method == "write") {
119  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
120  bool flip = true;
121  for (int i=3; i<nrhs; i+=2) {
122  string key(rhs[i].toString());
123  if (key == "FlipChannels")
124  flip = rhs[i+1].toBool();
125  else
126  mexErrMsgIdAndTxt("mexopencv:error",
127  "Unrecognized option %s", key.c_str());
128  }
129  Mat frame(rhs[2].toMat());
130  if (flip && frame.channels() == 3)
131  cvtColor(frame, frame, cv::COLOR_RGB2BGR);
132  obj->write(frame);
133  }
134  else if (method == "get") {
135  nargchk(nrhs==3 && nlhs<=1);
136  string prop(rhs[2].toString());
137  double value = obj->get(VidWriterProp[prop]);
138  plhs[0] = MxArray(value);
139  }
140  else if (method == "set") {
141  nargchk(nrhs==4 && nlhs==0);
142  string prop(rhs[2].toString());
143  double value = rhs[3].toDouble();
144  bool success = obj->set(VidWriterProp[prop], value);
145  if (!success)
146  mexWarnMsgIdAndTxt("mexopencv:error",
147  "Error setting property %s", prop.c_str());
148  }
149  else
150  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
151 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927