From 5d36dfd283d60dc6fcda4b97adcce7153f36c27f Mon Sep 17 00:00:00 2001 From: zhuxc Date: Mon, 11 Dec 2023 18:49:14 +0800 Subject: [PATCH 1/3] fix: fix opencv_contrib issue #2941 --- modules/cudabgsegm/src/mog2.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/cudabgsegm/src/mog2.cpp b/modules/cudabgsegm/src/mog2.cpp index 47135a088ba..719317014ab 100644 --- a/modules/cudabgsegm/src/mog2.cpp +++ b/modules/cudabgsegm/src/mog2.cpp @@ -105,10 +105,21 @@ class MOG2Impl CV_FINAL : public cuda::BackgroundSubtractorMOG2 void setVarInit(double varInit) CV_OVERRIDE { constantsHost_.varInit_ = (float)varInit; } double getVarMin() const CV_OVERRIDE { return constantsHost_.varMin_; } - void setVarMin(double varMin) CV_OVERRIDE { constantsHost_.varMin_ = ::fminf((float)varMin, constantsHost_.varMax_); } - + void setVarMin(double varMin) CV_OVERRIDE { + if (nframes_ == 0) { + constantsHost_.varMin_ = (float)varMin; + } else { + constantsHost_.varMin_ = ::fminf((float)varMin, constantsHost_.varMax_); + } + } double getVarMax() const CV_OVERRIDE { return constantsHost_.varMax_; } - void setVarMax(double varMax) CV_OVERRIDE { constantsHost_.varMax_ = ::fmaxf(constantsHost_.varMin_, (float)varMax); } + void setVarMax(double varMax) CV_OVERRIDE { + if (nframes_ == 0) { + constantsHost_.varMax_ = (float)varMax; + } else { + constantsHost_.varMax_ = ::fmaxf(constantsHost_.varMin_, (float)varMax); + } + } double getComplexityReductionThreshold() const CV_OVERRIDE { return ct_; } void setComplexityReductionThreshold(double ct) CV_OVERRIDE { ct_ = (float)ct; } From a6ecee1a1db4962f9df2e9b5bcc59e00e279eb2c Mon Sep 17 00:00:00 2001 From: zhuxc Date: Tue, 19 Dec 2023 18:51:08 +0800 Subject: [PATCH 2/3] "fix: fix trailing whitespace" --- modules/cudabgsegm/src/mog2.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/cudabgsegm/src/mog2.cpp b/modules/cudabgsegm/src/mog2.cpp index 719317014ab..12a607ecb29 100644 --- a/modules/cudabgsegm/src/mog2.cpp +++ b/modules/cudabgsegm/src/mog2.cpp @@ -113,9 +113,9 @@ class MOG2Impl CV_FINAL : public cuda::BackgroundSubtractorMOG2 } } double getVarMax() const CV_OVERRIDE { return constantsHost_.varMax_; } - void setVarMax(double varMax) CV_OVERRIDE { + void setVarMax(double varMax) CV_OVERRIDE { if (nframes_ == 0) { - constantsHost_.varMax_ = (float)varMax; + constantsHost_.varMax_ = (float)varMax; } else { constantsHost_.varMax_ = ::fmaxf(constantsHost_.varMin_, (float)varMax); } From ecc828d80c9ad65ccf54402987f6bc04b3e973b1 Mon Sep 17 00:00:00 2001 From: zhuxc Date: Fri, 19 Apr 2024 10:53:49 +0800 Subject: [PATCH 3/3] fix: compare varMin_ and varMax_ in initialize method --- modules/cudabgsegm/src/mog2.cpp | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/modules/cudabgsegm/src/mog2.cpp b/modules/cudabgsegm/src/mog2.cpp index 12a607ecb29..6d98796e229 100644 --- a/modules/cudabgsegm/src/mog2.cpp +++ b/modules/cudabgsegm/src/mog2.cpp @@ -105,21 +105,9 @@ class MOG2Impl CV_FINAL : public cuda::BackgroundSubtractorMOG2 void setVarInit(double varInit) CV_OVERRIDE { constantsHost_.varInit_ = (float)varInit; } double getVarMin() const CV_OVERRIDE { return constantsHost_.varMin_; } - void setVarMin(double varMin) CV_OVERRIDE { - if (nframes_ == 0) { - constantsHost_.varMin_ = (float)varMin; - } else { - constantsHost_.varMin_ = ::fminf((float)varMin, constantsHost_.varMax_); - } - } + void setVarMin(double varMin) CV_OVERRIDE { constantsHost_.varMin_ = (float)varMin; } double getVarMax() const CV_OVERRIDE { return constantsHost_.varMax_; } - void setVarMax(double varMax) CV_OVERRIDE { - if (nframes_ == 0) { - constantsHost_.varMax_ = (float)varMax; - } else { - constantsHost_.varMax_ = ::fmaxf(constantsHost_.varMin_, (float)varMax); - } - } + void setVarMax(double varMax) CV_OVERRIDE { constantsHost_.varMax_ = (float)varMax; } double getComplexityReductionThreshold() const CV_OVERRIDE { return ct_; } void setComplexityReductionThreshold(double ct) CV_OVERRIDE { ct_ = (float)ct; } @@ -250,6 +238,11 @@ void MOG2Impl::initialize(cv::Size frameSize, int frameType, Stream &stream) bgmodelUsedModes_.create(frameSize_, CV_8UC1); bgmodelUsedModes_.setTo(Scalar::all(0)); + float real_varMin = ::fminf(constantsHost_.varMin_, constantsHost_.varMax_); + float real_varMax = ::fmaxf(constantsHost_.varMin_, constantsHost_.varMax_); + constantsHost_.varMin_ = real_varMin; + constantsHost_.varMax_ = real_varMax; + cudaSafeCall(cudaMemcpyAsync(constantsDevice_, &constantsHost_, sizeof(Constants), cudaMemcpyHostToDevice, StreamAccessor::getStream(stream))); } } // namespace