9 #include "opencv2/bgsegm.hpp" 19 map<int,Ptr<BackgroundSubtractorMOG> > 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);
44 double backgroundRatio = 0.7;
45 double noiseSigma = 0;
46 for (
int i=2; i<nrhs; i+=2) {
47 string key(rhs[i].toString());
49 history = rhs[i+1].toInt();
50 else if (key==
"NMixtures")
51 nmixtures = rhs[i+1].toInt();
52 else if (key==
"BackgroundRatio")
53 backgroundRatio = rhs[i+1].toDouble();
54 else if (key==
"NoiseSigma")
55 noiseSigma = rhs[i+1].toDouble();
57 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
59 obj_[++last_id] = createBackgroundSubtractorMOG(
60 history, nmixtures, backgroundRatio, noiseSigma);
66 Ptr<BackgroundSubtractorMOG> obj = obj_[id];
67 if (method ==
"delete") {
71 else if (method ==
"clear") {
75 else if (method ==
"save") {
77 obj->save(rhs[2].toString());
79 else if (method ==
"load") {
80 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
82 bool loadFromString =
false;
83 for (
int i=3; i<nrhs; i+=2) {
84 string key(rhs[i].toString());
86 objname = rhs[i+1].toString();
87 else if (key==
"FromString")
88 loadFromString = rhs[i+1].toBool();
90 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
99 FileStorage fs(rhs[2].toString(), FileStorage::READ +
100 (loadFromString ? FileStorage::MEMORY : 0));
101 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
103 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
106 else if (method ==
"empty") {
108 plhs[0] =
MxArray(obj->empty());
110 else if (method ==
"getDefaultName") {
112 plhs[0] =
MxArray(obj->getDefaultName());
114 else if (method ==
"apply") {
115 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
116 double learningRate = -1;
117 for (
int i=3; i<nrhs; i+=2) {
118 string key(rhs[i].toString());
119 if (key==
"LearningRate")
120 learningRate = rhs[i+1].toDouble();
122 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
124 Mat image(rhs[2].toMat()), fgmask;
125 obj->apply(image, fgmask, learningRate);
126 plhs[0] =
MxArray(fgmask, mxLOGICAL_CLASS);
128 else if (method ==
"getBackgroundImage") {
131 obj->getBackgroundImage(backgroundImage);
132 plhs[0] =
MxArray(backgroundImage);
134 else if (method ==
"get") {
136 string prop(rhs[2].toString());
137 if (prop ==
"BackgroundRatio")
138 plhs[0] =
MxArray(obj->getBackgroundRatio());
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 ==
"NoiseSigma")
144 plhs[0] =
MxArray(obj->getNoiseSigma());
146 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
148 else if (method ==
"set") {
150 string prop(rhs[2].toString());
151 if (prop ==
"BackgroundRatio")
152 obj->setBackgroundRatio(rhs[3].toDouble());
153 else if (prop ==
"History")
154 obj->setHistory(rhs[3].toInt());
155 else if (prop ==
"NMixtures")
156 obj->setNMixtures(rhs[3].toInt());
157 else if (prop ==
"NoiseSigma")
158 obj->setNoiseSigma(rhs[3].toDouble());
160 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized property");
163 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.