mexopencv  3.4.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;
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",
58  "Unrecognized option %s", key.c_str());
59  }
61  history, nmixtures, backgroundRatio, noiseSigma);
62  plhs[0] = MxArray(last_id);
63  mexLock();
64  return;
65  }
66 
67  // Big operation switch
69  if (obj.empty())
70  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
71  if (method == "delete") {
72  nargchk(nrhs==2 && nlhs==0);
73  obj_.erase(id);
74  mexUnlock();
75  }
76  else if (method == "clear") {
77  nargchk(nrhs==2 && nlhs==0);
78  obj->clear();
79  }
80  else if (method == "save") {
81  nargchk(nrhs==3 && nlhs==0);
82  obj->save(rhs[2].toString());
83  }
84  else if (method == "load") {
85  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
86  string objname;
87  bool loadFromString = false;
88  for (int i=3; i<nrhs; i+=2) {
89  string key(rhs[i].toString());
90  if (key == "ObjName")
91  objname = rhs[i+1].toString();
92  else if (key == "FromString")
93  loadFromString = rhs[i+1].toBool();
94  else
95  mexErrMsgIdAndTxt("mexopencv:error",
96  "Unrecognized option %s", key.c_str());
97  }
98  /*
99  obj_[id] = (loadFromString ?
100  Algorithm::loadFromString<BackgroundSubtractorMOG>(rhs[2].toString(), objname) :
101  Algorithm::load<BackgroundSubtractorMOG>(rhs[2].toString(), objname));
102  */
104  // HACK: workaround for missing BackgroundSubtractorMOG::create()
105  FileStorage fs(rhs[2].toString(), FileStorage::READ +
106  (loadFromString ? FileStorage::MEMORY : 0));
107  if (!fs.isOpened())
108  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
109  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
110  if (fn.empty())
111  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
112  obj->read(fn);
113  //*/
114  }
115  else if (method == "empty") {
116  nargchk(nrhs==2 && nlhs<=1);
117  plhs[0] = MxArray(obj->empty());
118  }
119  else if (method == "getDefaultName") {
120  nargchk(nrhs==2 && nlhs<=1);
121  plhs[0] = MxArray(obj->getDefaultName());
122  }
123  else if (method == "apply") {
124  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
125  double learningRate = -1;
126  for (int i=3; i<nrhs; i+=2) {
127  string key(rhs[i].toString());
128  if (key == "LearningRate")
129  learningRate = rhs[i+1].toDouble();
130  else
131  mexErrMsgIdAndTxt("mexopencv:error",
132  "Unrecognized option %s", key.c_str());
133  }
134  Mat image(rhs[2].toMat(CV_8U)), fgmask;
135  obj->apply(image, fgmask, learningRate);
136  plhs[0] = MxArray(fgmask);
137  }
138  else if (method == "getBackgroundImage") {
139  nargchk(nrhs==2 && nlhs<=1);
140  Mat backgroundImage;
141  obj->getBackgroundImage(backgroundImage);
142  plhs[0] = MxArray(backgroundImage);
143  }
144  else if (method == "get") {
145  nargchk(nrhs==3 && nlhs<=1);
146  string prop(rhs[2].toString());
147  if (prop == "History")
148  plhs[0] = MxArray(obj->getHistory());
149  else if (prop == "NMixtures")
150  plhs[0] = MxArray(obj->getNMixtures());
151  else if (prop == "BackgroundRatio")
152  plhs[0] = MxArray(obj->getBackgroundRatio());
153  else if (prop == "NoiseSigma")
154  plhs[0] = MxArray(obj->getNoiseSigma());
155  else
156  mexErrMsgIdAndTxt("mexopencv:error",
157  "Unrecognized property %s", prop.c_str());
158  }
159  else if (method == "set") {
160  nargchk(nrhs==4 && nlhs==0);
161  string prop(rhs[2].toString());
162  if (prop == "History")
163  obj->setHistory(rhs[3].toInt());
164  else if (prop == "NMixtures")
165  obj->setNMixtures(rhs[3].toInt());
166  else if (prop == "BackgroundRatio")
167  obj->setBackgroundRatio(rhs[3].toDouble());
168  else if (prop == "NoiseSigma")
169  obj->setNoiseSigma(rhs[3].toDouble());
170  else
171  mexErrMsgIdAndTxt("mexopencv:error",
172  "Unrecognized property %s", prop.c_str());
173  }
174  else
175  mexErrMsgIdAndTxt("mexopencv:error",
176  "Unrecognized operation %s", method.c_str());
177 }
T empty(T... args)
virtual void setNMixtures(int nmix)=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual int getHistory() const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
Ptr< cuda::BackgroundSubtractorMOG > createBackgroundSubtractorMOG(int history=200, int nmixtures=5, double backgroundRatio=0.7, double noiseSigma=0)
#define CV_8U
virtual void setNoiseSigma(double noiseSigma)=0
virtual double getBackgroundRatio() const=0
STL namespace.
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1)=0
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual double getNoiseSigma() const=0
virtual void getBackgroundImage(OutputArray backgroundImage) const=0
virtual void clear()
virtual void read(const FileNode &fn)
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...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
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
virtual void setBackgroundRatio(double backgroundRatio)=0
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual int getNMixtures() const=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void setHistory(int nframes)=0
cv::Mat toMat() const
map< int, Ptr< BackgroundSubtractorMOG > > obj_
Object container.