mexopencv  3.4.1
MEX interface for OpenCV library
OpticalFlowPCAFlow_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/optflow.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::optflow;
13 
14 namespace {
15 // Persistent objects
17 int last_id = 0;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=1);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // constructor call
40  if (method == "new") {
41  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
42  string pathToPrior;
43  Size basisSize(18,14);
44  float sparseRate = 0.024f;
45  float retainedCornersFraction = 0.2f;
46  float occlusionsThreshold = 0.0003f;
47  float dampingFactor = 0.00002f;
48  float claheClip = 14;
49  for (int i=2; i<nrhs; i+=2) {
50  string key(rhs[i].toString());
51  if (key == "Prior")
52  pathToPrior = rhs[i+1].toString();
53  else if (key == "BasisSize")
54  basisSize = rhs[i+1].toSize();
55  else if (key == "SparseRate")
56  sparseRate = rhs[i+1].toFloat();
57  else if (key == "RetainedCornersFraction")
58  retainedCornersFraction = rhs[i+1].toFloat();
59  else if (key == "OcclusionsThreshold")
60  occlusionsThreshold = rhs[i+1].toFloat();
61  else if (key == "DampingFactor")
62  dampingFactor = rhs[i+1].toFloat();
63  else if (key == "ClaheClip")
64  claheClip = rhs[i+1].toFloat();
65  else
66  mexErrMsgIdAndTxt("mexopencv:error",
67  "Unrecognized option %s", key.c_str());
68  }
69  Ptr<PCAPrior> prior;
70  if (!pathToPrior.empty())
71  prior = makePtr<PCAPrior>(pathToPrior.c_str());
72  obj_[++last_id] = makePtr<OpticalFlowPCAFlow>(prior, basisSize,
73  sparseRate, retainedCornersFraction, occlusionsThreshold,
74  dampingFactor, claheClip);
75  //obj_[++last_id] = createOptFlow_PCAFlow();
76  plhs[0] = MxArray(last_id);
77  mexLock();
78  return;
79  }
80 
81  // Big operation switch
82  Ptr<OpticalFlowPCAFlow> obj = obj_[id];
83  if (obj.empty())
84  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
85  if (method == "delete") {
86  nargchk(nrhs==2 && nlhs==0);
87  obj_.erase(id);
88  mexUnlock();
89  }
90  else if (method == "clear") {
91  nargchk(nrhs==2 && nlhs==0);
92  obj->clear();
93  }
94  else if (method == "save") {
95  nargchk(nrhs==3 && nlhs==0);
96  obj->save(rhs[2].toString());
97  }
98  else if (method == "load") {
99  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
100  string objname;
101  bool loadFromString = false;
102  for (int i=3; i<nrhs; i+=2) {
103  string key(rhs[i].toString());
104  if (key == "ObjName")
105  objname = rhs[i+1].toString();
106  else if (key == "FromString")
107  loadFromString = rhs[i+1].toBool();
108  else
109  mexErrMsgIdAndTxt("mexopencv:error",
110  "Unrecognized option %s", key.c_str());
111  }
112  /*
113  obj_[id] = (loadFromString ?
114  Algorithm::loadFromString<OpticalFlowPCAFlow>(rhs[2].toString(), objname) :
115  Algorithm::load<OpticalFlowPCAFlow>(rhs[2].toString(), objname));
116  */
118  // HACK: workaround for missing OpticalFlowPCAFlow::create()
119  FileStorage fs(rhs[2].toString(), FileStorage::READ +
120  (loadFromString ? FileStorage::MEMORY : 0));
121  if (!fs.isOpened())
122  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
123  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
124  if (fn.empty())
125  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
126  obj->read(fn);
127  //*/
128  }
129  else if (method == "empty") {
130  nargchk(nrhs==2 && nlhs<=1);
131  plhs[0] = MxArray(obj->empty());
132  }
133  else if (method == "getDefaultName") {
134  nargchk(nrhs==2 && nlhs<=1);
135  plhs[0] = MxArray(obj->getDefaultName());
136  }
137  else if (method == "calc") {
138  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
139  Mat flow;
140  for (int i=4; i<nrhs; i+=2) {
141  string key(rhs[i].toString());
142  if (key == "InitialFlow")
143  flow = rhs[i+1].toMat(CV_32F);
144  else
145  mexErrMsgIdAndTxt("mexopencv:error",
146  "Unrecognized option %s", key.c_str());
147  }
148  Mat I0(rhs[2].toMat(CV_8U)),
149  I1(rhs[3].toMat(CV_8U));
150  obj->calc(I0, I1, flow);
151  plhs[0] = MxArray(flow);
152  }
153  else if (method == "collectGarbage") {
154  nargchk(nrhs==2 && nlhs==0);
155  obj->collectGarbage();
156  }
157  else
158  mexErrMsgIdAndTxt("mexopencv:error",
159  "Unrecognized operation %s", method.c_str());
160 }
T empty(T... args)
map< int, Ptr< OpticalFlowPCAFlow > > obj_
Object container.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
STL namespace.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
void calc(InputArray I0, InputArray I1, InputOutputArray flow)
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
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
cv::Mat toMat() const