mexopencv  3.4.1
MEX interface for OpenCV library
FarnebackOpticalFlow_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/video.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=2 && nlhs<=1);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35  int id = rhs[0].toInt();
36  string method(rhs[1].toString());
37 
38  // constructor call
39  if (method == "new") {
40  nargchk(nrhs==2 && nlhs<=1);
42  plhs[0] = MxArray(last_id);
43  mexLock();
44  return;
45  }
46 
47  // Big operation switch
49  if (obj.empty())
50  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
51  if (method == "delete") {
52  nargchk(nrhs==2 && nlhs==0);
53  obj_.erase(id);
54  mexUnlock();
55  }
56  else if (method == "clear") {
57  nargchk(nrhs==2 && nlhs==0);
58  obj->clear();
59  }
60  else if (method == "save") {
61  nargchk(nrhs==3 && nlhs==0);
62  obj->save(rhs[2].toString());
63  }
64  else if (method == "load") {
65  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
66  string objname;
67  bool loadFromString = false;
68  for (int i=3; i<nrhs; i+=2) {
69  string key(rhs[i].toString());
70  if (key == "ObjName")
71  objname = rhs[i+1].toString();
72  else if (key == "FromString")
73  loadFromString = rhs[i+1].toBool();
74  else
75  mexErrMsgIdAndTxt("mexopencv:error",
76  "Unrecognized option %s", key.c_str());
77  }
78  obj_[id] = (loadFromString ?
79  Algorithm::loadFromString<FarnebackOpticalFlow>(rhs[2].toString(), objname) :
80  Algorithm::load<FarnebackOpticalFlow>(rhs[2].toString(), objname));
81  }
82  else if (method == "empty") {
83  nargchk(nrhs==2 && nlhs<=1);
84  plhs[0] = MxArray(obj->empty());
85  }
86  else if (method == "getDefaultName") {
87  nargchk(nrhs==2 && nlhs<=1);
88  plhs[0] = MxArray(obj->getDefaultName());
89  }
90  else if (method == "calc") {
91  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
92  Mat flow;
93  for (int i=4; i<nrhs; i+=2) {
94  string key(rhs[i].toString());
95  if (key == "InitialFlow")
96  flow = rhs[i+1].toMat(CV_32F);
97  else
98  mexErrMsgIdAndTxt("mexopencv:error",
99  "Unrecognized option %s", key.c_str());
100  }
101  Mat I0(rhs[2].toMat(CV_8U)),
102  I1(rhs[3].toMat(CV_8U));
103  obj->calc(I0, I1, flow);
104  plhs[0] = MxArray(flow);
105  }
106  else if (method == "collectGarbage") {
107  nargchk(nrhs==2 && nlhs==0);
108  obj->collectGarbage();
109  }
110  else if (method == "get") {
111  nargchk(nrhs==3 && nlhs<=1);
112  string prop(rhs[2].toString());
113  if (prop == "NumLevels")
114  plhs[0] = MxArray(obj->getNumLevels());
115  else if (prop == "PyrScale")
116  plhs[0] = MxArray(obj->getPyrScale());
117  else if (prop == "FastPyramids")
118  plhs[0] = MxArray(obj->getFastPyramids());
119  else if (prop == "WinSize")
120  plhs[0] = MxArray(obj->getWinSize());
121  else if (prop == "NumIters")
122  plhs[0] = MxArray(obj->getNumIters());
123  else if (prop == "PolyN")
124  plhs[0] = MxArray(obj->getPolyN());
125  else if (prop == "PolySigma")
126  plhs[0] = MxArray(obj->getPolySigma());
127  else if (prop == "Flags")
128  plhs[0] = MxArray(obj->getFlags());
129  else
130  mexErrMsgIdAndTxt("mexopencv:error",
131  "Unrecognized property %s", prop.c_str());
132  }
133  else if (method == "set") {
134  nargchk(nrhs==4 && nlhs==0);
135  string prop(rhs[2].toString());
136  if (prop == "NumLevels")
137  obj->setNumLevels(rhs[3].toInt());
138  else if (prop == "PyrScale")
139  obj->setPyrScale(rhs[3].toDouble());
140  else if (prop == "FastPyramids")
141  obj->setFastPyramids(rhs[3].toBool());
142  else if (prop == "WinSize")
143  obj->setWinSize(rhs[3].toInt());
144  else if (prop == "NumIters")
145  obj->setNumIters(rhs[3].toInt());
146  else if (prop == "PolyN")
147  obj->setPolyN(rhs[3].toInt());
148  else if (prop == "PolySigma")
149  obj->setPolySigma(rhs[3].toDouble());
150  else if (prop == "Flags") //TODO: expose props for flag values
151  obj->setFlags(rhs[3].toInt());
152  else
153  mexErrMsgIdAndTxt("mexopencv:error",
154  "Unrecognized property %s", prop.c_str());
155  }
156  else
157  mexErrMsgIdAndTxt("mexopencv:error",
158  "Unrecognized operation %s", method.c_str());
159 }
virtual void setFastPyramids(bool fastPyramids)=0
virtual int getPolyN() const=0
virtual double getPyrScale() const=0
virtual void collectGarbage()=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual int getNumLevels() const=0
virtual void setWinSize(int winSize)=0
#define CV_8U
STL namespace.
virtual void setNumLevels(int numLevels)=0
map< int, Ptr< FarnebackOpticalFlow > > obj_
Object container.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual double getPolySigma() const=0
virtual void setPolyN(int polyN)=0
virtual void setNumIters(int numIters)=0
virtual void clear()
#define CV_32F
virtual void setPyrScale(double pyrScale)=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...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
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
virtual int getWinSize() const=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void setFlags(int flags)=0
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual int getFlags() const=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow)=0
virtual void setPolySigma(double polySigma)=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const
virtual int getNumIters() const=0
virtual bool getFastPyramids() const=0