9 #include "opencv2/video.hpp" 18 map<int,Ptr<BackgroundSubtractorMOG2> > 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 varThreshold = 16;
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==
"VarThreshold")
49 varThreshold = 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] = createBackgroundSubtractorMOG2(
56 history, varThreshold, detectShadows);
62 Ptr<BackgroundSubtractorMOG2> 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 ==
"BackgroundRatio")
134 plhs[0] =
MxArray(obj->getBackgroundRatio());
135 else if (prop ==
"ComplexityReductionThreshold")
136 plhs[0] =
MxArray(obj->getComplexityReductionThreshold());
137 else if (prop ==
"DetectShadows")
138 plhs[0] =
MxArray(obj->getDetectShadows());
139 else if (prop ==
"History")
140 plhs[0] =
MxArray(obj->getHistory());
141 else if (prop ==
"NMixtures")
142 plhs[0] =
MxArray(obj->getNMixtures());
143 else if (prop ==
"ShadowThreshold")
144 plhs[0] =
MxArray(obj->getShadowThreshold());
145 else if (prop ==
"ShadowValue")
146 plhs[0] =
MxArray(obj->getShadowValue());
147 else if (prop ==
"VarInit")
148 plhs[0] =
MxArray(obj->getVarInit());
149 else if (prop ==
"VarMax")
150 plhs[0] =
MxArray(obj->getVarMax());
151 else if (prop ==
"VarMin")
152 plhs[0] =
MxArray(obj->getVarMin());
153 else if (prop ==
"VarThreshold")
154 plhs[0] =
MxArray(obj->getVarThreshold());
155 else if (prop ==
"VarThresholdGen")
156 plhs[0] =
MxArray(obj->getVarThresholdGen());
158 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
160 else if (method ==
"set") {
162 string prop(rhs[2].toString());
163 if (prop ==
"BackgroundRatio")
164 obj->setBackgroundRatio(rhs[3].toDouble());
165 else if (prop ==
"ComplexityReductionThreshold")
166 obj->setComplexityReductionThreshold(rhs[3].toDouble());
167 else if (prop ==
"DetectShadows")
168 obj->setDetectShadows(rhs[3].toBool());
169 else if (prop ==
"History")
170 obj->setHistory(rhs[3].toInt());
171 else if (prop ==
"NMixtures")
172 obj->setNMixtures(rhs[3].toInt());
173 else if (prop ==
"ShadowThreshold")
174 obj->setShadowThreshold(rhs[3].toDouble());
175 else if (prop ==
"ShadowValue")
176 obj->setShadowValue(rhs[3].toInt());
177 else if (prop ==
"VarInit")
178 obj->setVarInit(rhs[3].toDouble());
179 else if (prop ==
"VarMax")
180 obj->setVarMax(rhs[3].toDouble());
181 else if (prop ==
"VarMin")
182 obj->setVarMin(rhs[3].toDouble());
183 else if (prop ==
"VarThreshold")
184 obj->setVarThreshold(rhs[3].toDouble());
185 else if (prop ==
"VarThresholdGen")
186 obj->setVarThresholdGen(rhs[3].toDouble());
188 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
191 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized operation");
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.