From 0a1767a6b5d6755871245c66e6a9091e3adde1fb Mon Sep 17 00:00:00 2001 From: Matthias Grundmann Date: Wed, 8 Mar 2017 19:27:43 -0800 Subject: [PATCH 1/3] Update bgfg_gaussmix2.cpp to avoid divide by zero cases. --- modules/video/src/bgfg_gaussmix2.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/video/src/bgfg_gaussmix2.cpp b/modules/video/src/bgfg_gaussmix2.cpp index ebe449825c..3ee57086a9 100644 --- a/modules/video/src/bgfg_gaussmix2.cpp +++ b/modules/video/src/bgfg_gaussmix2.cpp @@ -693,11 +693,16 @@ public: //go through all modes ////// - //renormalize weights - totalWeight = 1.f/totalWeight; + // Renormalize weights. In the special case that the pixel does + // not agree with any modes, set weights to zero (a new mode will be added below). + float invWeight = 0.f; + if (fabs(totalWeight) > 1e-12f) { + invWeight = 1.f/totalWeight; + } + for( int mode = 0; mode < nmodes; mode++ ) { - gmm[mode].weight *= totalWeight; + gmm[mode].weight *= invWeight; } //make new mode if needed and exit @@ -900,7 +905,10 @@ void BackgroundSubtractorMOG2Impl::getBackgroundImage_intern(OutputArray backgro if(totalWeight > backgroundRatio) break; } - float invWeight = 1.f/totalWeight; + float invWeight = 0.f; + if (fabs(totalWeight) > 1e-12f) { + invWeight = 1.f/totalWeight; + } meanBackground.at >(row, col) = Vec(meanVal * invWeight); meanVal = 0.f; From 7295dd7dece3e4deb255962817ec141b98c62353 Mon Sep 17 00:00:00 2001 From: Matthias Grundmann Date: Tue, 14 Mar 2017 16:57:50 -0700 Subject: [PATCH 2/3] Update bgfg_gaussmix2.cpp 1e-12 -> DBL_EPSILON --- modules/video/src/bgfg_gaussmix2.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/video/src/bgfg_gaussmix2.cpp b/modules/video/src/bgfg_gaussmix2.cpp index 3ee57086a9..ad81c55eae 100644 --- a/modules/video/src/bgfg_gaussmix2.cpp +++ b/modules/video/src/bgfg_gaussmix2.cpp @@ -696,7 +696,7 @@ public: // Renormalize weights. In the special case that the pixel does // not agree with any modes, set weights to zero (a new mode will be added below). float invWeight = 0.f; - if (fabs(totalWeight) > 1e-12f) { + if (fabs(totalWeight) > DBL_EPSILON) { invWeight = 1.f/totalWeight; } @@ -906,7 +906,7 @@ void BackgroundSubtractorMOG2Impl::getBackgroundImage_intern(OutputArray backgro break; } float invWeight = 0.f; - if (fabs(totalWeight) > 1e-12f) { + if (fabs(totalWeight) > DBL_EPSILON) { invWeight = 1.f/totalWeight; } From 13540bf7f4d91dacf12d32d6c0864ea180593468 Mon Sep 17 00:00:00 2001 From: Matthias Grundmann Date: Fri, 24 Mar 2017 13:26:32 -0700 Subject: [PATCH 3/3] Update bgfg_gaussmix2.cpp Addressed comments. --- modules/video/src/bgfg_gaussmix2.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/video/src/bgfg_gaussmix2.cpp b/modules/video/src/bgfg_gaussmix2.cpp index ad81c55eae..977a2581f3 100644 --- a/modules/video/src/bgfg_gaussmix2.cpp +++ b/modules/video/src/bgfg_gaussmix2.cpp @@ -696,7 +696,7 @@ public: // Renormalize weights. In the special case that the pixel does // not agree with any modes, set weights to zero (a new mode will be added below). float invWeight = 0.f; - if (fabs(totalWeight) > DBL_EPSILON) { + if (std::abs(totalWeight) > FLT_EPSILON) { invWeight = 1.f/totalWeight; } @@ -906,7 +906,7 @@ void BackgroundSubtractorMOG2Impl::getBackgroundImage_intern(OutputArray backgro break; } float invWeight = 0.f; - if (fabs(totalWeight) > DBL_EPSILON) { + if (std::abs(totalWeight) > FLT_EPSILON) { invWeight = 1.f/totalWeight; }