Skip to content

Commit 143c448

Browse files
committed
Merge branch 4.x
2 parents 32932e8 + c4c74c4 commit 143c448

File tree

27 files changed

+453
-26
lines changed

27 files changed

+453
-26
lines changed

modules/bgsegm/include/opencv2/bgsegm.hpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,32 @@ The class implements the algorithm described in @cite KB2001 .
6262
class CV_EXPORTS_W BackgroundSubtractorMOG : public BackgroundSubtractor
6363
{
6464
public:
65+
// BackgroundSubtractor interface
66+
/** @brief Computes a foreground mask.
67+
68+
@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.
69+
@param fgmask The output foreground mask as an 8-bit binary image.
70+
@param learningRate The value between 0 and 1 that indicates how fast the background model is
71+
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
72+
rate. 0 means that the background model is not updated at all, 1 means that the background model
73+
is completely reinitialized from the last frame.
74+
*/
75+
76+
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
77+
78+
/** @brief Computes a foreground mask and skips known foreground in evaluation.
79+
80+
@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.
81+
@param fgmask The output foreground mask as an 8-bit binary image.
82+
@param knownForegroundMask The mask for inputting already known foreground, allows model to ignore learning known pixels.
83+
@param learningRate The value between 0 and 1 that indicates how fast the background model is
84+
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
85+
rate. 0 means that the background model is not updated at all, 1 means that the background model
86+
is completely reinitialized from the last frame.
87+
*/
88+
89+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
90+
6591
CV_WRAP virtual int getHistory() const = 0;
6692
CV_WRAP virtual void setHistory(int nframes) = 0;
6793

@@ -110,6 +136,22 @@ class CV_EXPORTS_W BackgroundSubtractorGMG : public BackgroundSubtractor
110136
is completely reinitialized from the last frame.
111137
*/
112138
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
139+
140+
/** @brief Computes a foreground mask with known foreground mask input.
141+
142+
@param image Next video frame.
143+
@param fgmask The output foreground mask as an 8-bit binary image.
144+
@param knownForegroundMask The mask for inputting already known foreground.
145+
@param learningRate The value between 0 and 1 that indicates how fast the background model is
146+
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
147+
rate. 0 means that the background model is not updated at all, 1 means that the background model
148+
is completely reinitialized from the last frame.
149+
150+
@note This method has a default virtual implementation that throws a "not impemented" error.
151+
Foreground masking may not be supported by all background subtractors.
152+
*/
153+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
154+
113155
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
114156

115157
/** @brief Returns total number of distinct colors to maintain in histogram.
@@ -210,6 +252,22 @@ class CV_EXPORTS_W BackgroundSubtractorCNT : public BackgroundSubtractor
210252
public:
211253
// BackgroundSubtractor interface
212254
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
255+
256+
/** @brief Computes a foreground mask with known foreground mask input.
257+
258+
@param image Next video frame.
259+
@param knownForegroundMask The mask for inputting already known foreground.
260+
@param fgmask The output foreground mask as an 8-bit binary image.
261+
@param learningRate The value between 0 and 1 that indicates how fast the background model is
262+
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
263+
rate. 0 means that the background model is not updated at all, 1 means that the background model
264+
is completely reinitialized from the last frame.
265+
266+
@note This method has a default virtual implementation that throws a "not impemented" error.
267+
Foreground masking may not be supported by all background subtractors.
268+
*/
269+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
270+
213271
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
214272

215273
/** @brief Returns number of frames with same pixel color to consider stable.
@@ -269,6 +327,7 @@ class CV_EXPORTS_W BackgroundSubtractorGSOC : public BackgroundSubtractor
269327
public:
270328
// BackgroundSubtractor interface
271329
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
330+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
272331

273332
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
274333
};
@@ -280,6 +339,7 @@ class CV_EXPORTS_W BackgroundSubtractorLSBP : public BackgroundSubtractor
280339
public:
281340
// BackgroundSubtractor interface
282341
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
342+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
283343

284344
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
285345
};

modules/bgsegm/src/bgfg_gaussmix.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include "precomp.hpp"
4444
#include <float.h>
45+
#include "opencv2/core/utils/logger.hpp"
4546

4647
// to make sure we can use these short names
4748
#undef K
@@ -104,6 +105,8 @@ class BackgroundSubtractorMOGImpl CV_FINAL : public BackgroundSubtractorMOG
104105
//! the update operator
105106
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=0) CV_OVERRIDE;
106107

108+
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
109+
107110
//! re-initiaization method
108111
virtual void initialize(Size _frameSize, int _frameType)
109112
{
@@ -461,6 +464,15 @@ void BackgroundSubtractorMOGImpl::apply(InputArray _image, OutputArray _fgmask,
461464
CV_Error( Error::StsUnsupportedFormat, "Only 1- and 3-channel 8-bit images are supported in BackgroundSubtractorMOG" );
462465
}
463466

467+
void BackgroundSubtractorMOGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
468+
Mat knownForegroundMask = _knownForegroundMask.getMat();
469+
if(!_knownForegroundMask.empty())
470+
{
471+
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
472+
}
473+
apply(_image, _fgmask, learningRate);
474+
}
475+
464476
Ptr<BackgroundSubtractorMOG> createBackgroundSubtractorMOG(int history, int nmixtures,
465477
double backgroundRatio, double noiseSigma)
466478
{

modules/bgsegm/src/bgfg_gmg.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "precomp.hpp"
5252
#include "opencv2/core/utility.hpp"
5353
#include <limits>
54+
#include "opencv2/core/utils/logger.hpp"
5455

5556
namespace cv
5657
{
@@ -97,6 +98,7 @@ class BackgroundSubtractorGMGImpl CV_FINAL : public BackgroundSubtractorGMG
9798
* @param fgmask Output mask image representing foreground and background pixels
9899
*/
99100
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1.0) CV_OVERRIDE;
101+
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
100102

