Commit 99029564 authored by Firat Kalaycilar's avatar Firat Kalaycilar

made a performance improvement.

changed the way the mean value for each pixel is assigned in the output image.
parent 9eb0e7d9
...@@ -582,12 +582,12 @@ void BackgroundSubtractorMOG2::getBackgroundImage(OutputArray backgroundImage) c ...@@ -582,12 +582,12 @@ void BackgroundSubtractorMOG2::getBackgroundImage(OutputArray backgroundImage) c
int firstGaussianIdx = 0; int firstGaussianIdx = 0;
const GMM* gmm = (GMM*)bgmodel.data; const GMM* gmm = (GMM*)bgmodel.data;
const float* mean = reinterpret_cast<const float*>(gmm + frameSize.width*frameSize.height*nmixtures); const float* mean = reinterpret_cast<const float*>(gmm + frameSize.width*frameSize.height*nmixtures);
std::vector<float> meanVal(nchannels, 0.f);
for(int row=0; row<meanBackground.rows; row++) for(int row=0; row<meanBackground.rows; row++)
{ {
for(int col=0; col<meanBackground.cols; col++) for(int col=0; col<meanBackground.cols; col++)
{ {
int nmodes = bgmodelUsedModes.at<uchar>(row, col); int nmodes = bgmodelUsedModes.at<uchar>(row, col);
std::vector<float> meanVal(nchannels, 0.f);
float totalWeight = 0.f; float totalWeight = 0.f;
for(int gaussianIdx = firstGaussianIdx; gaussianIdx < firstGaussianIdx + nmodes; gaussianIdx++) for(int gaussianIdx = firstGaussianIdx; gaussianIdx < firstGaussianIdx + nmodes; gaussianIdx++)
{ {
...@@ -603,17 +603,16 @@ void BackgroundSubtractorMOG2::getBackgroundImage(OutputArray backgroundImage) c ...@@ -603,17 +603,16 @@ void BackgroundSubtractorMOG2::getBackgroundImage(OutputArray backgroundImage) c
break; break;
} }
float invWeight = 1.f/totalWeight; float invWeight = 1.f/totalWeight;
for(int chn = 0; chn < nchannels; chn++)
{
meanVal[chn] *= invWeight;
}
switch(nchannels) switch(nchannels)
{ {
case 1: case 1:
meanBackground.at<uchar>(row, col) = (uchar)meanVal[0]; meanBackground.at<uchar>(row, col) = (uchar)(meanVal[0] * invWeight);
meanVal[0] = 0.f;
break; break;
case 3: case 3:
meanBackground.at<Vec3b>(row, col) = Vec3b(*reinterpret_cast<Vec3f*>(&meanVal[0])); Vec3f& meanVec = *reinterpret_cast<Vec3f*>(&meanVal[0]);
meanBackground.at<Vec3b>(row, col) = Vec3b(meanVec * invWeight);
meanVec = 0.f;
break; break;
} }
firstGaussianIdx += nmixtures; firstGaussianIdx += nmixtures;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment