mexopencv  0.1
mex interface for opencv library
BackgroundSubtractorKNN_.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<BackgroundSubtractorKNN> > 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 dist2Threshold = 400.0;
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=="Dist2Threshold")
49  dist2Threshold = 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] = createBackgroundSubtractorKNN(
56  history, dist2Threshold, detectShadows);
57  plhs[0] = MxArray(last_id);
58  return;
59  }
60 
61  // Big operation switch
62  Ptr<BackgroundSubtractorKNN> 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<BackgroundSubtractorKNN>(rhs[2].toString(), objname) :
91  Algorithm::load<BackgroundSubtractorKNN>(rhs[2].toString(), objname));
92  */
94  // HACK: workaround for missing BackgroundSubtractorKNN::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 == "DetectShadows")
134  plhs[0] = MxArray(obj->getDetectShadows());
135  else if (prop == "Dist2Threshold")
136  plhs[0] = MxArray(obj->getDist2Threshold());
137  else if (prop == "History")
138  plhs[0] = MxArray(obj->getHistory());
139  else if (prop == "kNNSamples")
140  plhs[0] = MxArray(obj->getkNNSamples());
141  else if (prop == "NSamples")
142  plhs[0] = MxArray(obj->getNSamples());
143  else if (prop == "ShadowThreshold")
144  plhs[0] = MxArray(obj->getShadowThreshold());
145  else if (prop == "ShadowValue")
146  plhs[0] = MxArray(obj->getShadowValue());
147  else
148  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized property");
149  }
150  else if (method == "set") {
151  nargchk(nrhs==4 && nlhs==0);
152  string prop(rhs[2].toString());
153  if (prop == "DetectShadows")
154  obj->setDetectShadows(rhs[3].toBool());
155  else if (prop == "Dist2Threshold")
156  obj->setDist2Threshold(rhs[3].toDouble());
157  else if (prop == "History")
158  obj->setHistory(rhs[3].toInt());
159  else if (prop == "kNNSamples")
160  obj->setkNNSamples(rhs[3].toInt());
161  else if (prop == "NSamples")
162  obj->setNSamples(rhs[3].toInt());
163  else if (prop == "ShadowThreshold")
164  obj->setShadowThreshold(rhs[3].toDouble());
165  else if (prop == "ShadowValue")
166  obj->setShadowValue(rhs[3].toInt());
167  else
168  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized property");
169  }
170  else
171  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
172 }
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.