9 #include "opencv2/video.hpp" 18 map<int,Ptr<BackgroundSubtractorKNN> > obj_;
28 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
34 vector<MxArray> rhs(prhs, prhs+nrhs);
35 int id = rhs[0].toInt();
36 string method(rhs[1].toString());
39 if (method ==
"new") {
40 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
42 double dist2Threshold = 400.0;
43 bool detectShadows =
true;
44 for (
int i=2; i<nrhs; i+=2) {
45 string key(rhs[i].toString());
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();
53 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
55 obj_[++last_id] = createBackgroundSubtractorKNN(
56 history, dist2Threshold, detectShadows);
62 Ptr<BackgroundSubtractorKNN> obj = obj_[id];
63 if (method ==
"delete") {
67 else if (method ==
"clear") {
71 else if (method ==
"save") {
73 obj->save(rhs[2].toString());
75 else if (method ==
"load") {
76 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
78 bool loadFromString =
false;
79 for (
int i=3; i<nrhs; i+=2) {
80 string key(rhs[i].toString());
82 objname = rhs[i+1].toString();
83 else if (key==
"FromString")
84 loadFromString = rhs[i+1].toBool();
86 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
95 FileStorage fs(rhs[2].toString(), FileStorage::READ +
96 (loadFromString ? FileStorage::MEMORY : 0));
97 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
99 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
102 else if (method ==
"empty") {
104 plhs[0] =
MxArray(obj->empty());
106 else if (method ==
"getDefaultName") {
108 plhs[0] =
MxArray(obj->getDefaultName());
110 else if (method ==
"apply") {
111 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
112 double learningRate = -1;
113 for (
int i=3; i<nrhs; i+=2) {
114 string key(rhs[i].toString());
115 if (key==
"LearningRate")
116 learningRate = rhs[i+1].toDouble();
118 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
120 Mat image(rhs[2].toMat()), fgmask;
121 obj->apply(image, fgmask, learningRate);
122 plhs[0] =
MxArray(fgmask, mxLOGICAL_CLASS);
124 else if (method ==
"getBackgroundImage") {
127 obj->getBackgroundImage(backgroundImage);
128 plhs[0] =
MxArray(backgroundImage);
130 else if (method ==
"get") {
132 string prop(rhs[2].toString());
133 if (prop ==
"DetectShadows")
134 plhs[0] =
MxArray(obj->getDetectShadows());
135 else if (prop ==
"Dist2Threshold")
136 plhs[0] =
MxArray(obj->getDist2Threshold());
137 else if (prop ==
"History")
138 plhs[0] =
MxArray(obj->getHistory());
139 else if (prop ==
"kNNSamples")
140 plhs[0] =
MxArray(obj->getkNNSamples());
141 else if (prop ==
"NSamples")
142 plhs[0] =
MxArray(obj->getNSamples());
143 else if (prop ==
"ShadowThreshold")
144 plhs[0] =
MxArray(obj->getShadowThreshold());
145 else if (prop ==
"ShadowValue")
146 plhs[0] =
MxArray(obj->getShadowValue());
148 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
150 else if (method ==
"set") {
152 string prop(rhs[2].toString());
153 if (prop ==
"DetectShadows")
154 obj->setDetectShadows(rhs[3].toBool());
155 else if (prop ==
"Dist2Threshold")
156 obj->setDist2Threshold(rhs[3].toDouble());
157 else if (prop ==
"History")
158 obj->setHistory(rhs[3].toInt());
159 else if (prop ==
"kNNSamples")
160 obj->setkNNSamples(rhs[3].toInt());
161 else if (prop ==
"NSamples")
162 obj->setNSamples(rhs[3].toInt());
163 else if (prop ==
"ShadowThreshold")
164 obj->setShadowThreshold(rhs[3].toDouble());
165 else if (prop ==
"ShadowValue")
166 obj->setShadowValue(rhs[3].toInt());
168 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
171 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized operation");
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.