Skip to content

Commit 093fc18

Browse files
committed
Compatibility with core repos, pure virtual overload foreground masking
1 parent b01e6a7 commit 093fc18

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
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);
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class BackgroundSubtractorMOGImpl CV_FINAL : public BackgroundSubtractorMOG
104104
//! the update operator
105105
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=0) CV_OVERRIDE;
106106

107+
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
108+
107109
//! re-initiaization method
108110
virtual void initialize(Size _frameSize, int _frameType)
109111
{
@@ -461,6 +463,15 @@ void BackgroundSubtractorMOGImpl::apply(InputArray _image, OutputArray _fgmask,
461463
CV_Error( Error::StsUnsupportedFormat, "Only 1- and 3-channel 8-bit images are supported in BackgroundSubtractorMOG" );
462464
}
463465

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

modules/bgsegm/src/bgfg_gmg.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class BackgroundSubtractorGMGImpl CV_FINAL : public BackgroundSubtractorGMG
9797
* @param fgmask Output mask image representing foreground and background pixels
9898
*/
9999
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1.0) CV_OVERRIDE;
100+
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
100101

101102
/**
102103
* Releases all inner buffers.
@@ -473,6 +474,15 @@ void BackgroundSubtractorGMGImpl::apply(InputArray _frame, OutputArray _fgmask,
473474
++frameNum_;
474475
}
475476

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

modules/bgsegm/src/bgfg_gsoc.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ class BackgroundSubtractorGSOCImpl CV_FINAL : public BackgroundSubtractorGSOC {
494494
float noiseRemovalThresholdFacFG);
495495

496496
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE;
497+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
497498

498499
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
499500

@@ -542,6 +543,7 @@ class BackgroundSubtractorLSBPImpl CV_FINAL : public BackgroundSubtractorLSBP {
542543
);
543544

544545
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate = -1) CV_OVERRIDE;
546+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
545547

546548
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
547549

@@ -793,6 +795,15 @@ void BackgroundSubtractorGSOCImpl::apply(InputArray _image, OutputArray _fgmask,
793795
this->postprocessing(fgMask);
794796
}
795797

798+
void BackgroundSubtractorGSOCImpl::apply(InputArray _image, InputArray _knownForegroundMask, OutputArray _fgmask, double learningRate){
799+
Mat knownForegroundMask = _knownForegroundMask.getMat();
800+
if(!_knownForegroundMask.empty())
801+
{
802+
CV_Error( Error::StsNotImplemented, "Known Foreground Masking has not been implemented for this specific background subtractor, falling back to subtraction without known foreground");
803+
}
804+
apply(_image, _fgmask, learningRate);
805+
}
806+
796807
void BackgroundSubtractorGSOCImpl::getBackgroundImage(OutputArray _backgroundImage) const {
797808
CV_Assert(!backgroundModel.empty());
798809
const Size sz = backgroundModel->getSize();
@@ -928,6 +939,15 @@ void BackgroundSubtractorLSBPImpl::apply(InputArray _image, OutputArray _fgmask,
928939
this->postprocessing(fgMask);
929940
}
930941

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

modules/bgsegm/src/bgfg_subcnt.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class BackgroundSubtractorCNTImpl CV_FINAL : public BackgroundSubtractorCNT
6161

6262
// BackgroundSubtractor interface
6363
virtual void apply(InputArray image, OutputArray fgmask, double learningRate) CV_OVERRIDE;
64+
virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate) CV_OVERRIDE;
65+
6466
virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE;
6567

6668
int getMinPixelStability() const CV_OVERRIDE;
@@ -409,6 +411,14 @@ void BackgroundSubtractorCNTImpl::apply(InputArray image, OutputArray _fgmask, d
409411
prevFrame = frame;
410412
}
411413

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

413423
Ptr<BackgroundSubtractorCNT> createBackgroundSubtractorCNT(int minPixelStability, bool useHistory, int maxStability, bool isParallel)
414424
{

0 commit comments

Comments
 (0)