101103
/**
102104
* Releases all inner buffers.
@@ -473,6 +475,15 @@ void BackgroundSubtractorGMGImpl::apply(InputArray _frame, OutputArray _fgmask,
473475
++frameNum_;
474476
}
475477

478+
void BackgroundSubtractorGMGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double newLearningRate){
479+
Mat knownForegroundMask = _knownForegroundMask.getMat();
480+
if(!_knownForegroundMask.empty())
481+
{
482+
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
483+
}
484+
apply(_image, _fgmask, newLearningRate);
485+
}
486+
476487
void BackgroundSubtractorGMGImpl::release()
477488
{
478489
frameSize_ = Size();

modules/bgsegm/src/bgfg_gsoc.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <opencv2/3d.hpp>
5454
#include <iostream>
5555
#include "opencv2/core/cvdef.h"
56+
#include "opencv2/core/utils/logger.hpp"
5657

5758
namespace cv
5859
{
@@ -494,6 +495,7 @@ class BackgroundSubtractorGSOCImpl CV_FINAL : public BackgroundSubtractorGSOC {
494495
float noiseRemovalThresholdFacFG);
495496

496497
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE;
498+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
497499

498500
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
499501

@@ -542,6 +544,7 @@ class BackgroundSubtractorLSBPImpl CV_FINAL : public BackgroundSubtractorLSBP {
542544
);
543545

544546
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE;
547+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
545548

546549
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
547550

@@ -793,6 +796,15 @@ void BackgroundSubtractorGSOCImpl::apply(InputArray _image, OutputArray _fgmask,
793796
this->postprocessing(fgMask);
794797
}
795798

799+
void BackgroundSubtractorGSOCImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
800+
Mat knownForegroundMask = _knownForegroundMask.getMat();
801+
if(!_knownForegroundMask.empty())
802+
{
803+
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
804+
}
805+
apply(_image, _fgmask, learningRate);
806+
}
807+
796808
void BackgroundSubtractorGSOCImpl::getBackgroundImage(OutputArray _backgroundImage) const {
797809
CV_Assert(!backgroundModel.empty());
798810
const Size sz = backgroundModel->getSize();
@@ -928,6 +940,15 @@ void BackgroundSubtractorLSBPImpl::apply(InputArray _image, OutputArray _fgmask,
928940
this->postprocessing(fgMask);
929941
}
930942

943+
void BackgroundSubtractorLSBPImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
944+
Mat knownForegroundMask = _knownForegroundMask.getMat();
945+
if(!_knownForegroundMask.empty())
946+
{
947+
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
948+
}
949+
apply(_image, _fgmask, learningRate);
950+
}
951+
931952
void BackgroundSubtractorLSBPImpl::getBackgroundImage(OutputArray _backgroundImage) const {
932953
CV_Assert(!backgroundModel.empty());
933954
const Size sz = backgroundModel->getSize();

modules/bgsegm/src/bgfg_subcnt.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
#include "precomp.hpp"
4646
#include <functional>
47+
#include "opencv2/core/utils/logger.hpp"
4748

4849
namespace cv
4950
{
@@ -61,6 +62,8 @@ class BackgroundSubtractorCNTImpl CV_FINAL : public BackgroundSubtractorCNT
6162

6263
// BackgroundSubtractor interface
6364
virtual void apply(InputArray image, OutputArray fgmask, double learningRate) CV_OVERRIDE;
65+
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
66+
6467
virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
6568

6669
int getMinPixelStability() const CV_OVERRIDE;
@@ -409,6 +412,14 @@ void BackgroundSubtractorCNTImpl::apply(InputArray image, OutputArray _fgmask, d
409412
prevFrame = frame;
410413
}
411414

415+
void BackgroundSubtractorCNTImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
416+
Mat knownForegroundMask = _knownForegroundMask.getMat();
417+
if(!_knownForegroundMask.empty())
418+
{
419+
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
420+
}
421+
apply(_image, _fgmask, learningRate);
422+
}
412423

413424
Ptr<BackgroundSubtractorCNT> createBackgroundSubtractorCNT(int minPixelStability, bool useHistory, int maxStability, bool isParallel)
414425
{

modules/cudabgsegm/include/opencv2/cudabgsegm.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class CV_EXPORTS_W BackgroundSubtractorMOG : public cv::BackgroundSubtractor
8484
using cv::BackgroundSubtractor::apply;
8585
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) = 0;
8686

87+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) = 0;
88+
8789
using cv::BackgroundSubtractor::getBackgroundImage;
8890
virtual void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const = 0;
8991

@@ -135,6 +137,8 @@ class CV_EXPORTS_W BackgroundSubtractorMOG2 : public cv::BackgroundSubtractorMOG
135137

136138
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) = 0;
137139

140+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) = 0;
141+
138142
virtual void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const = 0;
139143

140144
CV_WRAP inline void getBackgroundImage(CV_OUT GpuMat &backgroundImage, Stream& stream) {

modules/cudabgsegm/src/mog.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
//M*/
4242

4343
#include "precomp.hpp"
44+
#include "opencv2/core/utils/logger.hpp"
4445

4546
using namespace cv;
4647
using namespace cv::cuda;
@@ -79,6 +80,9 @@ namespace
7980
void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE;
8081
void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) CV_OVERRIDE;
8182

83+
void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE;
84+
void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) CV_OVERRIDE;
85+
8286
void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
8387
void getBackgroundImage(OutputArray backgroundImage, Stream& stream) const CV_OVERRIDE;
8488

@@ -131,6 +135,22 @@ namespace
131135
apply(image, fgmask, learningRate, Stream::Null());
132136
}
133137

138+
void MOGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
139+
if(!_knownForegroundMask.empty())
140+
{
141+
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
142+
}
143+
apply(_image, _fgmask, learningRate, Stream::Null());
144+
}
145+
146+
void MOGImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate, Stream &stream){
147+
if(!_knownForegroundMask.empty())
148+
{
149+
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
150+
}
151+
apply(_image, _fgmask, learningRate, stream);
152+
}
153+
134154
void MOGImpl::apply(InputArray _frame, OutputArray _fgmask, double learningRate, Stream& stream)
135155
{
136156
using namespace cv::cuda::device::mog;

modules/cudabgsegm/src/mog2.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include "precomp.hpp"
4444
#include "cuda/mog2.hpp"
45+
#include "opencv2/core/utils/logger.hpp"
4546

4647
using namespace cv;
4748
using namespace cv::cuda;
@@ -83,6 +84,9 @@ class MOG2Impl CV_FINAL : public cuda::BackgroundSubtractorMOG2
8384
void apply(InputArray image, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE;
8485
void apply(InputArray image, OutputArray fgmask, double learningRate, Stream &stream) CV_OVERRIDE;
8586

87+
void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE;
88+
void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) CV_OVERRIDE;
89+
8690
void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
8791
void getBackgroundImage(OutputArray backgroundImage, Stream &stream) const CV_OVERRIDE;
8892

@@ -174,6 +178,22 @@ void MOG2Impl::apply(InputArray image, OutputArray fgmask, double learningRate)
174178
apply(image, fgmask, learningRate, Stream::Null());
175179
}
176180

