mexopencv  3.4.1
MEX interface for OpenCV library
BackgroundSubtractorGSOC_.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 
25 }
26 
34 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
35 {
36  // Check the number of arguments
37  nargchk(nrhs>=2 && nlhs<=1);
38 
39  // Argument vector
40  vector<MxArray> rhs(prhs, prhs+nrhs);
41  int id = rhs[0].toInt();
42  string method(rhs[1].toString());
43 
44  // constructor call
45  if (method == "new") {
46  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
48  int nSamples = 20;
49  float replaceRate = 0.003f;
50  float propagationRate = 0.01f;
51  int hitsThreshold = 32;
52  float alpha = 0.01f;
53  float beta = 0.0022f;
54  float blinkingSupressionDecay = 0.1f;
55  float blinkingSupressionMultiplier = 0.1f;
56  float noiseRemovalThresholdFacBG = 0.0004f;
57  float noiseRemovalThresholdFacFG = 0.0008f;
58  for (int i=2; i<nrhs; i+=2) {
59  string key(rhs[i].toString());
60  if (key == "MotionCompensation")
61  mc = MotionCompensationsMap[rhs[i+1].toString()];
62  else if (key == "NSamples")
63  nSamples = rhs[i+1].toInt();
64  else if (key == "ReplaceRate")
65  replaceRate = rhs[i+1].toFloat();
66  else if (key == "PropagationRate")
67  propagationRate = rhs[i+1].toFloat();
68  else if (key == "HitsThreshold")
69  hitsThreshold = rhs[i+1].toInt();
70  else if (key == "Alpha")
71  alpha = rhs[i+1].toFloat();
72  else if (key == "Beta")
73  beta = rhs[i+1].toFloat();
74  else if (key == "BlinkingSupressionDecay")
75  blinkingSupressionDecay = rhs[i+1].toFloat();
76  else if (key == "BlinkingSupressionMultiplier")
77  blinkingSupressionMultiplier = rhs[i+1].toFloat();
78  else if (key == "NoiseRemovalThresholdFacBG")
79  noiseRemovalThresholdFacBG = rhs[i+1].toFloat();
80  else if (key == "NoiseRemovalThresholdFacFG")
81  noiseRemovalThresholdFacFG = rhs[i+1].toFloat();
82  else
83  mexErrMsgIdAndTxt("mexopencv:error",
84  "Unrecognized option %s", key.c_str());
85  }
87  replaceRate, propagationRate, hitsThreshold, alpha, beta,
88  blinkingSupressionDecay, blinkingSupressionMultiplier,
89  noiseRemovalThresholdFacBG, noiseRemovalThresholdFacFG);
90  plhs[0] = MxArray(last_id);
91  mexLock();
92  return;
93  }
94 
95  // Big operation switch
97  if (obj.empty())
98  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
99  if (method == "delete") {
100  nargchk(nrhs==2 && nlhs==0);
101  obj_.erase(id);
102  mexUnlock();
103  }
104  else if (method == "clear") {
105  nargchk(nrhs==2 && nlhs==0);
106  obj->clear();
107  }
108  else if (method == "save") {
109  nargchk(nrhs==3 && nlhs==0);
110  obj->save(rhs[2].toString());
111  }
112  else if (method == "load") {
113  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
114  string objname;
115  bool loadFromString = false;
116  for (int i=3; i<nrhs; i+=2) {
117  string key(rhs[i].toString());
118  if (key == "ObjName")
119  objname = rhs[i+1].toString();
120  else if (key == "FromString")
121  loadFromString = rhs[i+1].toBool();
122  else
123  mexErrMsgIdAndTxt("mexopencv:error",
124  "Unrecognized option %s", key.c_str());
125  }
126  /*
127  obj_[id] = (loadFromString ?
128  Algorithm::loadFromString<BackgroundSubtractorGSOC>(rhs[2].toString(), objname) :
129  Algorithm::load<BackgroundSubtractorGSOC>(rhs[2].toString(), objname));
130  */
132  // HACK: workaround for missing BackgroundSubtractorGSOC::create()
133  FileStorage fs(rhs[2].toString(), FileStorage::READ +
134  (loadFromString ? FileStorage::MEMORY : 0));
135  if (!fs.isOpened())
136  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
137  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
138  if (fn.empty())
139  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
140  obj->read(fn);
141  //*/
142  }
143  else if (method == "empty") {
144  nargchk(nrhs==2 && nlhs<=1);
145  plhs[0] = MxArray(obj->empty());
146  }
147  else if (method == "getDefaultName") {
148  nargchk(nrhs==2 && nlhs<=1);
149  plhs[0] = MxArray(obj->getDefaultName());
150  }
151  else if (method == "apply") {
152  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
153  double learningRate = -1;
154  for (int i=3; i<nrhs; i+=2) {
155  string key(rhs[i].toString());
156  if (key == "LearningRate")
157  learningRate = rhs[i+1].toDouble();
158  else
159  mexErrMsgIdAndTxt("mexopencv:error",
160  "Unrecognized option %s", key.c_str());
161  }
162  Mat image(rhs[2].toMat(rhs[2].isFloat() ? CV_32F : CV_8U)),
163  fgmask;
164  obj->apply(image, fgmask, learningRate);
165  plhs[0] = MxArray(fgmask);
166  }
167  else if (method == "getBackgroundImage") {
168  nargchk(nrhs==2 && nlhs<=1);
169  Mat backgroundImage;
170  obj->getBackgroundImage(backgroundImage);
171  plhs[0] = MxArray(backgroundImage);
172  }
173  else
174  mexErrMsgIdAndTxt("mexopencv:error",
175  "Unrecognized operation %s", method.c_str());
176 }
map< int, Ptr< BackgroundSubtractorGSOC > > obj_
Object container.
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1)=0
T empty(T... args)
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 bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
const ConstMap< string, int > MotionCompensationsMap
motion compensation types for option processing
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
virtual void getBackgroundImage(OutputArray backgroundImage) 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.
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.
Ptr< BackgroundSubtractorGSOC > createBackgroundSubtractorGSOC(int mc=LSBP_CAMERA_MOTION_COMPENSATION_NONE, int nSamples=20, float replaceRate=0.003f, float propagationRate=0.01f, int hitsThreshold=32, float alpha=0.01f, float beta=0.0022f, float blinkingSupressionDecay=0.1f, float blinkingSupressionMultiplier=0.1f, float noiseRemovalThresholdFacBG=0.0004f, float noiseRemovalThresholdFacFG=0.0008f)
T c_str(T... args)
LSBP_CAMERA_MOTION_COMPENSATION_NONE
LSBP_CAMERA_MOTION_COMPENSATION_LK
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void save(const String &filename) const
virtual bool empty() const
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const