From b6620189a5da068e1ac913759ad3ab284a82d40e Mon Sep 17 00:00:00 2001 From: Roaim Date: Fri, 10 Oct 2025 01:54:20 +0500 Subject: [PATCH 1/3] Fix: preserve _min_disp in DisparityWLSFilterImpl init() --- modules/ximgproc/src/disparity_filters.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/ximgproc/src/disparity_filters.cpp b/modules/ximgproc/src/disparity_filters.cpp index 03a2691df4c..505fde6b8d1 100644 --- a/modules/ximgproc/src/disparity_filters.cpp +++ b/modules/ximgproc/src/disparity_filters.cpp @@ -147,7 +147,6 @@ void DisparityWLSFilterImpl::init(double _lambda, double _sigma_color, bool _use min_disp = _min_disp; valid_disp_ROI = Rect(); right_view_valid_disp_ROI = Rect(); - min_disp=0; lambda = _lambda; sigma_color = _sigma_color; use_confidence = _use_confidence; From 06fc7ad743ac299a816b9cc408578c3145573c6f Mon Sep 17 00:00:00 2001 From: pratham-mcw Date: Mon, 13 Oct 2025 14:03:42 +0530 Subject: [PATCH 2/3] Merge pull request #3980 from pratham-mcw:opt-arm64-adaptive-manifold-unroll ximgproc: optimize Adaptive Manifold function for ARM64 #3980 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - This PR introduces an ARM64-specific performance optimization in AdaptiveManifoldFilter::h_filter by applying loop unrolling. - The optimization is guarded with #if defined(_M_ARM64) to ensure it only affects ARM64 builds. - The optimization does not affect accuracy and maintains the same numerical behavior as the original scalar implementation. Performance Improvements : - The optimization significantly improves the performance of adaptive Manifold function on Windows ARM64 targets. - The table below shows timing comparisons before and after the optimization: image --- .../src/adaptive_manifold_filter_n.cpp | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/ximgproc/src/adaptive_manifold_filter_n.cpp b/modules/ximgproc/src/adaptive_manifold_filter_n.cpp index 3aa58cafe73..14d49adbf9c 100644 --- a/modules/ximgproc/src/adaptive_manifold_filter_n.cpp +++ b/modules/ximgproc/src/adaptive_manifold_filter_n.cpp @@ -520,11 +520,29 @@ void AdaptiveManifoldFilterN::h_filter(const Mat1f& src, Mat& dst, float sigma) float* dst_row = dst.ptr(y); dst_row[0] = src_row[0]; - for (int x = 1; x < src.cols; ++x) + int x = 1; + #if CV_ENABLE_UNROLLED && defined(_M_ARM64) + for ( ; x + 1 < src.cols; x += 2 ) { dst_row[x] = src_row[x] + a * (dst_row[x - 1] - src_row[x]); + dst_row[x + 1] = src_row[x + 1] + a * (dst_row[x] - src_row[x + 1]); } - for (int x = src.cols - 2; x >= 0; --x) + #endif + for ( ; x < src.cols; ++x ) + { + dst_row[x] = src_row[x] + a * (dst_row[x - 1] - src_row[x]); + } + + x = src.cols - 2; + + #if CV_ENABLE_UNROLLED && defined(_M_ARM64) + for ( ; x - 1 >= 0; x -= 2 ) + { + dst_row[x] = dst_row[x] + a * (dst_row[x + 1] - dst_row[x]); + dst_row[x - 1] = dst_row[x - 1] + a * (dst_row[x] - dst_row[x - 1]); + } + #endif + for ( ; x >= 0; --x ) { dst_row[x] = dst_row[x] + a * (dst_row[x + 1] - dst_row[x]); } From c4c74c435be8857a559810f7289cf9579f53649b Mon Sep 17 00:00:00 2001 From: Dave Merchant <131936113+D00E@users.noreply.github.com> Date: Tue, 14 Oct 2025 07:59:58 +0100 Subject: [PATCH 3/3] Merge pull request #4017 from D00E:known-foreground-mask In support of Optional Known Foreground Mask for Background Subtractors #4017 Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request I agree to contribute to the project under Apache 2 License. x To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV x The PR is proposed to the proper branch x There is a reference to the original bug report and related work There is accuracy test, performance test and test data in opencv_extra repository, if applicable x Patch to opencv_extra has the same branch name. The feature is well documented and sample code can be built with the project CMake Description Is in support to a pull request regarding, issue https://github.com/opencv/opencv/issues/26476. Core paired pull request: https://github.com/opencv/opencv/pull/27810 This adds function templates inline with the pure virtual overloaded apply function present for background subtraction. Supporting the build of the pull request: --- modules/bgsegm/include/opencv2/bgsegm.hpp | 60 +++++++++++++++++++ modules/bgsegm/src/bgfg_gaussmix.cpp | 12 ++++ modules/bgsegm/src/bgfg_gmg.cpp | 11 ++++ modules/bgsegm/src/bgfg_gsoc.cpp | 21 +++++++ modules/bgsegm/src/bgfg_subcnt.cpp | 11 ++++ .../cudabgsegm/include/opencv2/cudabgsegm.hpp | 4 ++ modules/cudabgsegm/src/mog.cpp | 20 +++++++ modules/cudabgsegm/src/mog2.cpp | 20 +++++++ .../cudalegacy/include/opencv2/cudalegacy.hpp | 1 + modules/cudalegacy/src/fgd.cpp | 10 ++++ modules/cudalegacy/src/gmg.cpp | 21 +++++++ 11 files changed, 191 insertions(+) diff --git a/modules/bgsegm/include/opencv2/bgsegm.hpp b/modules/bgsegm/include/opencv2/bgsegm.hpp index 1d2b6f892f2..6c894f8fb2f 100644 --- a/modules/bgsegm/include/opencv2/bgsegm.hpp +++ b/modules/bgsegm/include/opencv2/bgsegm.hpp @@ -62,6 +62,32 @@ The class implements the algorithm described in @cite KB2001 . class CV_EXPORTS_W BackgroundSubtractorMOG : public BackgroundSubtractor { public: + // BackgroundSubtractor interface + /** @brief Computes a foreground mask. + + @param image Next video frame of type CV_8UC(n),CV_8SC(n),CV_16UC(n),CV_16SC(n),CV_32SC(n),CV_32FC(n),CV_64FC(n), where n is 1,2,3,4. + @param fgmask The output foreground mask as an 8-bit binary image. + @param learningRate The value between 0 and 1 that indicates how fast the background model is + learnt. Negative parameter value makes the algorithm to use some automatically chosen learning + rate. 0 means that the background model is not updated at all, 1 means that the background model + is completely reinitialized from the last frame. + */ + + CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; + + /** @brief Computes a foreground mask and skips known foreground in evaluation. + + @param image Next video frame of type CV_8UC(n),CV_8SC(n),CV_16UC(n),CV_16SC(n),CV_32SC(n),CV_32FC(n),CV_64FC(n), where n is 1,2,3,4. + @param fgmask The output foreground mask as an 8-bit binary image. + @param knownForegroundMask The mask for inputting already known foreground, allows model to ignore learning known pixels. + @param learningRate The value between 0 and 1 that indicates how fast the background model is + learnt. Negative parameter value makes the algorithm to use some automatically chosen learning + rate. 0 means that the background model is not updated at all, 1 means that the background model + is completely reinitialized from the last frame. + */ + + CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; + CV_WRAP virtual int getHistory() const = 0; CV_WRAP virtual void setHistory(int nframes) = 0; @@ -110,6 +136,22 @@ class CV_EXPORTS_W BackgroundSubtractorGMG : public BackgroundSubtractor is completely reinitialized from the last frame. */ CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; + + /** @brief Computes a foreground mask with known foreground mask input. + + @param image Next video frame. + @param fgmask The output foreground mask as an 8-bit binary image. + @param knownForegroundMask The mask for inputting already known foreground. + @param learningRate The value between 0 and 1 that indicates how fast the background model is + learnt. Negative parameter value makes the algorithm to use some automatically chosen learning + rate. 0 means that the background model is not updated at all, 1 means that the background model + is completely reinitialized from the last frame. + + @note This method has a default virtual implementation that throws a "not impemented" error. + Foreground masking may not be supported by all background subtractors. + */ + CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; + CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0; /** @brief Returns total number of distinct colors to maintain in histogram. @@ -210,6 +252,22 @@ class CV_EXPORTS_W BackgroundSubtractorCNT : public BackgroundSubtractor public: // BackgroundSubtractor interface CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; + + /** @brief Computes a foreground mask with known foreground mask input. + + @param image Next video frame. + @param knownForegroundMask The mask for inputting already known foreground. + @param fgmask The output foreground mask as an 8-bit binary image. + @param learningRate The value between 0 and 1 that indicates how fast the background model is + learnt. Negative parameter value makes the algorithm to use some automatically chosen learning + rate. 0 means that the background model is not updated at all, 1 means that the background model + is completely reinitialized from the last frame. + + @note This method has a default virtual implementation that throws a "not impemented" error. + Foreground masking may not be supported by all background subtractors. + */ + CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; + CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0; /** @brief Returns number of frames with same pixel color to consider stable. @@ -269,6 +327,7 @@ class CV_EXPORTS_W BackgroundSubtractorGSOC : public BackgroundSubtractor public: // BackgroundSubtractor interface CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; + CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0; }; @@ -280,6 +339,7 @@ class CV_EXPORTS_W BackgroundSubtractorLSBP : public BackgroundSubtractor public: // BackgroundSubtractor interface CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; + CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0; CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0; }; diff --git a/modules/bgsegm/src/bgfg_gaussmix.cpp b/modules/bgsegm/src/bgfg_gaussmix.cpp index d6c167689db..6fafe999e71 100644 --- a/modules/bgsegm/src/bgfg_gaussmix.cpp +++ b/modules/bgsegm/src/bgfg_gaussmix.cpp @@ -42,6 +42,7 @@ #include "precomp.hpp" #include +#include "opencv2/core/utils/logger.hpp" // to make sure we can use these short names #undef K @@ -104,6 +105,8 @@ class BackgroundSubtractorMOGImpl CV_FINAL : public BackgroundSubtractorMOG //! the update operator virtual void apply(InputArray image, OutputArray fgmask, double learningRate=0) CV_OVERRIDE; + virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE; + //! re-initiaization method virtual void initialize(Size _frameSize, int _frameType) { @@ -461,6 +464,15 @@ void BackgroundSubtractorMOGImpl::apply(InputArray _image, OutputArray _fgmask, CV_Error( Error::StsUnsupportedFormat, "Only 1- and 3-channel 8-bit images are supported in BackgroundSubtractorMOG" ); } +void BackgroundSubtractorMOGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){ + Mat knownForegroundMask = _knownForegroundMask.getMat(); + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate); +} + Ptr createBackgroundSubtractorMOG(int history, int nmixtures, double backgroundRatio, double noiseSigma) { diff --git a/modules/bgsegm/src/bgfg_gmg.cpp b/modules/bgsegm/src/bgfg_gmg.cpp index a83a83cc696..9ec5101a736 100644 --- a/modules/bgsegm/src/bgfg_gmg.cpp +++ b/modules/bgsegm/src/bgfg_gmg.cpp @@ -51,6 +51,7 @@ #include "precomp.hpp" #include "opencv2/core/utility.hpp" #include +#include "opencv2/core/utils/logger.hpp" namespace cv { @@ -97,6 +98,7 @@ class BackgroundSubtractorGMGImpl CV_FINAL : public BackgroundSubtractorGMG * @param fgmask Output mask image representing foreground and background pixels */ virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1.0) CV_OVERRIDE; + virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE; /** * Releases all inner buffers. @@ -473,6 +475,15 @@ void BackgroundSubtractorGMGImpl::apply(InputArray _frame, OutputArray _fgmask, ++frameNum_; } +void BackgroundSubtractorGMGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double newLearningRate){ + Mat knownForegroundMask = _knownForegroundMask.getMat(); + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, newLearningRate); +} + void BackgroundSubtractorGMGImpl::release() { frameSize_ = Size(); diff --git a/modules/bgsegm/src/bgfg_gsoc.cpp b/modules/bgsegm/src/bgfg_gsoc.cpp index 2cc33febfc6..eab5c57097a 100644 --- a/modules/bgsegm/src/bgfg_gsoc.cpp +++ b/modules/bgsegm/src/bgfg_gsoc.cpp @@ -53,6 +53,7 @@ #include #include #include "opencv2/core/cvdef.h" +#include "opencv2/core/utils/logger.hpp" namespace cv { @@ -494,6 +495,7 @@ class BackgroundSubtractorGSOCImpl CV_FINAL : public BackgroundSubtractorGSOC { float noiseRemovalThresholdFacFG); CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE; + CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE; CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE; @@ -542,6 +544,7 @@ class BackgroundSubtractorLSBPImpl CV_FINAL : public BackgroundSubtractorLSBP { ); CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE; + CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE; CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE; @@ -793,6 +796,15 @@ void BackgroundSubtractorGSOCImpl::apply(InputArray _image, OutputArray _fgmask, this->postprocessing(fgMask); } +void BackgroundSubtractorGSOCImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){ + Mat knownForegroundMask = _knownForegroundMask.getMat(); + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate); +} + void BackgroundSubtractorGSOCImpl::getBackgroundImage(OutputArray _backgroundImage) const { CV_Assert(!backgroundModel.empty()); const Size sz = backgroundModel->getSize(); @@ -928,6 +940,15 @@ void BackgroundSubtractorLSBPImpl::apply(InputArray _image, OutputArray _fgmask, this->postprocessing(fgMask); } +void BackgroundSubtractorLSBPImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){ + Mat knownForegroundMask = _knownForegroundMask.getMat(); + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate); +} + void BackgroundSubtractorLSBPImpl::getBackgroundImage(OutputArray _backgroundImage) const { CV_Assert(!backgroundModel.empty()); const Size sz = backgroundModel->getSize(); diff --git a/modules/bgsegm/src/bgfg_subcnt.cpp b/modules/bgsegm/src/bgfg_subcnt.cpp index 1638de50e62..c46111f6a40 100644 --- a/modules/bgsegm/src/bgfg_subcnt.cpp +++ b/modules/bgsegm/src/bgfg_subcnt.cpp @@ -44,6 +44,7 @@ #include "precomp.hpp" #include +#include "opencv2/core/utils/logger.hpp" namespace cv { @@ -61,6 +62,8 @@ class BackgroundSubtractorCNTImpl CV_FINAL : public BackgroundSubtractorCNT // BackgroundSubtractor interface virtual void apply(InputArray image, OutputArray fgmask, double learningRate) CV_OVERRIDE; + virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE; + virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE; int getMinPixelStability() const CV_OVERRIDE; @@ -409,6 +412,14 @@ void BackgroundSubtractorCNTImpl::apply(InputArray image, OutputArray _fgmask, d prevFrame = frame; } +void BackgroundSubtractorCNTImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){ + Mat knownForegroundMask = _knownForegroundMask.getMat(); + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate); +} Ptr createBackgroundSubtractorCNT(int minPixelStability, bool useHistory, int maxStability, bool isParallel) { diff --git a/modules/cudabgsegm/include/opencv2/cudabgsegm.hpp b/modules/cudabgsegm/include/opencv2/cudabgsegm.hpp index eb5467e9b87..fa05913e51d 100644 --- a/modules/cudabgsegm/include/opencv2/cudabgsegm.hpp +++ b/modules/cudabgsegm/include/opencv2/cudabgsegm.hpp @@ -84,6 +84,8 @@ class CV_EXPORTS_W BackgroundSubtractorMOG : public cv::BackgroundSubtractor using cv::BackgroundSubtractor::apply; CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) = 0; + CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) = 0; + using cv::BackgroundSubtractor::getBackgroundImage; virtual void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const = 0; @@ -135,6 +137,8 @@ class CV_EXPORTS_W BackgroundSubtractorMOG2 : public cv::BackgroundSubtractorMOG CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) = 0; + CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) = 0; + virtual void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const = 0; CV_WRAP inline void getBackgroundImage(CV_OUT GpuMat &backgroundImage, Stream& stream) { diff --git a/modules/cudabgsegm/src/mog.cpp b/modules/cudabgsegm/src/mog.cpp index 8a43293d43a..1c0506ba7c9 100644 --- a/modules/cudabgsegm/src/mog.cpp +++ b/modules/cudabgsegm/src/mog.cpp @@ -41,6 +41,7 @@ //M*/ #include "precomp.hpp" +#include "opencv2/core/utils/logger.hpp" using namespace cv; using namespace cv::cuda; @@ -79,6 +80,9 @@ namespace void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE; void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) CV_OVERRIDE; + void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE; + void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) CV_OVERRIDE; + void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE; void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const CV_OVERRIDE; @@ -131,6 +135,22 @@ namespace apply(image, fgmask, learningRate, Stream::Null()); } + void MOGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){ + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate, Stream::Null()); + } + + void MOGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate, Stream &stream){ + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate, stream); + } + void MOGImpl::apply(InputArray _frame, OutputArray _fgmask, double learningRate, Stream& stream) { using namespace cv::cuda::device::mog; diff --git a/modules/cudabgsegm/src/mog2.cpp b/modules/cudabgsegm/src/mog2.cpp index 47135a088ba..f4edc9b6216 100644 --- a/modules/cudabgsegm/src/mog2.cpp +++ b/modules/cudabgsegm/src/mog2.cpp @@ -42,6 +42,7 @@ #include "precomp.hpp" #include "cuda/mog2.hpp" +#include "opencv2/core/utils/logger.hpp" using namespace cv; using namespace cv::cuda; @@ -83,6 +84,9 @@ class MOG2Impl CV_FINAL : public cuda::BackgroundSubtractorMOG2 void apply(InputArray image, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE; void apply(InputArray image, OutputArray fgmask, double learningRate, Stream &stream) CV_OVERRIDE; + void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE; + void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) CV_OVERRIDE; + void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE; void getBackgroundImage(OutputArray backgroundImage, Stream &stream) const CV_OVERRIDE; @@ -174,6 +178,22 @@ void MOG2Impl::apply(InputArray image, OutputArray fgmask, double learningRate) apply(image, fgmask, learningRate, Stream::Null()); } +void MOG2Impl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){ + if(!_knownForegroundMask.empty()) + { + CV_Error( Error::StsNotImplemented, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate, Stream::Null()); +} + +void MOG2Impl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate, Stream &stream){ + if(!_knownForegroundMask.empty()) + { + CV_Error( Error::StsNotImplemented, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate, stream); +} + void MOG2Impl::apply(InputArray _frame, OutputArray _fgmask, double learningRate, Stream &stream) { using namespace cv::cuda::device::mog2; diff --git a/modules/cudalegacy/include/opencv2/cudalegacy.hpp b/modules/cudalegacy/include/opencv2/cudalegacy.hpp index 8230eaa2171..ec8ea72eca2 100644 --- a/modules/cudalegacy/include/opencv2/cudalegacy.hpp +++ b/modules/cudalegacy/include/opencv2/cudalegacy.hpp @@ -92,6 +92,7 @@ class CV_EXPORTS BackgroundSubtractorGMG : public cv::BackgroundSubtractor public: using cv::BackgroundSubtractor::apply; virtual void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) = 0; + virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) = 0; virtual int getMaxFeatures() const = 0; virtual void setMaxFeatures(int maxFeatures) = 0; diff --git a/modules/cudalegacy/src/fgd.cpp b/modules/cudalegacy/src/fgd.cpp index db98348741c..99319c9b0a1 100644 --- a/modules/cudalegacy/src/fgd.cpp +++ b/modules/cudalegacy/src/fgd.cpp @@ -55,6 +55,7 @@ Ptr cv::cuda::createBackgroundSubtractorFGD(const #include "cuda/fgd.hpp" #include "opencv2/imgproc.hpp" +#include "opencv2/core/utils/logger.hpp" ///////////////////////////////////////////////////////////////////////// // FGDParams @@ -546,6 +547,7 @@ namespace ~FGDImpl(); void apply(InputArray image, OutputArray fgmask, double learningRate=-1); + void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1); void getBackgroundImage(OutputArray backgroundImage) const; @@ -588,6 +590,14 @@ namespace { } + void FGDImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){ + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate); + } + void FGDImpl::apply(InputArray _frame, OutputArray fgmask, double) { GpuMat curFrame = _frame.getGpuMat(); diff --git a/modules/cudalegacy/src/gmg.cpp b/modules/cudalegacy/src/gmg.cpp index a982d8689bf..28f9cf4fa4a 100644 --- a/modules/cudalegacy/src/gmg.cpp +++ b/modules/cudalegacy/src/gmg.cpp @@ -41,6 +41,7 @@ //M*/ #include "precomp.hpp" +#include "opencv2/core/utils/logger.hpp" using namespace cv; using namespace cv::cuda; @@ -73,6 +74,10 @@ namespace void apply(InputArray image, OutputArray fgmask, double learningRate=-1); void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream); + // Overloaded Background Subtractor Applys featuring knownForegroundMask parameter + void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1); + void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream); + void getBackgroundImage(OutputArray backgroundImage) const; int getMaxFeatures() const { return maxFeatures_; } @@ -165,6 +170,22 @@ namespace apply(image, fgmask, learningRate, Stream::Null()); } + void GMGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){ + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate); + } + + void GMGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate, Stream& stream){ + if(!_knownForegroundMask.empty()) + { + CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground"); + } + apply(_image, _fgmask, learningRate, stream); + } + void GMGImpl::apply(InputArray _frame, OutputArray _fgmask, double newLearningRate, Stream& stream) { using namespace cv::cuda::device::gmg;