181+
void MOG2Impl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
182+
if(!_knownForegroundMask.empty())
183+
{
184+
CV_Error( Error::StsNotImplemented, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
185+
}
186+
apply(_image, _fgmask, learningRate, Stream::Null());
187+
}
188+
189+
void MOG2Impl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate, Stream &stream){
190+
if(!_knownForegroundMask.empty())
191+
{
192+
CV_Error( Error::StsNotImplemented, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
193+
}
194+
apply(_image, _fgmask, learningRate, stream);
195+
}
196+
177197
void MOG2Impl::apply(InputArray _frame, OutputArray _fgmask, double learningRate, Stream &stream)
178198
{
179199
using namespace cv::cuda::device::mog2;

modules/cudalegacy/include/opencv2/cudalegacy.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class CV_EXPORTS BackgroundSubtractorGMG : public cv::BackgroundSubtractor
9292
public:
9393
using cv::BackgroundSubtractor::apply;
9494
virtual void apply(InputArray image, OutputArray fgmask, double learningRate, Stream& stream) = 0;
95+
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate, Stream& stream) = 0;
9596

9697
virtual int getMaxFeatures() const = 0;
9798
virtual void setMaxFeatures(int maxFeatures) = 0;

modules/cudalegacy/src/fgd.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Ptr<cuda::BackgroundSubtractorFGD> cv::cuda::createBackgroundSubtractorFGD(const
5454
#else
5555

5656
#include "cuda/fgd.hpp"
57+
#include "opencv2/imgproc.hpp"
58+
#include "opencv2/core/utils/logger.hpp"
5759

5860
/////////////////////////////////////////////////////////////////////////
5961
// FGDParams
@@ -539,6 +541,7 @@ namespace
539541
~FGDImpl();
540542

541543
void apply(InputArray image, OutputArray fgmask, double learningRate=-1);
544+
void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1);
542545

543546
void getBackgroundImage(OutputArray backgroundImage) const;
544547

@@ -581,6 +584,14 @@ namespace
581584
{
582585
}
583586

587+
void FGDImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
588+
if(!_knownForegroundMask.empty())
589+
{
590+
CV_LOG_WARNING(NULL, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
591+
}
592+
apply(_image, _fgmask, learningRate);
593+
}
594+
584595
void FGDImpl::apply(InputArray _frame, OutputArray fgmask, double)
585596
{
586597
GpuMat curFrame = _frame.getGpuMat();

0 commit comments

Comments
 (0)