mexopencv  3.4.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;
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",
54  "Unrecognized option %s", key.c_str());
55  }
57  history, varThreshold, detectShadows);
58  plhs[0] = MxArray(last_id);
59  mexLock();
60  return;
61  }
62 
63  // Big operation switch
65  if (obj.empty())
66  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
67  if (method == "delete") {
68  nargchk(nrhs==2 && nlhs==0);
69  obj_.erase(id);
70  mexUnlock();
71  }
72  else if (method == "clear") {
73  nargchk(nrhs==2 && nlhs==0);
74  obj->clear();
75  }
76  else if (method == "save") {
77  nargchk(nrhs==3 && nlhs==0);
78  obj->save(rhs[2].toString());
79  }
80  else if (method == "load") {
81  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
82  string objname;
83  bool loadFromString = false;
84  for (int i=3; i<nrhs; i+=2) {
85  string key(rhs[i].toString());
86  if (key == "ObjName")
87  objname = rhs[i+1].toString();
88  else if (key == "FromString")
89  loadFromString = rhs[i+1].toBool();
90  else
91  mexErrMsgIdAndTxt("mexopencv:error",
92  "Unrecognized option %s", key.c_str());
93  }
94  /*
95  obj_[id] = (loadFromString ?
96  Algorithm::loadFromString<BackgroundSubtractorMOG2>(rhs[2].toString(), objname) :
97  Algorithm::load<BackgroundSubtractorMOG2>(rhs[2].toString(), objname));
98  */
100  // HACK: workaround for missing BackgroundSubtractorMOG2::create()
101  FileStorage fs(rhs[2].toString(), FileStorage::READ +
102  (loadFromString ? FileStorage::MEMORY : 0));
103  if (!fs.isOpened())
104  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
105  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
106  if (fn.empty())
107  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
108  obj->read(fn);
109  //*/
110  }
111  else if (method == "empty") {
112  nargchk(nrhs==2 && nlhs<=1);
113  plhs[0] = MxArray(obj->empty());
114  }
115  else if (method == "getDefaultName") {
116  nargchk(nrhs==2 && nlhs<=1);
117  plhs[0] = MxArray(obj->getDefaultName());
118  }
119  else if (method == "apply") {
120  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
121  double learningRate = -1;
122  for (int i=3; i<nrhs; i+=2) {
123  string key(rhs[i].toString());
124  if (key == "LearningRate")
125  learningRate = rhs[i+1].toDouble();
126  else
127  mexErrMsgIdAndTxt("mexopencv:error",
128  "Unrecognized option %s", key.c_str());
129  }
130  Mat image(rhs[2].toMat(rhs[2].isFloat() ? CV_32F : CV_8U)), fgmask;
131  obj->apply(image, fgmask, learningRate);
132  plhs[0] = MxArray(fgmask);
133  }
134  else if (method == "getBackgroundImage") {
135  nargchk(nrhs==2 && nlhs<=1);
136  Mat backgroundImage;
137  obj->getBackgroundImage(backgroundImage);
138  plhs[0] = MxArray(backgroundImage);
139  }
140  else if (method == "get") {
141  nargchk(nrhs==3 && nlhs<=1);
142  string prop(rhs[2].toString());
143  if (prop == "History")
144  plhs[0] = MxArray(obj->getHistory());
145  else if (prop == "NMixtures")
146  plhs[0] = MxArray(obj->getNMixtures());
147  else if (prop == "BackgroundRatio")
148  plhs[0] = MxArray(obj->getBackgroundRatio());
149  else if (prop == "VarThreshold")
150  plhs[0] = MxArray(obj->getVarThreshold());
151  else if (prop == "VarThresholdGen")
152  plhs[0] = MxArray(obj->getVarThresholdGen());
153  else if (prop == "VarInit")
154  plhs[0] = MxArray(obj->getVarInit());
155  else if (prop == "VarMin")
156  plhs[0] = MxArray(obj->getVarMin());
157  else if (prop == "VarMax")
158  plhs[0] = MxArray(obj->getVarMax());
159  else if (prop == "ComplexityReductionThreshold")
160  plhs[0] = MxArray(obj->getComplexityReductionThreshold());
161  else if (prop == "DetectShadows")
162  plhs[0] = MxArray(obj->getDetectShadows());
163  else if (prop == "ShadowValue")
164  plhs[0] = MxArray(obj->getShadowValue());
165  else if (prop == "ShadowThreshold")
166  plhs[0] = MxArray(obj->getShadowThreshold());
167  else
168  mexErrMsgIdAndTxt("mexopencv:error",
169  "Unrecognized property %s", prop.c_str());
170  }
171  else if (method == "set") {
172  nargchk(nrhs==4 && nlhs==0);
173  string prop(rhs[2].toString());
174  if (prop == "History")
175  obj->setHistory(rhs[3].toInt());
176  else if (prop == "NMixtures")
177  obj->setNMixtures(rhs[3].toInt());
178  else if (prop == "BackgroundRatio")
179  obj->setBackgroundRatio(rhs[3].toDouble());
180  else if (prop == "VarThreshold")
181  obj->setVarThreshold(rhs[3].toDouble());
182  else if (prop == "VarThresholdGen")
183  obj->setVarThresholdGen(rhs[3].toDouble());
184  else if (prop == "VarInit")
185  obj->setVarInit(rhs[3].toDouble());
186  else if (prop == "VarMin")
187  obj->setVarMin(rhs[3].toDouble());
188  else if (prop == "VarMax")
189  obj->setVarMax(rhs[3].toDouble());
190  else if (prop == "ComplexityReductionThreshold")
191  obj->setComplexityReductionThreshold(rhs[3].toDouble());
192  else if (prop == "DetectShadows")
193  obj->setDetectShadows(rhs[3].toBool());
194  else if (prop == "ShadowValue")
195  obj->setShadowValue(rhs[3].toInt());
196  else if (prop == "ShadowThreshold")
197  obj->setShadowThreshold(rhs[3].toDouble());
198  else
199  mexErrMsgIdAndTxt("mexopencv:error",
200  "Unrecognized property %s", prop.c_str());
201  }
202  else
203  mexErrMsgIdAndTxt("mexopencv:error",
204  "Unrecognized operation %s", method.c_str());
205 }
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1)=0
T empty(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void setVarInit(double varInit)=0
virtual int getHistory() const=0
virtual void setNMixtures(int nmixtures)=0
virtual void setComplexityReductionThreshold(double ct)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void setHistory(int history)=0
virtual bool getDetectShadows() const=0
virtual int getShadowValue() const=0
virtual double getVarMin() const=0
#define CV_8U
STL namespace.
virtual void setVarThresholdGen(double varThresholdGen)=0
virtual bool isOpened() const
virtual void setVarThreshold(double varThreshold)=0
virtual double getVarInit() const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void getBackgroundImage(OutputArray backgroundImage) const=0
virtual int getNMixtures() const=0
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
virtual void setShadowThreshold(double threshold)=0
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...
virtual double getComplexityReductionThreshold() const=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
map< int, Ptr< BackgroundSubtractorMOG2 > > obj_
Object container.
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
Global constant definitions.
virtual double getVarMax() const=0
T c_str(T... args)
virtual double getVarThresholdGen() const=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void setShadowValue(int value)=0
virtual double getShadowThreshold() const=0
virtual void setVarMin(double varMin)=0
virtual double getBackgroundRatio() const=0
virtual void setBackgroundRatio(double ratio)=0
virtual double getVarThreshold() const=0
cv::Mat toMat() const
virtual void setDetectShadows(bool detectShadows)=0
virtual void setVarMax(double varMax)=0
Ptr< BackgroundSubtractorMOG2 > createBackgroundSubtractorMOG2(int history=500, double varThreshold=16, bool detectShadows=true)