Added computing of the mean background image in the BackgroundSubtractorMOG2 model.
authorIlya Lysenkov <no@email>
Fri, 3 Jun 2011 14:10:12 +0000 (14:10 +0000)
committerIlya Lysenkov <no@email>
Fri, 3 Jun 2011 14:10:12 +0000 (14:10 +0000)
modules/video/src/bgfg_gaussmix.cpp
modules/video/src/bgfg_gaussmix2.cpp

index 9a5426a..ce0300e 100644 (file)
@@ -63,6 +63,10 @@ void BackgroundSubtractor::operator()(const InputArray&, OutputArray, double)
 {
 }
 
+void BackgroundSubtractor::getBackgroundImage(OutputArray) const
+{
+}
+
 static const int defaultNMixtures = CV_BGFG_MOG_NGAUSSIANS;
 static const int defaultHistory = CV_BGFG_MOG_WINDOW_SIZE;
 static const double defaultBackgroundRatio = CV_BGFG_MOG_BACKGROUND_THRESHOLD;
index 655db00..936d965 100644 (file)
@@ -1291,6 +1291,62 @@ void BackgroundSubtractorMOG2::operator()(const InputArray& _image, OutputArray
                float(learningRate));
 }
 
+void BackgroundSubtractorMOG2::getBackgroundImage(OutputArray backgroundImage) const
+{
+    CV_Assert(CV_BGFG_MOG2_NDMAX == 3);
+    Mat meanBackground(frameSize, CV_8UC3, Scalar::all(0));
+
+    int firstGaussianIdx = 0;
+    CvPBGMMGaussian* pGMM = (CvPBGMMGaussian*)bgmodel.data;
+    for(int row=0; row<meanBackground.rows; row++)
+    {
+        for(int col=0; col<meanBackground.cols; col++)
+        {
+            int nModes = static_cast<int>(bgmodelUsedModes.at<uchar>(row, col));
+            double meanVal[CV_BGFG_MOG2_NDMAX] = {0.0, 0.0, 0.0};
+
+            double totalWeight = 0.0;
+            for(int gaussianIdx = firstGaussianIdx; gaussianIdx < firstGaussianIdx + nModes; gaussianIdx++)
+            {
+                CvPBGMMGaussian gaussian = pGMM[gaussianIdx];
+                totalWeight += gaussian.weight;
+
+                for(int chIdx = 0; chIdx < CV_BGFG_MOG2_NDMAX; chIdx++)
+                {
+                    meanVal[chIdx] += gaussian.weight * gaussian.mean[chIdx];
+                }
+
+                if(totalWeight > backgroundRatio)
+                    break;
+            }
+
+            Vec3f val = Vec3f(meanVal[0], meanVal[1], meanVal[2]) * (1.0 / totalWeight);
+            meanBackground.at<Vec3b>(row, col) = Vec3b(val);
+            firstGaussianIdx += nmixtures;
+        }
+    }
+
+    switch(CV_MAT_CN(frameType))
+    {
+        case 1:
+        {
+            vector<Mat> channels;
+            split(meanBackground, channels);
+            channels[0].copyTo(backgroundImage);
+            break;
+        }
+
+        case 3:
+        {
+            meanBackground.copyTo(backgroundImage);
+            break;
+        }
+
+        default:
+            CV_Assert(false);
+    }
+}
+
 }
 
 /* End of file. */