mexopencv  3.4.1
MEX interface for OpenCV library
SparsePyrLKOpticalFlow_.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<=3);
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<SparsePyrLKOpticalFlow>(rhs[2].toString(), objname) :
80  Algorithm::load<SparsePyrLKOpticalFlow>(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>=5 && (nrhs%2)==1 && nlhs<=3);
92  vector<Point2f> nextPts;
93  for (int i=5; i<nrhs; i+=2) {
94  string key(rhs[i].toString());
95  if (key == "InitialFlow")
96  nextPts = rhs[i+1].toVector<Point2f>();
97  else
98  mexErrMsgIdAndTxt("mexopencv:error",
99  "Unrecognized option %s", key.c_str());
100  }
101  Mat prevImg(rhs[2].toMat(CV_8U)),
102  nextImg(rhs[3].toMat(CV_8U));
103  vector<Point2f> prevPts(rhs[4].toVector<Point2f>());
104  Mat status, err;
105  obj->calc(prevImg, nextImg, prevPts, nextPts, status,
106  (nlhs>2 ? err : noArray()));
107  plhs[0] = MxArray(nextPts);
108  if (nlhs>1)
109  plhs[1] = MxArray(status);
110  if (nlhs>2)
111  plhs[2] = MxArray(err);
112  }
113  else if (method == "get") {
114  nargchk(nrhs==3 && nlhs<=1);
115  string prop(rhs[2].toString());
116  if (prop == "WinSize")
117  plhs[0] = MxArray(obj->getWinSize());
118  else if (prop == "MaxLevel")
119  plhs[0] = MxArray(obj->getMaxLevel());
120  else if (prop == "TermCriteria")
121  plhs[0] = MxArray(obj->getTermCriteria());
122  else if (prop == "Flags")
123  plhs[0] = MxArray(obj->getFlags());
124  else if (prop == "MinEigThreshold")
125  plhs[0] = MxArray(obj->getMinEigThreshold());
126  else
127  mexErrMsgIdAndTxt("mexopencv:error",
128  "Unrecognized property %s", prop.c_str());
129  }
130  else if (method == "set") {
131  nargchk(nrhs==4 && nlhs==0);
132  string prop(rhs[2].toString());
133  if (prop == "WinSize")
134  obj->setWinSize(rhs[3].toSize());
135  else if (prop == "MaxLevel")
136  obj->setMaxLevel(rhs[3].toInt());
137  else if (prop == "TermCriteria") {
138  //HACK: method defined as: setTermCriteria(TermCriteria& crit)
139  // rvalue should be passed either by-value or by-const-ref
140  // (GCC complains when rvalue is passed by-non-const-ref)
141  TermCriteria tc(rhs[3].toTermCriteria());
142  obj->setTermCriteria(tc);
143  }
144  else if (prop == "Flags") //TODO: expose props for flag values
145  obj->setFlags(rhs[3].toInt());
146  else if (prop == "MinEigThreshold")
147  obj->setMinEigThreshold(rhs[3].toDouble());
148  else
149  mexErrMsgIdAndTxt("mexopencv:error",
150  "Unrecognized property %s", prop.c_str());
151  }
152  else
153  mexErrMsgIdAndTxt("mexopencv:error",
154  "Unrecognized operation %s", method.c_str());
155 }
virtual Size getWinSize() const=0
virtual int getMaxLevel() const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
virtual void calc(InputArray prevImg, InputArray nextImg, InputArray prevPts, InputOutputArray nextPts, OutputArray status, OutputArray err=cv::noArray())=0
STL namespace.
virtual void setTermCriteria(TermCriteria &crit)=0
virtual double getMinEigThreshold() const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setMinEigThreshold(double minEigThreshold)=0
virtual void clear()
InputOutputArray noArray()
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.
virtual int getFlags() 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 mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
map< int, Ptr< SparsePyrLKOpticalFlow > > obj_
Object container.
virtual TermCriteria getTermCriteria() const=0
STL class.
bool empty() const
virtual void setWinSize(Size winSize)=0
virtual void setFlags(int flags)=0
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
virtual void setMaxLevel(int maxLevel)=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const