mexopencv  3.4.1
MEX interface for OpenCV library
BackgroundSubtractorCNT_.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 minPixelStability = 15;
43  int maxPixelStability = 15*60;
44  bool useHistory = true;
45  bool isParallel = true;
46  for (int i=2; i<nrhs; i+=2) {
47  string key(rhs[i].toString());
48  if (key == "MinPixelStability")
49  minPixelStability = rhs[i+1].toInt();
50  else if (key == "MaxPixelStability")
51  maxPixelStability = rhs[i+1].toInt();
52  else if (key == "UseHistory")
53  useHistory = rhs[i+1].toBool();
54  else if (key == "IsParallel")
55  isParallel = rhs[i+1].toBool();
56  else
57  mexErrMsgIdAndTxt("mexopencv:error",
58  "Unrecognized option %s", key.c_str());
59  }
61  minPixelStability, maxPixelStability, useHistory, isParallel);
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<BackgroundSubtractorCNT>(rhs[2].toString(), objname) :
101  Algorithm::load<BackgroundSubtractorCNT>(rhs[2].toString(), objname));
102  */
104  // HACK: workaround for missing BackgroundSubtractorCNT::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 == "MinPixelStability")
148  plhs[0] = MxArray(obj->getMinPixelStability());
149  else if (prop == "MaxPixelStability")
150  plhs[0] = MxArray(obj->getMaxPixelStability());
151  else if (prop == "UseHistory")
152  plhs[0] = MxArray(obj->getUseHistory());
153  else if (prop == "IsParallel")
154  plhs[0] = MxArray(obj->getIsParallel());
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 == "MinPixelStability")
163  obj->setMinPixelStability(rhs[3].toInt());
164  else if (prop == "MaxPixelStability")
165  obj->setMaxPixelStability(rhs[3].toInt());
166  else if (prop == "UseHistory")
167  obj->setUseHistory(rhs[3].toBool());
168  else if (prop == "IsParallel")
169  obj->setIsParallel(rhs[3].toBool());
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)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void getBackgroundImage(OutputArray backgroundImage) const=0
#define CV_8U
map< int, Ptr< BackgroundSubtractorCNT > > obj_
Object container.
virtual bool getIsParallel() const=0
STL namespace.
Ptr< BackgroundSubtractorCNT > createBackgroundSubtractorCNT(int minPixelStability=15, bool useHistory=true, int maxPixelStability=15 *60, bool isParallel=true)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setMaxPixelStability(int value)=0
virtual void clear()
virtual void read(const FileNode &fn)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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 void apply(InputArray image, OutputArray fgmask, double learningRate=-1)=0
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 setUseHistory(bool value)=0
virtual int getMaxPixelStability() const=0
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
virtual void setIsParallel(bool value)=0
virtual int getMinPixelStability() const=0
virtual void setMinPixelStability(int value)=0
cv::Mat toMat() const
virtual bool getUseHistory() const=0