1 #ifndef RELIABILITYMOG2_H 2 #define RELIABILITYMOG2_H 4 #include <opencv2/opencv.hpp> 23 static const int defaultHistory2 = 500;
24 static const float defaultVarThreshold2 = 4.0f*4.0f;
25 static const int defaultNMixtures2 = 5;
26 static const float defaultBackgroundRatio2 = 0.9f;
27 static const float defaultVarThresholdGen2 = 3.0f*3.0f;
28 static const float defaultVarInit2 = 15.0f;
29 static const float defaultVarMax2 = 5*defaultVarInit2;
30 static const float defaultVarMin2 = 4.0f;
33 static const float defaultfCT2 = 0.05f;
34 static const unsigned char defaultnShadowDetection2 = (
unsigned char)127;
35 static const float defaultfTau = 0.5f;
45 ReliabilityMog2(
int _history,
float _varThreshold,
bool _bShadowDetection=
true);
51 void apply(InputArray image, OutputArray fgmask,
double learningRate=-1);
52 void apply(InputArray image, OutputArray fgmask, OutputArray featureMap,
double learningRate=-1);
55 virtual void getBackgroundImage(OutputArray backgroundImage)
const;
58 void initialize(Size _frameSize,
int _frameType);
60 virtual int getHistory()
const {
return history; }
61 virtual void setHistory(
int _nframes) { history = _nframes; }
63 virtual int getNMixtures()
const {
return nmixtures; }
64 virtual void setNMixtures(
int nmix) { nmixtures = nmix; }
66 virtual double getBackgroundRatio()
const {
return backgroundRatio; }
67 virtual void setBackgroundRatio(
double _backgroundRatio) { backgroundRatio = (float)_backgroundRatio; }
69 virtual double getVarThreshold()
const {
return varThreshold; }
70 virtual void setVarThreshold(
double _varThreshold) { varThreshold = _varThreshold; }
72 virtual double getVarThresholdGen()
const {
return varThresholdGen; }
73 virtual void setVarThresholdGen(
double _varThresholdGen) { varThresholdGen = (float)_varThresholdGen; }
75 virtual double getVarInit()
const {
return fVarInit; }
76 virtual void setVarInit(
double varInit) { fVarInit = (float)varInit; }
78 virtual double getVarMin()
const {
return fVarMin; }
79 virtual void setVarMin(
double varMin) { fVarMin = (float)varMin; }
81 virtual double getVarMax()
const {
return fVarMax; }
82 virtual void setVarMax(
double varMax) { fVarMax = (float)varMax; }
84 virtual double getComplexityReductionThreshold()
const {
return fCT; }
85 virtual void setComplexityReductionThreshold(
double ct) { fCT = (float)ct; }
87 virtual bool getDetectShadows()
const {
return bShadowDetection; }
88 virtual void setDetectShadows(
bool detectshadows)
90 if ((bShadowDetection && detectshadows) || (!bShadowDetection && !detectshadows))
92 bShadowDetection = detectshadows;
95 virtual int getShadowValue()
const {
return nShadowDetection; }
96 virtual void setShadowValue(
int value) { nShadowDetection = (uchar)value; }
98 virtual double getShadowThreshold()
const {
return fTau; }
99 virtual void setShadowThreshold(
double value) { fTau = (float)value; }
101 virtual void write(FileStorage& fs)
const 103 fs <<
"name" << name_
104 <<
"history" << history
105 <<
"nmixtures" << nmixtures
106 <<
"backgroundRatio" << backgroundRatio
107 <<
"varThreshold" << varThreshold
108 <<
"varThresholdGen" << varThresholdGen
109 <<
"varInit" << fVarInit
110 <<
"varMin" << fVarMin
111 <<
"varMax" << fVarMax
112 <<
"complexityReductionThreshold" << fCT
113 <<
"detectShadows" << (int)bShadowDetection
114 <<
"shadowValue" << (
int)nShadowDetection
115 <<
"shadowThreshold" << fTau;
118 virtual void read(
const FileNode& fn)
120 CV_Assert( (String)fn[
"name"] == name_ );
121 history = (int)fn[
"history"];
122 nmixtures = (int)fn[
"nmixtures"];
123 backgroundRatio = (float)fn[
"backgroundRatio"];
124 varThreshold = (double)fn[
"varThreshold"];
125 varThresholdGen = (float)fn[
"varThresholdGen"];
126 fVarInit = (float)fn[
"varInit"];
127 fVarMin = (float)fn[
"varMin"];
128 fVarMax = (float)fn[
"varMax"];
129 fCT = (float)fn[
"complexityReductionThreshold"];
130 bShadowDetection = (int)fn[
"detectShadows"] != 0;
131 nShadowDetection = saturate_cast<uchar>((int)fn[
"shadowValue"]);
132 fTau = (float)fn[
"shadowThreshold"];
139 Mat bgmodelUsedModes;
143 mutable bool opencl_ON;
148 UMat u_bgmodelUsedModes;
164 float backgroundRatio;
171 float varThresholdGen;
191 bool bShadowDetection;
192 unsigned char nShadowDetection;
201 bool ocl_getBackgroundImage(OutputArray backgroundImage)
const;
202 bool ocl_apply(InputArray _image, OutputArray _fgmask,
double learningRate=-1);
203 void create_ocl_apply_kernel();
261 bool bShadowDetection;
262 unsigned char nShadowDetection;
280 detectShadowGMM(
const float*
data,
int nchannels,
int nmodes,
281 const GMM* gmm,
const float* mean,
282 float Tb,
float TB,
float tau)
287 for(
int mode = 0; mode < nmodes; mode++, mean += nchannels )
291 float numerator = 0.0f;
292 float denominator = 0.0f;
293 for(
int c = 0; c < nchannels; c++ )
295 numerator +=
data[c] * mean[c];
296 denominator += mean[c] * mean[c];
300 if( denominator == 0 )
304 if( numerator <= denominator && numerator >= tau*denominator )
306 float a = numerator / denominator;
309 for(
int c = 0; c < nchannels; c++ )
311 float dD= a*mean[c] -
data[c];
315 if (dist2a < Tb*g.variance*a*a)
343 MOG2Invoker(
const Mat& _src, Mat& _dst, Mat& _featureMap,
344 GMM* _gmm,
float* _mean,
346 int _nmixtures,
float _alphaT,
347 float _Tb,
float _TB,
float _Tg,
348 float _varInit,
float _varMin,
float _varMax,
349 float _prune,
float _tau,
bool _detectShadows,
354 featureMap = &_featureMap;
357 modesUsed0 = _modesUsed;
358 nmixtures = _nmixtures;
364 varMin = MIN(_varMin, _varMax);
365 varMax = MAX(_varMin, _varMax);
368 detectShadows = _detectShadows;
369 shadowVal = _shadowVal;
374 int y0 = range.start, y1 = range.end;
375 int ncols = src->cols, nchannels = src->channels();
376 AutoBuffer<float> buf(src->cols*nchannels);
377 float alpha1 = 1.f - alphaT;
378 float dData[CV_CN_MAX];
381 for(
int y = y0; y < y1; y++ )
383 const float*
data = buf;
384 if( src->depth() != CV_32F )
385 src->row(y).convertTo(Mat(1, ncols, CV_32FC(nchannels), (
void*)data), CV_32F);
387 data = src->ptr<
float>(y);
389 float* mean = mean0 + ncols*nmixtures*nchannels*y;
390 GMM* gmm = gmm0 + ncols*nmixtures*y;
391 uchar* modesUsed = modesUsed0 + ncols*y;
392 uchar* mask = dst->ptr(y);
393 uchar* ptrFeatureMap = featureMap->ptr(y);
395 for(
int x = 0; x < ncols; x++, data += nchannels, gmm += nmixtures, mean += nmixtures*nchannels )
399 bool background =
false;
402 bool fitsPDF =
false;
403 int nmodes = modesUsed[x], nNewModes = nmodes;
404 float totalWeight = 0.f;
406 float* mean_m = mean;
411 for(
int mode = 0; mode < nmodes; mode++, mean_m += nchannels )
413 float weight = alpha1*gmm[mode].weight + prune;
420 float var = gmm[mode].variance;
427 dData[0] = mean_m[0] - data[0];
428 dData[1] = mean_m[1] - data[1];
429 dData[2] = mean_m[2] - data[2];
430 dist2 = dData[0]*dData[0] + dData[1]*dData[1] + dData[2]*dData[2];
435 for(
int c = 0; c < nchannels; c++ )
437 dData[c] = mean_m[c] - data[c];
438 dist2 += dData[c]*dData[c];
442 if( totalWeight < TB && dist2 < featureVal)
448 if( totalWeight < TB && dist2 < Tb*var )
470 float k = alphaT/weight;
473 for(
int c = 0; c < nchannels; c++ )
474 mean_m[c] -= k*dData[c];
477 float varnew = var + k*(dist2-var);
479 varnew = MAX(varnew, varMin);
480 varnew = MIN(varnew, varMax);
481 gmm[mode].variance = varnew;
486 for(
int i = mode; i > 0; i-- )
489 if( weight < gmm[i-1].weight )
494 std::swap(gmm[i], gmm[i-1]);
495 for(
int c = 0; c < nchannels; c++ )
496 std::swap(mean[i*nchannels + c], mean[(i-1)*nchannels + c]);
504 if( weight < -prune )
510 gmm[mode-swap_count].weight = weight;
511 totalWeight += weight;
517 totalWeight = 1.f/totalWeight;
518 for(
int mode = 0; mode < nmodes; mode++ )
520 gmm[mode].weight *= totalWeight;
526 if( !fitsPDF && alphaT > 0.f )
529 int mode = nmodes == nmixtures ? nmixtures-1 : nmodes++;
532 gmm[mode].weight = 1.f;
535 gmm[mode].weight = alphaT;
538 for(
int i = 0; i < nmodes-1; i++ )
539 gmm[i].weight *= alpha1;
543 for(
int c = 0; c < nchannels; c++ )
544 mean[mode*nchannels + c] = data[c];
546 gmm[mode].variance = varInit;
550 for(
int i = nmodes - 1; i > 0; i-- )
553 if( alphaT < gmm[i-1].weight )
557 std::swap(gmm[i], gmm[i-1]);
558 for(
int c = 0; c < nchannels; c++ )
559 std::swap(mean[i*nchannels + c], mean[(i-1)*nchannels + c]);
564 modesUsed[x] = uchar(nmodes);
565 mask[x] = background ? 0 :
566 detectShadows && detectShadowGMM(data, nchannels, nmodes, gmm, mean, Tb, TB, tau) ?
568 if(featureVal == 100000)
569 ptrFeatureMap[x] = 0;
571 ptrFeatureMap[x] = featureVal < 255? featureVal: 255;
584 float alphaT, Tb, TB, Tg;
585 float varInit, varMin, varMax, prune, tau;
599 #endif // RELIABILITYMOG2_H double varThreshold
Definition: ReliabilityMog2.h:155
Definition: ReliabilityMog2.h:206
void operator()(const Range &range) const
Definition: ReliabilityMog2.h:372
Definition: QtOpencvConversion.h:19
~ReliabilityMog2()
the destructor
Definition: ReliabilityMog2.h:48
Definition: ReliabilityMog2.h:270
Definition: ReliabilityMog2.h:340
Definition: ReliabilityMog2.h:37
Definition: BackgroundRecLigth.h:20