mexopencv  3.4.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;
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",
52  "Unrecognized option %s", key.c_str());
53  }
55  initializationFrames, decisionThreshold);
56  plhs[0] = MxArray(last_id);
57  mexLock();
58  return;
59  }
60 
61  // Big operation switch
63  if (obj.empty())
64  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
65  if (method == "delete") {
66  nargchk(nrhs==2 && nlhs==0);
67  obj_.erase(id);
68  mexUnlock();
69  }
70  else if (method == "clear") {
71  nargchk(nrhs==2 && nlhs==0);
72  obj->clear();
73  }
74  else if (method == "save") {
75  nargchk(nrhs==3 && nlhs==0);
76  obj->save(rhs[2].toString());
77  }
78  else if (method == "load") {
79  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
80  string objname;
81  bool loadFromString = false;
82  for (int i=3; i<nrhs; i+=2) {
83  string key(rhs[i].toString());
84  if (key == "ObjName")
85  objname = rhs[i+1].toString();
86  else if (key == "FromString")
87  loadFromString = rhs[i+1].toBool();
88  else
89  mexErrMsgIdAndTxt("mexopencv:error",
90  "Unrecognized option %s", key.c_str());
91  }
92  /*
93  obj_[id] = (loadFromString ?
94  Algorithm::loadFromString<BackgroundSubtractorGMG>(rhs[2].toString(), objname) :
95  Algorithm::load<BackgroundSubtractorGMG>(rhs[2].toString(), objname));
96  */
98  // HACK: workaround for missing BackgroundSubtractorGMG::create()
99  FileStorage fs(rhs[2].toString(), FileStorage::READ +
100  (loadFromString ? FileStorage::MEMORY : 0));
101  if (!fs.isOpened())
102  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
103  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
104  if (fn.empty())
105  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
106  obj->read(fn);
107  //*/
108  }
109  else if (method == "empty") {
110  nargchk(nrhs==2 && nlhs<=1);
111  plhs[0] = MxArray(obj->empty());
112  }
113  else if (method == "getDefaultName") {
114  nargchk(nrhs==2 && nlhs<=1);
115  plhs[0] = MxArray(obj->getDefaultName());
116  }
117  else if (method == "apply") {
118  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
119  double learningRate = -1;
120  for (int i=3; i<nrhs; i+=2) {
121  string key(rhs[i].toString());
122  if (key == "LearningRate")
123  learningRate = rhs[i+1].toDouble();
124  else
125  mexErrMsgIdAndTxt("mexopencv:error",
126  "Unrecognized option %s", key.c_str());
127  }
128  Mat image(rhs[2].toMat(rhs[2].isFloat() ? CV_32F :
129  (rhs[2].isUint16() ? CV_16U : CV_8U))), fgmask;
130  obj->apply(image, fgmask, learningRate);
131  plhs[0] = MxArray(fgmask);
132  }
133  else if (method == "getBackgroundImage") {
134  nargchk(nrhs==2 && nlhs<=1);
135  Mat backgroundImage;
136  obj->getBackgroundImage(backgroundImage);
137  plhs[0] = MxArray(backgroundImage);
138  }
139  else if (method == "get") {
140  nargchk(nrhs==3 && nlhs<=1);
141  string prop(rhs[2].toString());
142  if (prop == "MaxFeatures")
143  plhs[0] = MxArray(obj->getMaxFeatures());
144  else if (prop == "DefaultLearningRate")
145  plhs[0] = MxArray(obj->getDefaultLearningRate());
146  else if (prop == "NumFrames")
147  plhs[0] = MxArray(obj->getNumFrames());
148  else if (prop == "QuantizationLevels")
149  plhs[0] = MxArray(obj->getQuantizationLevels());
150  else if (prop == "BackgroundPrior")
151  plhs[0] = MxArray(obj->getBackgroundPrior());
152  else if (prop == "SmoothingRadius")
153  plhs[0] = MxArray(obj->getSmoothingRadius());
154  else if (prop == "DecisionThreshold")
155  plhs[0] = MxArray(obj->getDecisionThreshold());
156  else if (prop == "UpdateBackgroundModel")
157  plhs[0] = MxArray(obj->getUpdateBackgroundModel());
158  else if (prop == "MinVal")
159  plhs[0] = MxArray(obj->getMinVal());
160  else if (prop == "MaxVal")
161  plhs[0] = MxArray(obj->getMaxVal());
162  else
163  mexErrMsgIdAndTxt("mexopencv:error",
164  "Unrecognized property %s", prop.c_str());
165  }
166  else if (method == "set") {
167  nargchk(nrhs==4 && nlhs==0);
168  string prop(rhs[2].toString());
169  if (prop == "MaxFeatures")
170  obj->setMaxFeatures(rhs[3].toInt());
171  else if (prop == "DefaultLearningRate")
172  obj->setDefaultLearningRate(rhs[3].toDouble());
173  else if (prop == "NumFrames")
174  obj->setNumFrames(rhs[3].toInt());
175  else if (prop == "QuantizationLevels")
176  obj->setQuantizationLevels(rhs[3].toInt());
177  else if (prop == "BackgroundPrior")
178  obj->setBackgroundPrior(rhs[3].toDouble());
179  else if (prop == "SmoothingRadius")
180  obj->setSmoothingRadius(rhs[3].toInt());
181  else if (prop == "DecisionThreshold")
182  obj->setDecisionThreshold(rhs[3].toDouble());
183  else if (prop == "UpdateBackgroundModel")
184  obj->setUpdateBackgroundModel(rhs[3].toBool());
185  else if (prop == "MinVal")
186  obj->setMinVal(rhs[3].toDouble());
187  else if (prop == "MaxVal")
188  obj->setMaxVal(rhs[3].toDouble());
189  else
190  mexErrMsgIdAndTxt("mexopencv:error",
191  "Unrecognized property %s", prop.c_str());
192  }
193  else
194  mexErrMsgIdAndTxt("mexopencv:error",
195  "Unrecognized operation %s", method.c_str());
196 }
virtual void setMaxFeatures(int maxFeatures)=0
virtual void setQuantizationLevels(int nlevels)=0
virtual void setMinVal(double val)=0
T empty(T... args)
virtual void setSmoothingRadius(int radius)=0
virtual bool getUpdateBackgroundModel() const=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
STL namespace.
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1)=0
Ptr< cuda::BackgroundSubtractorGMG > createBackgroundSubtractorGMG(int initializationFrames=120, double decisionThreshold=0.8)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
virtual int getMaxFeatures() const=0
STL class.
virtual double getMinVal() const=0
virtual void getBackgroundImage(OutputArray backgroundImage) const=0
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
virtual double getMaxVal() const=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...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual int getNumFrames() const=0
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
virtual int getSmoothingRadius() const=0
FileNode getFirstTopLevelNode() const
#define CV_16U
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
virtual double getDefaultLearningRate() const=0
T c_str(T... args)
virtual void setDefaultLearningRate(double lr)=0
virtual void setDecisionThreshold(double thresh)=0
virtual void setUpdateBackgroundModel(bool update)=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual double getBackgroundPrior() const=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual double getDecisionThreshold() const=0
virtual int getQuantizationLevels() const=0
map< int, Ptr< BackgroundSubtractorGMG > > obj_
Object container.
virtual void setNumFrames(int nframes)=0
cv::Mat toMat() const
virtual void setBackgroundPrior(double bgprior)=0
virtual void setMaxVal(double val)=0