Skip to content

Cv::BackgroundSubtractorMOG2

Gaussian Mixture-based Background/Foreground Segmentation Algorithm.

Default parameters

// default parameters of gaussian background detection algorithm

static const int defaultHistory2 = 500; ///< Learning rate; alpha = 1/defaultHistory2
static const float defaultVarThreshold2 = 4.0f * 4.0f;
static const int defaultNMixtures2 = 5; ///< maximal number of Gaussians in mixture
static const float defaultBackgroundRatio2 = 0.9f; ///< threshold sum of weights for background test
static const float defaultVarThresholdGen2 = 3.0f * 3.0f;
static const float defaultVarInit2 = 15.0f; ///< initial variance for new components
static const float defaultVarMax2 = 5 * defaultVarInit2;
static const float defaultVarMin2 = 4.0f;

// additional parameters

/// @brief complexity reduction prior constant 0 - no reduction of number of components
static const float defaultfCT2 = 0.05f;

/// @brief value to use in the segmentation mask for shadows, set 0 not to do shadow detection
static const unsigned char defaultnShadowDetection2 = (unsigned char) 127;

/// @brief Tau - shadow threshold, see the paper for explanation
static const float defaultfTau = 0.5f;

Example

OpenCV 2.x

pMOG2 = new BackgroundSubtractorMOG2(history, varThreshold, bShadowDetection); //MOG2 approach
pMOG2->setInt("nmixtures", 3);
pMOG2->setDouble("fTau", 0.5);
pMOG2->operator()(src, fgMaskMOG2);

OpenCV 3.x version

cv::Ptr<BackgroundSubtractorMOG2> bg = createBackgroundSubtractorMOG2(...)
bg->setNMixtures(3);
bg->apply(img,mask);

See also

Favorite site

OpenCV 3.x version

References


  1. Tutorial_-_Background_detection_with_OpenCV.pdf