mexopencv  3.4.1
MEX interface for OpenCV library
SuperResolution_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/superres.hpp"
10 #include <typeinfo>
11 using namespace std;
12 using namespace cv;
13 using namespace cv::superres;
14 // note ambiguity between: cv::DualTVL1OpticalFlow and cv::superres::DualTVL1OpticalFlow
15 // note ambiguity between: cv::FarnebackOpticalFlow and cv::superres::FarnebackOpticalFlow
16 
17 // Persistent objects
18 namespace {
20 int last_id = 0;
23 
35  const string& type,
38 {
39  ptrdiff_t len = std::distance(first, last);
40  nargchk(len==0 || len==1);
42  if (type == "Camera") {
43  int deviceId = (len==1) ? first->toInt() : 0;
44  p = createFrameSource_Camera(deviceId);
45  }
46  else if (type == "Video") {
47  nargchk(len==1);
48  string fileName(first->toString());
49  p = createFrameSource_Video(fileName);
50  }
51  //TODO: CUDA
52  //else if (type == "VideoCUDA") {
53  // nargchk(len==1);
54  // string fileName(first->toString());
55  // p = createFrameSource_Video_CUDA(fileName);
56  //}
57  //TODO: causes access violation
58  //else if (type == "Empty") {
59  // nargchk(len==0);
60  // p = createFrameSource_Empty();
61  //}
62  else
63  mexErrMsgIdAndTxt("mexopencv:error",
64  "Unrecognized frame source %s", type.c_str());
65  if (p.empty())
66  mexErrMsgIdAndTxt("mexopencv:error",
67  "Failed to create FrameSource of type %s", type.c_str());
68  return p;
69 }
70 
78  bool use_gpu,
81 {
82  ptrdiff_t len = std::distance(first, last);
83  nargchk((len%2)==0);
87  if (p.empty())
88  mexErrMsgIdAndTxt("mexopencv:error",
89  "Failed to create FarnebackOpticalFlow");
90  for (; first != last; first += 2) {
91  string key((*first).toString());
92  const MxArray& val = *(first + 1);
93  if (key == "PyrScale")
94  p->setPyrScale(val.toDouble());
95  else if (key == "LevelsNumber")
96  p->setLevelsNumber(val.toInt());
97  else if (key == "WindowSize")
98  p->setWindowSize(val.toInt());
99  else if (key == "Iterations")
100  p->setIterations(val.toInt());
101  else if (key == "PolyN")
102  p->setPolyN(val.toInt());
103  else if (key == "PolySigma")
104  p->setPolySigma(val.toDouble());
105  else if (key == "Flags")
106  p->setFlags(val.toInt()); //TODO: cv::OPTFLOW_FARNEBACK_GAUSSIAN
107  else
108  mexErrMsgIdAndTxt("mexopencv:error",
109  "Unrecognized option %s", key.c_str());
110  }
111  return p;
112 }
113 
121  bool use_gpu,
124 {
125  ptrdiff_t len = std::distance(first, last);
126  nargchk((len%2)==0);
127  Ptr<superres::DualTVL1OpticalFlow> p = (use_gpu) ?
130  if (p.empty())
131  mexErrMsgIdAndTxt("mexopencv:error",
132  "Failed to create DualTVL1OpticalFlow");
133  for (; first != last; first += 2) {
134  string key((*first).toString());
135  const MxArray& val = *(first + 1);
136  if (key == "Tau")
137  p->setTau(val.toDouble());
138  else if (key == "Lambda")
139  p->setLambda(val.toDouble());
140  else if (key == "Theta")
141  p->setTheta(val.toDouble());
142  else if (key == "ScalesNumber")
143  p->setScalesNumber(val.toInt());
144  else if (key == "WarpingsNumber")
145  p->setWarpingsNumber(val.toInt());
146  else if (key == "Epsilon")
147  p->setEpsilon(val.toDouble());
148  else if (key == "Iterations")
149  p->setIterations(val.toInt());
150  else if (key == "UseInitialFlow")
151  p->setUseInitialFlow(val.toBool());
152  else
153  mexErrMsgIdAndTxt("mexopencv:error",
154  "Unrecognized option %s", key.c_str());
155  }
156  return p;
157 }
158 
166  bool /*use_gpu*/,
169 {
170  ptrdiff_t len = std::distance(first, last);
171  nargchk((len%2)==0);
173  if (p.empty())
174  mexErrMsgIdAndTxt("mexopencv:error",
175  "Failed to create BroxOpticalFlow");
176  for (; first != last; first += 2) {
177  string key((*first).toString());
178  const MxArray& val = *(first + 1);
179  if (key == "Alpha")
180  p->setAlpha(val.toDouble());
181  else if (key == "Gamma")
182  p->setGamma(val.toDouble());
183  else if (key == "ScaleFactor")
184  p->setScaleFactor(val.toDouble());
185  else if (key == "InnerIterations")
186  p->setInnerIterations(val.toInt());
187  else if (key == "OuterIterations")
188  p->setOuterIterations(val.toInt());
189  else if (key == "SolverIterations")
190  p->setSolverIterations(val.toInt());
191  else
192  mexErrMsgIdAndTxt("mexopencv:error",
193  "Unrecognized option %s", key.c_str());
194  }
195  return p;
196 }
197 
205  bool /*use_gpu*/,
208 {
209  ptrdiff_t len = std::distance(first, last);
210  nargchk((len%2)==0);
212  if (p.empty())
213  mexErrMsgIdAndTxt("mexopencv:error",
214  "Failed to create PyrLKOpticalFlow");
215  for (; first != last; first += 2) {
216  string key((*first).toString());
217  const MxArray& val = *(first + 1);
218  if (key == "WindowSize")
219  p->setWindowSize(val.toInt());
220  else if (key == "MaxLevel")
221  p->setMaxLevel(val.toInt());
222  else if (key == "Iterations")
223  p->setIterations(val.toInt());
224  else
225  mexErrMsgIdAndTxt("mexopencv:error",
226  "Unrecognized option %s", key.c_str());
227  }
228  return p;
229 }
230 
244  const string& type,
247 {
249  if (type == "FarnebackOpticalFlow")
250  p = createFarnebackOpticalFlow(false, first, last);
251  else if (type == "DualTVL1OpticalFlow")
252  p = createDualTVL1OpticalFlow(false, first, last);
253  else if (type == "FarnebackOpticalFlowCUDA")
254  p = createFarnebackOpticalFlow(true, first, last);
255  else if (type == "DualTVL1OpticalFlowCUDA")
256  p = createDualTVL1OpticalFlow(true, first, last);
257  else if (type == "BroxOpticalFlowCUDA")
258  p = createBroxOpticalFlow(true, first, last);
259  else if (type == "PyrLKOpticalFlowCUDA")
260  p = createPyrLKOpticalFlow(true, first, last);
261  else
262  mexErrMsgIdAndTxt("mexopencv:error",
263  "Unrecognized optical flow %s", type.c_str());
264  if (p.empty())
265  mexErrMsgIdAndTxt("mexopencv:error",
266  "Failed to create DenseOpticalFlowExt of type %s", type.c_str());
267  return p;
268 }
269 
277 {
279  if (type == "BTVL1")
281  else if (type == "BTVL1_CUDA")
283  else
284  mexErrMsgIdAndTxt("mexopencv:error",
285  "Unrecognized super resolution %s", type.c_str());
286  if (p.empty())
287  mexErrMsgIdAndTxt("mexopencv:error",
288  "Failed to create SuperResolution of type %s", type.c_str());
289  return p;
290 }
291 
297 {
299  if (!p.empty()) {
300  s.set("TypeId", string(typeid(*p).name()));
301  {
303  if (!pp.empty()) {
304  s.set("PyrScale", pp->getPyrScale());
305  s.set("LevelsNumber", pp->getLevelsNumber());
306  s.set("WindowSize", pp->getWindowSize());
307  s.set("Iterations", pp->getIterations());
308  s.set("PolyN", pp->getPolyN());
309  s.set("PolySigma", pp->getPolySigma());
310  s.set("Flags", pp->getFlags());
311  }
312  }
313  {
315  if (!pp.empty()) {
316  s.set("Tau", pp->getTau());
317  s.set("Lambda", pp->getLambda());
318  s.set("Theta", pp->getTheta());
319  s.set("ScalesNumber", pp->getScalesNumber());
320  s.set("WarpingsNumber", pp->getWarpingsNumber());
321  s.set("Epsilon", pp->getEpsilon());
322  s.set("Iterations", pp->getIterations());
323  s.set("UseInitialFlow", pp->getUseInitialFlow());
324  }
325  }
326  {
328  if (!pp.empty()) {
329  s.set("Alpha", pp->getAlpha());
330  s.set("Gamma", pp->getGamma());
331  s.set("ScaleFactor", pp->getScaleFactor());
332  s.set("InnerIterations", pp->getInnerIterations());
333  s.set("OuterIterations", pp->getOuterIterations());
334  s.set("SolverIterations", pp->getSolverIterations());
335  }
336  }
337  {
339  if (!pp.empty()) {
340  s.set("WindowSize", pp->getWindowSize());
341  s.set("MaxLevel", pp->getMaxLevel());
342  s.set("Iterations", pp->getIterations());
343  }
344  }
345  }
346  return s;
347 }
348 }
349 
357 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
358 {
359  // Check the number of arguments
360  nargchk(nrhs>=2 && nlhs<=1);
361 
362  // Argument vector
363  vector<MxArray> rhs(prhs, prhs+nrhs);
364  int id = rhs[0].toInt();
365  string method(rhs[1].toString());
366 
367  // constructor call
368  if (method == "new") {
369  nargchk(nrhs==3 && nlhs<=1);
370  obj_[++last_id] = createSuperResolution(rhs[2].toString());
371  plhs[0] = MxArray(last_id);
372  mexLock();
373  return;
374  }
375 
376  // Big operation switch
377  Ptr<SuperResolution> obj = obj_[id];
378  if (obj.empty())
379  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
380  if (method == "delete") {
381  nargchk(nrhs==2 && nlhs==0);
382  obj_.erase(id);
383  mexUnlock();
384  }
385  else if (method == "clear") {
386  nargchk(nrhs==2 && nlhs==0);
387  obj->clear();
388  }
389  else if (method == "load") {
390  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
391  string objname;
392  bool loadFromString = false;
393  for (int i=3; i<nrhs; i+=2) {
394  string key(rhs[i].toString());
395  if (key == "ObjName")
396  objname = rhs[i+1].toString();
397  else if (key == "FromString")
398  loadFromString = rhs[i+1].toBool();
399  else
400  mexErrMsgIdAndTxt("mexopencv:error",
401  "Unrecognized option %s", key.c_str());
402  }
403  /*
404  obj_[id] = (loadFromString ?
405  Algorithm::loadFromString<SuperResolution>(rhs[2].toString(), objname) :
406  Algorithm::load<SuperResolution>(rhs[2].toString(), objname));
407  */
409  // HACK: workaround for missing SuperResolution::create()
410  FileStorage fs(rhs[2].toString(), FileStorage::READ +
411  (loadFromString ? FileStorage::MEMORY : 0));
412  if (!fs.isOpened())
413  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
414  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
415  if (fn.empty())
416  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
417  obj->read(fn);
418  //*/
419  }
420  else if (method == "save") {
421  nargchk(nrhs==3 && nlhs==0);
422  obj->save(rhs[2].toString());
423  }
424  else if (method == "empty") {
425  nargchk(nrhs==2 && nlhs<=1);
426  plhs[0] = MxArray(obj->empty());
427  }
428  else if (method == "getDefaultName") {
429  nargchk(nrhs==2 && nlhs<=1);
430  plhs[0] = MxArray(obj->getDefaultName());
431  }
432  else if (method == "collectGarbage") {
433  nargchk(nrhs==2 && nlhs==0);
434  obj->collectGarbage();
435  }
436  else if (method == "nextFrame") {
437  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
438  bool flip = false;
439  for (int i=2; i<nrhs; i+=2) {
440  string key(rhs[i].toString());
441  if (key == "FlipChannels")
442  flip = rhs[i+1].toBool();
443  else
444  mexErrMsgIdAndTxt("mexopencv:error",
445  "Unrecognized option %s", key.c_str());
446  }
447  Mat frame;
448  obj->nextFrame(frame);
449  if (flip && (frame.channels() == 3 || frame.channels() == 4)) {
450  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
451  cvtColor(frame, frame, (frame.channels()==3 ?
453  }
454  plhs[0] = MxArray(frame);
455  }
456  else if (method == "reset") {
457  nargchk(nrhs==2 && nlhs==0);
458  obj->reset();
459  }
460  else if (method == "setInput") {
461  nargchk(nrhs>=3 && nlhs==0);
463  rhs[2].toString(), rhs.begin() + 3, rhs.end());
464  obj->setInput(p);
465  }
466  else if (method == "setOpticalFlow") {
467  nargchk(nrhs>=3 && nlhs==0);
469  rhs[2].toString(), rhs.begin() + 3, rhs.end());
470  obj->setOpticalFlow(p);
471  }
472  else if (method == "getOpticalFlow") {
473  nargchk(nrhs==2 && nlhs<=1);
475  plhs[0] = toStruct(p);
476  }
477  else if (method == "get") {
478  nargchk(nrhs==3 && nlhs<=1);
479  string prop(rhs[2].toString());
480  if (prop == "Alpha")
481  plhs[0] = MxArray(obj->getAlpha());
482  else if (prop == "BlurKernelSize")
483  plhs[0] = MxArray(obj->getBlurKernelSize());
484  else if (prop == "BlurSigma")
485  plhs[0] = MxArray(obj->getBlurSigma());
486  else if (prop == "Iterations")
487  plhs[0] = MxArray(obj->getIterations());
488  else if (prop == "KernelSize")
489  plhs[0] = MxArray(obj->getKernelSize());
490  else if (prop == "Labmda")
491  plhs[0] = MxArray(obj->getLabmda());
492  else if (prop == "Scale")
493  plhs[0] = MxArray(obj->getScale());
494  else if (prop == "Tau")
495  plhs[0] = MxArray(obj->getTau());
496  else if (prop == "TemporalAreaRadius")
497  plhs[0] = MxArray(obj->getTemporalAreaRadius());
498  else
499  mexErrMsgIdAndTxt("mexopencv:error",
500  "Unrecognized property %s", prop.c_str());
501  }
502  else if (method == "set") {
503  nargchk(nrhs==4 && nlhs==0);
504  string prop(rhs[2].toString());
505  if (prop == "Alpha")
506  obj->setAlpha(rhs[3].toDouble());
507  else if (prop == "BlurKernelSize")
508  obj->setBlurKernelSize(rhs[3].toInt());
509  else if (prop == "BlurSigma")
510  obj->setBlurSigma(rhs[3].toDouble());
511  else if (prop == "Iterations")
512  obj->setIterations(rhs[3].toInt());
513  else if (prop == "KernelSize")
514  obj->setKernelSize(rhs[3].toInt());
515  else if (prop == "Labmda")
516  obj->setLabmda(rhs[3].toDouble());
517  else if (prop == "Scale")
518  obj->setScale(rhs[3].toInt());
519  else if (prop == "Tau")
520  obj->setTau(rhs[3].toDouble());
521  else if (prop == "TemporalAreaRadius")
522  obj->setTemporalAreaRadius(rhs[3].toInt());
523  else
524  mexErrMsgIdAndTxt("mexopencv:error",
525  "Unrecognized property %s", prop.c_str());
526  }
527  else
528  mexErrMsgIdAndTxt("mexopencv:error",
529  "Unrecognized operation %s", method.c_str());
530 }
virtual double getTheta() const=0
virtual int getScale() const=0
virtual void setPolyN(int val)=0
T empty(T... args)
virtual int getMaxLevel() const=0
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
T distance(T... args)
Ptr< FrameSource > createFrameSource(const string &type, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of FrameSource using options in arguments.
virtual int getFlags() const=0
virtual Ptr< cv::superres::DenseOpticalFlowExt > getOpticalFlow() const=0
Ptr< PyrLKOpticalFlow > createPyrLKOpticalFlow(bool, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of PyrLKOpticalFlow using options in arguments.
virtual void setWarpingsNumber(int val)=0
virtual void setFlags(int val)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
Ptr< SuperResolution > createSuperResolution(const string &type)
Create an instance of SuperResolution using options in arguments.
virtual void setScale(int val)=0
COLOR_BGR2RGB
virtual void setTau(double val)=0
virtual void setScalesNumber(int val)=0
virtual void setWindowSize(int val)=0
virtual void setOuterIterations(int val)=0
virtual void setPyrScale(double val)=0
virtual void setAlpha(double val)=0
virtual void setLabmda(double val)=0
Ptr< FrameSource > createFrameSource_Video(const String &fileName)
STL namespace.
Ptr< DualTVL1OpticalFlow > createOptFlow_DualTVL1_CUDA()
virtual double getScaleFactor() const=0
virtual int getKernelSize() const=0
T end(T... args)
virtual int getWindowSize() const=0
virtual bool isOpened() const
Ptr< superres::DualTVL1OpticalFlow > createDualTVL1OpticalFlow(bool use_gpu, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of DualTVL1OpticalFlow using options in arguments.
virtual int getIterations() const=0
virtual double getTau() const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
Definition: MxArray.hpp:1310
STL class.
virtual int getIterations() const=0
virtual int getIterations() const=0
void nextFrame(OutputArray frame)
Ptr< SuperResolution > createSuperResolution_BTVL1_CUDA()
virtual void setScaleFactor(double val)=0
virtual bool getUseInitialFlow() const=0
virtual int getLevelsNumber() const=0
virtual int getOuterIterations() const=0
virtual void clear()
virtual double getTau() const=0
COLOR_BGRA2RGBA
virtual int getTemporalAreaRadius() const=0
virtual int getWindowSize() const=0
virtual void setPolySigma(double val)=0
virtual void setSolverIterations(int val)=0
virtual void read(const FileNode &fn)
Ptr< FarnebackOpticalFlow > createOptFlow_Farneback()
Ptr< Y > dynamicCast() const
map< int, Ptr< SuperResolution > > obj_
Object container.
virtual void setOpticalFlow(const Ptr< cv::superres::DenseOpticalFlowExt > &val)=0
Ptr< DenseOpticalFlowExt > createDenseOpticalFlowExt(const string &type, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of DenseOpticalFlowExt using options in arguments.
virtual void setTau(double val)=0
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...
virtual double getLabmda() const=0
virtual int getSolverIterations() const=0
virtual void setLevelsNumber(int val)=0
virtual void setIterations(int val)=0
virtual int getBlurKernelSize() const=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual double getLambda() const=0
Ptr< BroxOpticalFlow > createOptFlow_Brox_CUDA()
virtual double getPyrScale() const=0
virtual void setAlpha(double val)=0
virtual void setBlurKernelSize(int val)=0
virtual double getAlpha() const=0
virtual int getScalesNumber() const=0
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
void setInput(const Ptr< FrameSource > &frameSource)
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
Definition: MxArray.hpp:312
FileNode getFirstTopLevelNode() const
Ptr< BroxOpticalFlow > createBroxOpticalFlow(bool, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of BroxOpticalFlow using options in arguments.
virtual int getIterations() const=0
virtual double getGamma() const=0
virtual void setGamma(double val)=0
STL class.
bool empty() const
MxArray toStruct(Ptr< DenseOpticalFlowExt > p)
Convert a DenseOpticalFlowExt to MxArray.
virtual String getDefaultName() const
virtual void setInnerIterations(int val)=0
Ptr< FarnebackOpticalFlow > createOptFlow_Farneback_CUDA()
Global constant definitions.
T begin(T... args)
virtual void setIterations(int val)=0
virtual int getPolyN() const=0
virtual double getBlurSigma() const=0
virtual double getAlpha() const=0
virtual void setTheta(double val)=0
virtual double getEpsilon() const=0
T c_str(T... args)
virtual void setKernelSize(int val)=0
Ptr< SuperResolution > createSuperResolution_BTVL1()
virtual int getInnerIterations() const=0
virtual void setWindowSize(int val)=0
virtual void setUseInitialFlow(bool val)=0
virtual double getPolySigma() const=0
virtual void setBlurSigma(double val)=0
virtual void setTemporalAreaRadius(int val)=0
Ptr< PyrLKOpticalFlow > createOptFlow_PyrLK_CUDA()
virtual int getWarpingsNumber() const=0
void flip(InputArray src, OutputArray dst, int flipCode)
virtual void save(const String &filename) const
virtual bool empty() const
virtual void setLambda(double val)=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void setMaxLevel(int val)=0
virtual void setIterations(int val)=0
virtual void setEpsilon(double val)=0
Ptr< FrameSource > createFrameSource_Camera(int deviceId=0)
int type() const
Ptr< DualTVL1OpticalFlow > createOptFlow_DualTVL1()
Ptr< superres::FarnebackOpticalFlow > createFarnebackOpticalFlow(bool use_gpu, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of FarnebackOpticalFlow using options in arguments.
int last_id
Last object id to allocate.
int channels() const
virtual void setIterations(int val)=0