Skip to content

Commit 61003e2

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

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

modules/bgsegm/include/opencv2/bgsegm.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ 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 knownForegroundMask The mask for inputting already known foreground, allows model to ignore learning known pixels.
71+
@param learningRate The value between 0 and 1 that indicates how fast the background model is
72+
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
73+
rate. 0 means that the background model is not updated at all, 1 means that the background model
74+
is completely reinitialized from the last frame.
75+
*/
76+
77+
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
78+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
79+
6580
CV_WRAP virtual int getHistory() const = 0;
6681
CV_WRAP virtual void setHistory(int nframes) = 0;
6782

@@ -104,12 +119,29 @@ class CV_EXPORTS_W BackgroundSubtractorGMG : public BackgroundSubtractor
104119
105120
@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.
106121
@param fgmask The output foreground mask as an 8-bit binary image.
122+
@param knownForegroundMask The mask for inputting already known foreground, allows model to ignore learning known pixels.
107123
@param learningRate The value between 0 and 1 that indicates how fast the background model is
108124
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
109125
rate. 0 means that the background model is not updated at all, 1 means that the background model
110126
is completely reinitialized from the last frame.
111127
*/
112128
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
129+
130+
/** @brief Computes a foreground mask with known foreground mask input.
131+
132+
@param image Next video frame.
133+
@param fgmask The output foreground mask as an 8-bit binary image.
134+
@param knownForegroundMask The mask for inputting already known foreground.
135+
@param learningRate The value between 0 and 1 that indicates how fast the background model is
136+
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
137+
rate. 0 means that the background model is not updated at all, 1 means that the background model
138+
is completely reinitialized from the last frame.
139+
140+
@note This method has a default virtual implementation that throws a "not impemented" error.
141+
Foreground masking may not be supported by all background subtractors.
142+
*/
143+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
144+
113145
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
114146

115147
/** @brief Returns total number of distinct colors to maintain in histogram.
@@ -210,6 +242,22 @@ class CV_EXPORTS_W BackgroundSubtractorCNT : public BackgroundSubtractor
210242
public:
211243
// BackgroundSubtractor interface
212244
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
245+
246+
/** @brief Computes a foreground mask with known foreground mask input.
247+
248+
@param image Next video frame.
249+
@param knownForegroundMask The mask for inputting already known foreground.
250+
@param fgmask The output foreground mask as an 8-bit binary image.
251+
@param learningRate The value between 0 and 1 that indicates how fast the background model is
252+
learnt. Negative parameter value makes the algorithm to use some automatically chosen learning
253+
rate. 0 means that the background model is not updated at all, 1 means that the background model
254+
is completely reinitialized from the last frame.
255+
256+
@note This method has a default virtual implementation that throws a "not impemented" error.
257+
Foreground masking may not be supported by all background subtractors.
258+
*/
259+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
260+
213261
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
214262

215263
/** @brief Returns number of frames with same pixel color to consider stable.
@@ -269,6 +317,7 @@ class CV_EXPORTS_W BackgroundSubtractorGSOC : public BackgroundSubtractor
269317
public:
270318
// BackgroundSubtractor interface
271319
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
320+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
272321

273322
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
274323
};
@@ -280,6 +329,7 @@ class CV_EXPORTS_W BackgroundSubtractorLSBP : public BackgroundSubtractor
280329
public:
281330
// BackgroundSubtractor interface
282331
CV_WRAP virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
332+
CV_WRAP virtual void apply(InputArray image, InputArray knownForegroundMask, OutputArray fgmask, double learningRate=-1) CV_OVERRIDE = 0;
283333

284334
CV_WRAP virtual void getBackgroundImage(OutputArray backgroundImage) const CV_OVERRIDE = 0;
285335
};

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)