Skip to content

Commit 2409924

Browse files
committed
Merge pull request opencv#8342 from grundman:patch-1
2 parents 8bfc384 + 13540bf commit 2409924

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

modules/video/src/bgfg_gaussmix2.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,16 @@ class MOG2Invoker : public ParallelLoopBody
693693
//go through all modes
694694
//////
695695

696-
//renormalize weights
697-
totalWeight = 1.f/totalWeight;
696+
// Renormalize weights. In the special case that the pixel does
697+
// not agree with any modes, set weights to zero (a new mode will be added below).
698+
float invWeight = 0.f;
699+
if (std::abs(totalWeight) > FLT_EPSILON) {
700+
invWeight = 1.f/totalWeight;
701+
}
702+
698703
for( int mode = 0; mode < nmodes; mode++ )
699704
{
700-
gmm[mode].weight *= totalWeight;
705+
gmm[mode].weight *= invWeight;
701706
}
702707

703708
//make new mode if needed and exit
@@ -900,7 +905,10 @@ void BackgroundSubtractorMOG2Impl::getBackgroundImage_intern(OutputArray backgro
900905
if(totalWeight > backgroundRatio)
901906
break;
902907
}
903-
float invWeight = 1.f/totalWeight;
908+
float invWeight = 0.f;
909+
if (std::abs(totalWeight) > FLT_EPSILON) {
910+
invWeight = 1.f/totalWeight;
911+
}
904912

905913
meanBackground.at<Vec<T,CN> >(row, col) = Vec<T,CN>(meanVal * invWeight);
906914
meanVal = 0.f;

0 commit comments

Comments
 (0)