9 #include "opencv2/bgsegm.hpp" 19 map<int,Ptr<BackgroundSubtractorGMG> > obj_;
29 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
35 vector<MxArray> rhs(prhs, prhs+nrhs);
36 int id = rhs[0].toInt();
37 string method(rhs[1].toString());
40 if (method ==
"new") {
41 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
42 int initializationFrames = 120;
43 double decisionThreshold = 0.8;
44 for (
int i=2; i<nrhs; i+=2) {
45 string key(rhs[i].toString());
46 if (key==
"InitializationFrames")
47 initializationFrames = rhs[i+1].toInt();
48 else if (key==
"DecisionThreshold")
49 decisionThreshold = rhs[i+1].toDouble();
51 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
53 obj_[++last_id] = createBackgroundSubtractorGMG(
54 initializationFrames, decisionThreshold);
60 Ptr<BackgroundSubtractorGMG> obj = obj_[id];
61 if (method ==
"delete") {
65 else if (method ==
"clear") {
69 else if (method ==
"save") {
71 obj->save(rhs[2].toString());
73 else if (method ==
"load") {
74 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
76 bool loadFromString =
false;
77 for (
int i=3; i<nrhs; i+=2) {
78 string key(rhs[i].toString());
80 objname = rhs[i+1].toString();
81 else if (key==
"FromString")
82 loadFromString = rhs[i+1].toBool();
84 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
93 FileStorage fs(rhs[2].toString(), FileStorage::READ +
94 (loadFromString ? FileStorage::MEMORY : 0));
95 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
97 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
100 else if (method ==
"empty") {
102 plhs[0] =
MxArray(obj->empty());
104 else if (method ==
"getDefaultName") {
106 plhs[0] =
MxArray(obj->getDefaultName());
108 else if (method ==
"apply") {
109 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
110 double learningRate = -1;
111 for (
int i=3; i<nrhs; i+=2) {
112 string key(rhs[i].toString());
113 if (key==
"LearningRate")
114 learningRate = rhs[i+1].toDouble();
116 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
118 Mat image(rhs[2].toMat()), fgmask;
119 obj->apply(image, fgmask, learningRate);
120 plhs[0] =
MxArray(fgmask, mxLOGICAL_CLASS);
122 else if (method ==
"getBackgroundImage") {
125 obj->getBackgroundImage(backgroundImage);
126 plhs[0] =
MxArray(backgroundImage);
128 else if (method ==
"get") {
130 string prop(rhs[2].toString());
131 if (prop ==
"BackgroundPrior")
132 plhs[0] =
MxArray(obj->getBackgroundPrior());
133 else if (prop ==
"DecisionThreshold")
134 plhs[0] =
MxArray(obj->getDecisionThreshold());
135 else if (prop ==
"DefaultLearningRate")
136 plhs[0] =
MxArray(obj->getDefaultLearningRate());
137 else if (prop ==
"MaxFeatures")
138 plhs[0] =
MxArray(obj->getMaxFeatures());
139 else if (prop ==
"MaxVal")
140 plhs[0] =
MxArray(obj->getMaxVal());
141 else if (prop ==
"MinVal")
142 plhs[0] =
MxArray(obj->getMinVal());
143 else if (prop ==
"NumFrames")
144 plhs[0] =
MxArray(obj->getNumFrames());
145 else if (prop ==
"QuantizationLevels")
146 plhs[0] =
MxArray(obj->getQuantizationLevels());
147 else if (prop ==
"SmoothingRadius")
148 plhs[0] =
MxArray(obj->getSmoothingRadius());
149 else if (prop ==
"UpdateBackgroundModel")
150 plhs[0] =
MxArray(obj->getUpdateBackgroundModel());
152 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
154 else if (method ==
"set") {
156 string prop(rhs[2].toString());
157 if (prop ==
"BackgroundPrior")
158 obj->setBackgroundPrior(rhs[3].toDouble());
159 else if (prop ==
"DecisionThreshold")
160 obj->setDecisionThreshold(rhs[3].toDouble());
161 else if (prop ==
"DefaultLearningRate")
162 obj->setDefaultLearningRate(rhs[3].toDouble());
163 else if (prop ==
"MaxFeatures")
164 obj->setMaxFeatures(rhs[3].toInt());
165 else if (prop ==
"MaxVal")
166 obj->setMaxVal(rhs[3].toDouble());
167 else if (prop ==
"MinVal")
168 obj->setMinVal(rhs[3].toDouble());
169 else if (prop ==
"NumFrames")
170 obj->setNumFrames(rhs[3].toInt());
171 else if (prop ==
"QuantizationLevels")
172 obj->setQuantizationLevels(rhs[3].toInt());
173 else if (prop ==
"SmoothingRadius")
174 obj->setSmoothingRadius(rhs[3].toInt());
175 else if (prop ==
"UpdateBackgroundModel")
176 obj->setUpdateBackgroundModel(rhs[3].toBool());
178 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
181 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.