mexopencv  0.1
mex interface for opencv library
BackgroundSubtractorMOG2_.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;
18 map<int,Ptr<BackgroundSubtractorMOG2> > obj_;
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 && (nrhs%2)==0 && nlhs<=1);
41  int history = 500;
42  double varThreshold = 16;
43  bool detectShadows = true;
44  for (int i=2; i<nrhs; i+=2) {
45  string key(rhs[i].toString());
46  if (key=="History")
47  history = rhs[i+1].toInt();
48  else if (key=="VarThreshold")
49  varThreshold = rhs[i+1].toDouble();
50  else if (key=="DetectShadows")
51  detectShadows = rhs[i+1].toBool();
52  else
53  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
54  }
55  obj_[++last_id] = createBackgroundSubtractorMOG2(
56  history, varThreshold, detectShadows);
57  plhs[0] = MxArray(last_id);
58  return;
59  }
60 
61  // Big operation switch
62  Ptr<BackgroundSubtractorMOG2> obj = obj_[id];
63  if (method == "delete") {
64  nargchk(nrhs==2 && nlhs==0);
65  obj_.erase(id);
66  }
67  else if (method == "clear") {
68  nargchk(nrhs==2 && nlhs==0);
69  obj->clear();
70  }
71  else if (method == "save") {
72  nargchk(nrhs==3 && nlhs==0);
73  obj->save(rhs[2].toString());
74  }
75  else if (method == "load") {
76  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
77  string objname;
78  bool loadFromString = false;
79  for (int i=3; i<nrhs; i+=2) {
80  string key(rhs[i].toString());
81  if (key=="ObjName")
82  objname = rhs[i+1].toString();
83  else if (key=="FromString")
84  loadFromString = rhs[i+1].toBool();
85  else
86  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
87  }
88  /*
89  obj_[id] = (loadFromString ?
90  Algorithm::loadFromString<BackgroundSubtractorMOG2>(rhs[2].toString(), objname) :
91  Algorithm::load<BackgroundSubtractorMOG2>(rhs[2].toString(), objname));
92  */
94  // HACK: workaround for missing BackgroundSubtractorMOG2::create()
95  FileStorage fs(rhs[2].toString(), FileStorage::READ +
96  (loadFromString ? FileStorage::MEMORY : 0));
97  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
98  if (obj.empty())
99  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
100  //*/
101  }
102  else if (method == "empty") {
103  nargchk(nrhs==2 && nlhs<=1);
104  plhs[0] = MxArray(obj->empty());
105  }
106  else if (method == "getDefaultName") {
107  nargchk(nrhs==2 && nlhs<=1);
108  plhs[0] = MxArray(obj->getDefaultName());
109  }
110  else if (method == "apply") {
111  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
112  double learningRate = -1;
113  for (int i=3; i<nrhs; i+=2) {
114  string key(rhs[i].toString());
115  if (key=="LearningRate")
116  learningRate = rhs[i+1].toDouble();
117  else
118  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
119  }
120  Mat image(rhs[2].toMat()), fgmask;
121  obj->apply(image, fgmask, learningRate);
122  plhs[0] = MxArray(fgmask, mxLOGICAL_CLASS);
123  }
124  else if (method == "getBackgroundImage") {
125  nargchk(nrhs==2 && nlhs<=1);
126  Mat backgroundImage;
127  obj->getBackgroundImage(backgroundImage);
128  plhs[0] = MxArray(backgroundImage);
129  }
130  else if (method == "get") {
131  nargchk(nrhs==3 && nlhs<=1);
132  string prop(rhs[2].toString());
133  if (prop == "BackgroundRatio")
134  plhs[0] = MxArray(obj->getBackgroundRatio());
135  else if (prop == "ComplexityReductionThreshold")
136  plhs[0] = MxArray(obj->getComplexityReductionThreshold());
137  else if (prop == "DetectShadows")
138  plhs[0] = MxArray(obj->getDetectShadows());
139  else if (prop == "History")
140  plhs[0] = MxArray(obj->getHistory());
141  else if (prop == "NMixtures")
142  plhs[0] = MxArray(obj->getNMixtures());
143  else if (prop == "ShadowThreshold")
144  plhs[0] = MxArray(obj->getShadowThreshold());
145  else if (prop == "ShadowValue")
146  plhs[0] = MxArray(obj->getShadowValue());
147  else if (prop == "VarInit")
148  plhs[0] = MxArray(obj->getVarInit());
149  else if (prop == "VarMax")
150  plhs[0] = MxArray(obj->getVarMax());
151  else if (prop == "VarMin")
152  plhs[0] = MxArray(obj->getVarMin());
153  else if (prop == "VarThreshold")
154  plhs[0] = MxArray(obj->getVarThreshold());
155  else if (prop == "VarThresholdGen")
156  plhs[0] = MxArray(obj->getVarThresholdGen());
157  else
158  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized property");
159  }
160  else if (method == "set") {
161  nargchk(nrhs==4 && nlhs==0);
162  string prop(rhs[2].toString());
163  if (prop == "BackgroundRatio")
164  obj->setBackgroundRatio(rhs[3].toDouble());
165  else if (prop == "ComplexityReductionThreshold")
166  obj->setComplexityReductionThreshold(rhs[3].toDouble());
167  else if (prop == "DetectShadows")
168  obj->setDetectShadows(rhs[3].toBool());
169  else if (prop == "History")
170  obj->setHistory(rhs[3].toInt());
171  else if (prop == "NMixtures")
172  obj->setNMixtures(rhs[3].toInt());
173  else if (prop == "ShadowThreshold")
174  obj->setShadowThreshold(rhs[3].toDouble());
175  else if (prop == "ShadowValue")
176  obj->setShadowValue(rhs[3].toInt());
177  else if (prop == "VarInit")
178  obj->setVarInit(rhs[3].toDouble());
179  else if (prop == "VarMax")
180  obj->setVarMax(rhs[3].toDouble());
181  else if (prop == "VarMin")
182  obj->setVarMin(rhs[3].toDouble());
183  else if (prop == "VarThreshold")
184  obj->setVarThreshold(rhs[3].toDouble());
185  else if (prop == "VarThresholdGen")
186  obj->setVarThresholdGen(rhs[3].toDouble());
187  else
188  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized property");
189  }
190  else
191  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
192 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.