mexopencv  0.1
mex interface for opencv library
BackgroundSubtractorGMG_.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;
19 map<int,Ptr<BackgroundSubtractorGMG> > obj_;
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  int initializationFrames = 120;
43  double decisionThreshold = 0.8;
44  for (int i=2; i<nrhs; i+=2) {
45  string key(rhs[i].toString());
46  if (key=="InitializationFrames")
47  initializationFrames = rhs[i+1].toInt();
48  else if (key=="DecisionThreshold")
49  decisionThreshold = rhs[i+1].toDouble();
50  else
51  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
52  }
53  obj_[++last_id] = createBackgroundSubtractorGMG(
54  initializationFrames, decisionThreshold);
55  plhs[0] = MxArray(last_id);
56  return;
57  }
58 
59  // Big operation switch
60  Ptr<BackgroundSubtractorGMG> obj = obj_[id];
61  if (method == "delete") {
62  nargchk(nrhs==2 && nlhs==0);
63  obj_.erase(id);
64  }
65  else if (method == "clear") {
66  nargchk(nrhs==2 && nlhs==0);
67  obj->clear();
68  }
69  else if (method == "save") {
70  nargchk(nrhs==3 && nlhs==0);
71  obj->save(rhs[2].toString());
72  }
73  else if (method == "load") {
74  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
75  string objname;
76  bool loadFromString = false;
77  for (int i=3; i<nrhs; i+=2) {
78  string key(rhs[i].toString());
79  if (key=="ObjName")
80  objname = rhs[i+1].toString();
81  else if (key=="FromString")
82  loadFromString = rhs[i+1].toBool();
83  else
84  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
85  }
86  /*
87  obj_[id] = (loadFromString ?
88  Algorithm::loadFromString<BackgroundSubtractorGMG>(rhs[2].toString(), objname) :
89  Algorithm::load<BackgroundSubtractorGMG>(rhs[2].toString(), objname));
90  */
92  // HACK: workaround for missing BackgroundSubtractorGMG::create()
93  FileStorage fs(rhs[2].toString(), FileStorage::READ +
94  (loadFromString ? FileStorage::MEMORY : 0));
95  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
96  if (obj.empty())
97  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
98  //*/
99  }
100  else if (method == "empty") {
101  nargchk(nrhs==2 && nlhs<=1);
102  plhs[0] = MxArray(obj->empty());
103  }
104  else if (method == "getDefaultName") {
105  nargchk(nrhs==2 && nlhs<=1);
106  plhs[0] = MxArray(obj->getDefaultName());
107  }
108  else if (method == "apply") {
109  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
110  double learningRate = -1;
111  for (int i=3; i<nrhs; i+=2) {
112  string key(rhs[i].toString());
113  if (key=="LearningRate")
114  learningRate = rhs[i+1].toDouble();
115  else
116  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
117  }
118  Mat image(rhs[2].toMat()), fgmask;
119  obj->apply(image, fgmask, learningRate);
120  plhs[0] = MxArray(fgmask, mxLOGICAL_CLASS);
121  }
122  else if (method == "getBackgroundImage") {
123  nargchk(nrhs==2 && nlhs<=1);
124  Mat backgroundImage;
125  obj->getBackgroundImage(backgroundImage);
126  plhs[0] = MxArray(backgroundImage);
127  }
128  else if (method == "get") {
129  nargchk(nrhs==3 && nlhs<=1);
130  string prop(rhs[2].toString());
131  if (prop == "BackgroundPrior")
132  plhs[0] = MxArray(obj->getBackgroundPrior());
133  else if (prop == "DecisionThreshold")
134  plhs[0] = MxArray(obj->getDecisionThreshold());
135  else if (prop == "DefaultLearningRate")
136  plhs[0] = MxArray(obj->getDefaultLearningRate());
137  else if (prop == "MaxFeatures")
138  plhs[0] = MxArray(obj->getMaxFeatures());
139  else if (prop == "MaxVal")
140  plhs[0] = MxArray(obj->getMaxVal());
141  else if (prop == "MinVal")
142  plhs[0] = MxArray(obj->getMinVal());
143  else if (prop == "NumFrames")
144  plhs[0] = MxArray(obj->getNumFrames());
145  else if (prop == "QuantizationLevels")
146  plhs[0] = MxArray(obj->getQuantizationLevels());
147  else if (prop == "SmoothingRadius")
148  plhs[0] = MxArray(obj->getSmoothingRadius());
149  else if (prop == "UpdateBackgroundModel")
150  plhs[0] = MxArray(obj->getUpdateBackgroundModel());
151  else
152  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized property");
153  }
154  else if (method == "set") {
155  nargchk(nrhs==4 && nlhs==0);
156  string prop(rhs[2].toString());
157  if (prop == "BackgroundPrior")
158  obj->setBackgroundPrior(rhs[3].toDouble());
159  else if (prop == "DecisionThreshold")
160  obj->setDecisionThreshold(rhs[3].toDouble());
161  else if (prop == "DefaultLearningRate")
162  obj->setDefaultLearningRate(rhs[3].toDouble());
163  else if (prop == "MaxFeatures")
164  obj->setMaxFeatures(rhs[3].toInt());
165  else if (prop == "MaxVal")
166  obj->setMaxVal(rhs[3].toDouble());
167  else if (prop == "MinVal")
168  obj->setMinVal(rhs[3].toDouble());
169  else if (prop == "NumFrames")
170  obj->setNumFrames(rhs[3].toInt());
171  else if (prop == "QuantizationLevels")
172  obj->setQuantizationLevels(rhs[3].toInt());
173  else if (prop == "SmoothingRadius")
174  obj->setSmoothingRadius(rhs[3].toInt());
175  else if (prop == "UpdateBackgroundModel")
176  obj->setUpdateBackgroundModel(rhs[3].toBool());
177  else
178  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized property");
179  }
180  else
181  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
182 }
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.