mexopencv  3.4.1
MEX interface for OpenCV library
BackgroundSubtractorKNN_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/video.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=2 && nlhs<=1);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35  int id = rhs[0].toInt();
36  string method(rhs[1].toString());
37 
38  // constructor call
39  if (method == "new") {
40  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
41  int history = 500;
42  double dist2Threshold = 400.0;
43  bool detectShadows = true;
44  for (int i=2; i<nrhs; i+=2) {
45  string key(rhs[i].toString());
46  if (key == "History")
47  history = rhs[i+1].toInt();
48  else if (key == "Dist2Threshold")
49  dist2Threshold = rhs[i+1].toDouble();
50  else if (key == "DetectShadows")
51  detectShadows = rhs[i+1].toBool();
52  else
53  mexErrMsgIdAndTxt("mexopencv:error",
54  "Unrecognized option %s", key.c_str());
55  }
57  history, dist2Threshold, detectShadows);
58  plhs[0] = MxArray(last_id);
59  mexLock();
60  return;
61  }
62 
63  // Big operation switch
65  if (obj.empty())
66  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
67  if (method == "delete") {
68  nargchk(nrhs==2 && nlhs==0);
69  obj_.erase(id);
70  mexUnlock();
71  }
72  else if (method == "clear") {
73  nargchk(nrhs==2 && nlhs==0);
74  obj->clear();
75  }
76  else if (method == "save") {
77  nargchk(nrhs==3 && nlhs==0);
78  obj->save(rhs[2].toString());
79  }
80  else if (method == "load") {
81  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
82  string objname;
83  bool loadFromString = false;
84  for (int i=3; i<nrhs; i+=2) {
85  string key(rhs[i].toString());
86  if (key == "ObjName")
87  objname = rhs[i+1].toString();
88  else if (key == "FromString")
89  loadFromString = rhs[i+1].toBool();
90  else
91  mexErrMsgIdAndTxt("mexopencv:error",
92  "Unrecognized option %s", key.c_str());
93  }
94  /*
95  obj_[id] = (loadFromString ?
96  Algorithm::loadFromString<BackgroundSubtractorKNN>(rhs[2].toString(), objname) :
97  Algorithm::load<BackgroundSubtractorKNN>(rhs[2].toString(), objname));
98  */
100  // HACK: workaround for missing BackgroundSubtractorKNN::create()
101  FileStorage fs(rhs[2].toString(), FileStorage::READ +
102  (loadFromString ? FileStorage::MEMORY : 0));
103  if (!fs.isOpened())
104  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
105  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
106  if (fn.empty())
107  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
108  obj->read(fn);
109  //*/
110  }
111  else if (method == "empty") {
112  nargchk(nrhs==2 && nlhs<=1);
113  plhs[0] = MxArray(obj->empty());
114  }
115  else if (method == "getDefaultName") {
116  nargchk(nrhs==2 && nlhs<=1);
117  plhs[0] = MxArray(obj->getDefaultName());
118  }
119  else if (method == "apply") {
120  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
121  double learningRate = -1;
122  for (int i=3; i<nrhs; i+=2) {
123  string key(rhs[i].toString());
124  if (key == "LearningRate")
125  learningRate = rhs[i+1].toDouble();
126  else
127  mexErrMsgIdAndTxt("mexopencv:error",
128  "Unrecognized option %s", key.c_str());
129  }
130  Mat image(rhs[2].toMat(CV_8U)), fgmask;
131  obj->apply(image, fgmask, learningRate);
132  plhs[0] = MxArray(fgmask);
133  }
134  else if (method == "getBackgroundImage") {
135  nargchk(nrhs==2 && nlhs<=1);
136  Mat backgroundImage;
137  obj->getBackgroundImage(backgroundImage);
138  plhs[0] = MxArray(backgroundImage);
139  }
140  else if (method == "get") {
141  nargchk(nrhs==3 && nlhs<=1);
142  string prop(rhs[2].toString());
143  if (prop == "History")
144  plhs[0] = MxArray(obj->getHistory());
145  else if (prop == "NSamples")
146  plhs[0] = MxArray(obj->getNSamples());
147  else if (prop == "kNNSamples")
148  plhs[0] = MxArray(obj->getkNNSamples());
149  else if (prop == "Dist2Threshold")
150  plhs[0] = MxArray(obj->getDist2Threshold());
151  else if (prop == "DetectShadows")
152  plhs[0] = MxArray(obj->getDetectShadows());
153  else if (prop == "ShadowValue")
154  plhs[0] = MxArray(obj->getShadowValue());
155  else if (prop == "ShadowThreshold")
156  plhs[0] = MxArray(obj->getShadowThreshold());
157  else
158  mexErrMsgIdAndTxt("mexopencv:error",
159  "Unrecognized property %s", prop.c_str());
160  }
161  else if (method == "set") {
162  nargchk(nrhs==4 && nlhs==0);
163  string prop(rhs[2].toString());
164  if (prop == "History")
165  obj->setHistory(rhs[3].toInt());
166  else if (prop == "NSamples")
167  obj->setNSamples(rhs[3].toInt());
168  else if (prop == "kNNSamples")
169  obj->setkNNSamples(rhs[3].toInt());
170  else if (prop == "Dist2Threshold")
171  obj->setDist2Threshold(rhs[3].toDouble());
172  else if (prop == "DetectShadows")
173  obj->setDetectShadows(rhs[3].toBool());
174  else if (prop == "ShadowValue")
175  obj->setShadowValue(rhs[3].toInt());
176  else if (prop == "ShadowThreshold")
177  obj->setShadowThreshold(rhs[3].toDouble());
178  else
179  mexErrMsgIdAndTxt("mexopencv:error",
180  "Unrecognized property %s", prop.c_str());
181  }
182  else
183  mexErrMsgIdAndTxt("mexopencv:error",
184  "Unrecognized operation %s", method.c_str());
185 }
virtual bool getDetectShadows() const=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 void apply(InputArray image, OutputArray fgmask, double learningRate=-1)=0
map< int, Ptr< BackgroundSubtractorKNN > > obj_
Object container.
virtual bool isOpened() const
virtual double getShadowThreshold() const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void getBackgroundImage(OutputArray backgroundImage) const=0
virtual void setShadowValue(int value)=0
virtual double getDist2Threshold() const=0
virtual void setHistory(int history)=0
virtual void clear()
virtual void read(const FileNode &fn)
virtual int getShadowValue() const=0
virtual int getNSamples() 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...
virtual void setNSamples(int _nN)=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual int getkNNSamples() 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 void setkNNSamples(int _nkNN)=0
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
virtual void setShadowThreshold(double threshold)=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
T c_str(T... args)
virtual void setDist2Threshold(double _dist2Threshold)=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void setDetectShadows(bool detectShadows)=0
Ptr< BackgroundSubtractorKNN > createBackgroundSubtractorKNN(int history=500, double dist2Threshold=400.0, bool detectShadows=true)
cv::Mat toMat() const
virtual int getHistory() const=0