mexopencv  3.4.1
MEX interface for OpenCV library
SyntheticSequenceGenerator_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/bgsegm.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::bgsegm;
13 
14 // Persistent objects
15 namespace {
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<=2);
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>=4 && (nrhs%2)==0 && nlhs<=1);
42  double amplitude = 2.0;
43  double wavelength = 20.0;
44  double wavespeed = 0.2;
45  double objspeed = 6.0;
46  for (int i=4; i<nrhs; i+=2) {
47  string key(rhs[i].toString());
48  if (key == "Amplitude")
49  amplitude = rhs[i+1].toDouble();
50  else if (key == "WaveLength")
51  wavelength = rhs[i+1].toDouble();
52  else if (key == "WaveSpeed")
53  wavespeed = rhs[i+1].toDouble();
54  else if (key == "ObjSpeed")
55  objspeed = rhs[i+1].toDouble();
56  else
57  mexErrMsgIdAndTxt("mexopencv:error",
58  "Unrecognized option %s", key.c_str());
59  }
60  Mat background(rhs[2].toMat(CV_8U)),
61  object(rhs[3].toMat(CV_8U));
62  obj_[++last_id] = createSyntheticSequenceGenerator(background, object,
63  amplitude, wavelength, wavespeed, objspeed);
64  plhs[0] = MxArray(last_id);
65  mexLock();
66  return;
67  }
68 
69  // Big operation switch
71  if (obj.empty())
72  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
73  if (method == "delete") {
74  nargchk(nrhs==2 && nlhs==0);
75  obj_.erase(id);
76  mexUnlock();
77  }
78  else if (method == "clear") {
79  nargchk(nrhs==2 && nlhs==0);
80  obj->clear();
81  }
82  else if (method == "load") {
83  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
84  string objname;
85  bool loadFromString = false;
86  for (int i=3; i<nrhs; i+=2) {
87  string key(rhs[i].toString());
88  if (key == "ObjName")
89  objname = rhs[i+1].toString();
90  else if (key == "FromString")
91  loadFromString = rhs[i+1].toBool();
92  else
93  mexErrMsgIdAndTxt("mexopencv:error",
94  "Unrecognized option %s", key.c_str());
95  }
96  /*
97  obj_[id] = (loadFromString ?
98  Algorithm::loadFromString<SyntheticSequenceGenerator>(rhs[2].toString(), objname) :
99  Algorithm::load<SyntheticSequenceGenerator>(rhs[2].toString(), objname));
100  */
102  // HACK: workaround for missing SyntheticSequenceGenerator::create()
103  FileStorage fs(rhs[2].toString(), FileStorage::READ +
104  (loadFromString ? FileStorage::MEMORY : 0));
105  if (!fs.isOpened())
106  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
107  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
108  if (fn.empty())
109  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
110  obj->read(fn);
111  //*/
112  }
113  else if (method == "save") {
114  nargchk(nrhs==3 && nlhs==0);
115  obj->save(rhs[2].toString());
116  }
117  else if (method == "empty") {
118  nargchk(nrhs==2 && nlhs<=1);
119  plhs[0] = MxArray(obj->empty());
120  }
121  else if (method == "getDefaultName") {
122  nargchk(nrhs==2 && nlhs<=1);
123  plhs[0] = MxArray(obj->getDefaultName());
124  }
125  else if (method == "getNextFrame") {
126  nargchk(nrhs==2 && nlhs<=2);
127  Mat frame, gtMask;
128  obj->getNextFrame(frame, gtMask);
129  plhs[0] = MxArray(frame);
130  if (nlhs > 1)
131  plhs[1] = MxArray(gtMask);
132  }
133  else
134  mexErrMsgIdAndTxt("mexopencv:error",
135  "Unrecognized operation %s", method.c_str());
136 }
T empty(T... args)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
Ptr< SyntheticSequenceGenerator > createSyntheticSequenceGenerator(InputArray background, InputArray object, double amplitude=2.0, double wavelength=20.0, double wavespeed=0.2, double objspeed=6.0)
void getNextFrame(OutputArray frame, OutputArray gtMask)
virtual void clear()
virtual void read(const FileNode &fn)
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
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
map< int, Ptr< SyntheticSequenceGenerator > > obj_
Object container.
cv::Mat toMat() const