Added computing of the mean background image in the BackgroundSubtractorMOG2 model.

This commit is contained in:
Ilya Lysenkov 2011-06-03 14:10:12 +00:00
parent 6be2a79fb9
commit 04ebfc0a02
2 changed files with 60 additions and 0 deletions

View 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;

View 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. */