mexopencv  0.1
mex interface for opencv library
BackgroundSubtractorMOG_.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<BackgroundSubtractorMOG> > 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 history = 200;
43  int nmixtures = 5;
44  double backgroundRatio = 0.7;
45  double noiseSigma = 0;
46  for (int i=2; i<nrhs; i+=2) {
47  string key(rhs[i].toString());
48  if (key=="History")
49  history = rhs[i+1].toInt();
50  else if (key=="NMixtures")
51  nmixtures = rhs[i+1].toInt();
52  else if (key=="BackgroundRatio")
53  backgroundRatio = rhs[i+1].toDouble();
54  else if (key=="NoiseSigma")
55  noiseSigma = rhs[i+1].toDouble();
56  else
57  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
58  }
59  obj_[++last_id] = createBackgroundSubtractorMOG(
60  history, nmixtures, backgroundRatio, noiseSigma);
61  plhs[0] = MxArray(last_id);
62  return;
63  }
64 
65  // Big operation switch
66  Ptr<BackgroundSubtractorMOG> obj = obj_[id];
67  if (method == "delete") {
68  nargchk(nrhs==2 && nlhs==0);
69  obj_.erase(id);
70  }
71  else if (method == "clear") {
72  nargchk(nrhs==2 && nlhs==0);
73  obj->clear();
74  }
75  else if (method == "save") {
76  nargchk(nrhs==3 && nlhs==0);
77  obj->save(rhs[2].toString());
78  }
79  else if (method == "load") {
80  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
81  string objname;
82  bool loadFromString = false;
83  for (int i=3; i<nrhs; i+=2) {
84  string key(rhs[i].toString());
85  if (key=="ObjName")
86  objname = rhs[i+1].toString();
87  else if (key=="FromString")
88  loadFromString = rhs[i+1].toBool();
89  else
90  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
91  }
92  /*
93  obj_[id] = (loadFromString ?
94  Algorithm::loadFromString<BackgroundSubtractorMOG>(rhs[2].toString(), objname) :
95  Algorithm::load<BackgroundSubtractorMOG>(rhs[2].toString(), objname));
96  */
98  // HACK: workaround for missing BackgroundSubtractorMOG::create()
99  FileStorage fs(rhs[2].toString(), FileStorage::READ +
100  (loadFromString ? FileStorage::MEMORY : 0));
101  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
102  if (obj.empty())
103  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
104  //*/
105  }
106  else if (method == "empty") {
107  nargchk(nrhs==2 && nlhs<=1);
108  plhs[0] = MxArray(obj->empty());
109  }
110  else if (method == "getDefaultName") {
111  nargchk(nrhs==2 && nlhs<=1);
112  plhs[0] = MxArray(obj->getDefaultName());
113  }
114  else if (method == "apply") {
115  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
116  double learningRate = -1;
117  for (int i=3; i<nrhs; i+=2) {
118  string key(rhs[i].toString());
119  if (key=="LearningRate")
120  learningRate = rhs[i+1].toDouble();
121  else
122  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
123  }
124  Mat image(rhs[2].toMat()), fgmask;
125  obj->apply(image, fgmask, learningRate);
126  plhs[0] = MxArray(fgmask, mxLOGICAL_CLASS);
127  }
128  else if (method == "getBackgroundImage") {
129  nargchk(nrhs==2 && nlhs<=1);
130  Mat backgroundImage;
131  obj->getBackgroundImage(backgroundImage);
132  plhs[0] = MxArray(backgroundImage);
133  }
134  else if (method == "get") {
135  nargchk(nrhs==3 && nlhs<=1);
136  string prop(rhs[2].toString());
137  if (prop == "BackgroundRatio")
138  plhs[0] = MxArray(obj->getBackgroundRatio());
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 == "NoiseSigma")
144  plhs[0] = MxArray(obj->getNoiseSigma());
145  else
146  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized property");
147  }
148  else if (method == "set") {
149  nargchk(nrhs==4 && nlhs==0);
150  string prop(rhs[2].toString());
151  if (prop == "BackgroundRatio")
152  obj->setBackgroundRatio(rhs[3].toDouble());
153  else if (prop == "History")
154  obj->setHistory(rhs[3].toInt());
155  else if (prop == "NMixtures")
156  obj->setNMixtures(rhs[3].toInt());
157  else if (prop == "NoiseSigma")
158  obj->setNoiseSigma(rhs[3].toDouble());
159  else
160  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized property");
161  }
162  else
163  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
164 }
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.