mexopencv  3.4.1
MEX interface for OpenCV library
BackgroundSubtractorLSBP_.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  int LSBPRadius = 16;
50  float Tlower = 2.0f;
51  float Tupper = 32.0f;
52  float Tinc = 1.0f;
53  float Tdec = 0.05f;
54  float Rscale = 10.0f;
55  float Rincdec = 0.005f;
56  float noiseRemovalThresholdFacBG = 0.0004f;
57  float noiseRemovalThresholdFacFG = 0.0008f;
58  int LSBPthreshold = 8;
59  int minCount = 2;
60  for (int i=2; i<nrhs; i+=2) {
61  string key(rhs[i].toString());
62  if (key == "MotionCompensation")
63  mc = MotionCompensationsMap[rhs[i+1].toString()];
64  else if (key == "NSamples")
65  nSamples = rhs[i+1].toInt();
66  else if (key == "LSBPRadius")
67  LSBPRadius = rhs[i+1].toInt();
68  else if (key == "TLower")
69  Tlower = rhs[i+1].toFloat();
70  else if (key == "TUpper")
71  Tupper = rhs[i+1].toFloat();
72  else if (key == "TInc")
73  Tinc = rhs[i+1].toFloat();
74  else if (key == "TDec")
75  Tdec = rhs[i+1].toFloat();
76  else if (key == "RScale")
77  Rscale = rhs[i+1].toFloat();
78  else if (key == "RIncDec")
79  Rincdec = rhs[i+1].toFloat();
80  else if (key == "NoiseRemovalThresholdFacBG")
81  noiseRemovalThresholdFacBG = rhs[i+1].toFloat();
82  else if (key == "NoiseRemovalThresholdFacFG")
83  noiseRemovalThresholdFacFG = rhs[i+1].toFloat();
84  else if (key == "LSBPThreshold")
85  LSBPthreshold = rhs[i+1].toInt();
86  else if (key == "MinCount")
87  minCount = rhs[i+1].toInt();
88  else
89  mexErrMsgIdAndTxt("mexopencv:error",
90  "Unrecognized option %s", key.c_str());
91  }
93  LSBPRadius, Tlower, Tupper, Tinc, Tdec, Rscale, Rincdec,
94  noiseRemovalThresholdFacBG, noiseRemovalThresholdFacFG,
95  LSBPthreshold, minCount);
96  plhs[0] = MxArray(last_id);
97  mexLock();
98  return;
99  }
100  // static method call
101  else if (method == "computeLSBPDesc") {
102  nargchk(nrhs==4 && nlhs<=1);
103  Mat frame(rhs[2].toMat(CV_32F)),
104  desc;
105  vector<Point2i> LSBPSamplePoints(rhs[3].toVector<Point2i>());
106  if (LSBPSamplePoints.size() != 32)
107  mexErrMsgIdAndTxt("mexopencv:error", "32 points required");
108  BackgroundSubtractorLSBPDesc::compute(desc, frame, &LSBPSamplePoints[0]);
109  plhs[0] = MxArray(desc);
110  return;
111  }
112 
113  // Big operation switch
115  if (obj.empty())
116  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
117  if (method == "delete") {
118  nargchk(nrhs==2 && nlhs==0);
119  obj_.erase(id);
120  mexUnlock();
121  }
122  else if (method == "clear") {
123  nargchk(nrhs==2 && nlhs==0);
124  obj->clear();
125  }
126  else if (method == "save") {
127  nargchk(nrhs==3 && nlhs==0);
128  obj->save(rhs[2].toString());
129  }
130  else if (method == "load") {
131  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
132  string objname;
133  bool loadFromString = false;
134  for (int i=3; i<nrhs; i+=2) {
135  string key(rhs[i].toString());
136  if (key == "ObjName")
137  objname = rhs[i+1].toString();
138  else if (key == "FromString")
139  loadFromString = rhs[i+1].toBool();
140  else
141  mexErrMsgIdAndTxt("mexopencv:error",
142  "Unrecognized option %s", key.c_str());
143  }
144  /*
145  obj_[id] = (loadFromString ?
146  Algorithm::loadFromString<BackgroundSubtractorLSBP>(rhs[2].toString(), objname) :
147  Algorithm::load<BackgroundSubtractorLSBP>(rhs[2].toString(), objname));
148  */
150  // HACK: workaround for missing BackgroundSubtractorLSBP::create()
151  FileStorage fs(rhs[2].toString(), FileStorage::READ +
152  (loadFromString ? FileStorage::MEMORY : 0));
153  if (!fs.isOpened())
154  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
155  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
156  if (fn.empty())
157  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
158  obj->read(fn);
159  //*/
160  }
161  else if (method == "empty") {
162  nargchk(nrhs==2 && nlhs<=1);
163  plhs[0] = MxArray(obj->empty());
164  }
165  else if (method == "getDefaultName") {
166  nargchk(nrhs==2 && nlhs<=1);
167  plhs[0] = MxArray(obj->getDefaultName());
168  }
169  else if (method == "apply") {
170  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
171  double learningRate = -1;
172  for (int i=3; i<nrhs; i+=2) {
173  string key(rhs[i].toString());
174  if (key == "LearningRate")
175  learningRate = rhs[i+1].toDouble();
176  else
177  mexErrMsgIdAndTxt("mexopencv:error",
178  "Unrecognized option %s", key.c_str());
179  }
180  Mat image(rhs[2].toMat(rhs[2].isFloat() ? CV_32F : CV_8U)),
181  fgmask;
182  obj->apply(image, fgmask, learningRate);
183  plhs[0] = MxArray(fgmask);
184  }
185  else if (method == "getBackgroundImage") {
186  nargchk(nrhs==2 && nlhs<=1);
187  Mat backgroundImage;
188  obj->getBackgroundImage(backgroundImage);
189  plhs[0] = MxArray(backgroundImage);
190  }
191  else
192  mexErrMsgIdAndTxt("mexopencv:error",
193  "Unrecognized operation %s", method.c_str());
194 }
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
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
STL class.
virtual void clear()
const ConstMap< string, int > MotionCompensationsMap
motion compensation types for option processing
#define CV_32F
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
T size(T... args)
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
Ptr< BackgroundSubtractorLSBP > createBackgroundSubtractorLSBP(int mc=LSBP_CAMERA_MOTION_COMPENSATION_NONE, int nSamples=20, int LSBPRadius=16, float Tlower=2.0f, float Tupper=32.0f, float Tinc=1.0f, float Tdec=0.05f, float Rscale=10.0f, float Rincdec=0.005f, float noiseRemovalThresholdFacBG=0.0004f, float noiseRemovalThresholdFacFG=0.0008f, int LSBPthreshold=8, int minCount=2)
T c_str(T... args)
LSBP_CAMERA_MOTION_COMPENSATION_NONE
LSBP_CAMERA_MOTION_COMPENSATION_LK
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1)=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void getBackgroundImage(OutputArray backgroundImage) const=0
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
map< int, Ptr< BackgroundSubtractorLSBP > > obj_
Object container.
cv::Mat toMat() const