[OpenCV] Gaussian mixture Background Modeling

Source: Internet
Author: User

Two versions of Gaussian Mixture Background/Foreground Segmentation (Gaussian Mixture-based Background/Foreground Segmentation Algorithm) [1-2] are implemented in OpenCV. The Calling Interface is clear and the effect is good.

BackgroundSubtractorMOG example

int main(){VideoCapture video("1.avi");Mat frame,mask,thresholdImage, output;video>>frame;BackgroundSubtractorMOG bgSubtractor(20,10,0.5,false);while(true){video>>frame;++frameNum;bgSubtractor(frame,mask,0.001);imshow("mask",mask);waitKey(10);}return 0;}

The constructor can use the default constructor or constructor with parameters:

BackgroundSubtractorMOG::BackgroundSubtractorMOG()BackgroundSubtractorMOG::BackgroundSubtractorMOG(int history, int nmixtures, double backgroundRatio, double noiseSigma=0)

History indicates the number of historical frames, nmixtures indicates the number of Gaussian mixture, backgroundRatio indicates the background ratio, and noiseSigma indicates the noise weight.

The called interface only has the overload operator ():

void BackgroundSubtractorMOG::operator()(InputArray image, OutputArray fgmask, double learningRate=0)
Image indicates the current frame, fgmask indicates the output foreground mask, and learningRate indicates the background learning rate.

The following is a foreground/background check using BackgroundSubtractorMOG.


BackgroundSubtractorMOG2 usage example

int main(){VideoCapture video("1.avi");Mat frame,mask,thresholdImage, output;//video>>frame;BackgroundSubtractorMOG2 bgSubtractor(20,16,true);while(true){video>>frame;++frameNum;bgSubtractor(frame,mask,0.001);cout<<frameNum<<endl;//imshow("mask",mask);//waitKey(10);}return 0;}

Likewise, constructor can use the default constructor and constructor with parameters.

BackgroundSubtractorMOG2::BackgroundSubtractorMOG2()BackgroundSubtractorMOG2::BackgroundSubtractorMOG2(int history, float varThreshold, bool bShadowDetection=true )

History is the same as above. varThreshold indicates the threshold value used on the Markov square distance to determine whether the background is set (this value does not affect the background update rate). bShadowDetection indicates whether to use shadow detection (if Shadow Detection is enabled, 127 indicates the shadow in the mask ).

Call each frame detection function using the overload operator:

void BackgroundSubtractorMOG2::operator()(InputArray image, OutputArray fgmask, double learningRate=-1)
The parameter meaning is the same as the operator () function in BackgroundSubtractorMOG.

At the same time, BackgroundSubtractorMOG2 provides the getBackgroundImage () function to return the background image:

void BackgroundSubtractorMOG2::getBackgroundImage(OutputArray backgroundImage)

In addition, the refman of OpenCV says there are other parameters related to model oil that can be modified after the object is created, but the pitfall is that opencv declares these function parameters as protected, the access interface is not provided at the same time, so you must modify the source file to provide the access interface.

protected:    Size frameSize;    int frameType;    Mat bgmodel;    Mat bgmodelUsedModes;//keep track of number of modes per pixel    int nframes;    int history;    int nmixtures;    //! here it is the maximum allowed number of mixture components.    //! Actual number is determined dynamically per pixel    double varThreshold;    // threshold on the squared Mahalanobis distance to decide if it is well described    // by the background model or not. Related to Cthr from the paper.    // This does not influence the update of the background. A typical value could be 4 sigma    // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.    /////////////////////////    // less important parameters - things you might change but be carefull    ////////////////////////    float backgroundRatio;    // corresponds to fTB=1-cf from the paper    // TB - threshold when the component becomes significant enough to be included into    // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.    // For alpha=0.001 it means that the mode should exist for approximately 105 frames before    // it is considered foreground    // float noiseSigma;    float varThresholdGen;    //correspondts to Tg - threshold on the squared Mahalan. dist. to decide    //when a sample is close to the existing components. If it is not close    //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.    //Smaller Tg leads to more generated components and higher Tg might make    //lead to small number of components but they can grow too large    float fVarInit;    float fVarMin;    float fVarMax;    //initial variance  for the newly generated components.    //It will will influence the speed of adaptation. A good guess should be made.    //A simple way is to estimate the typical standard deviation from the images.    //I used here 10 as a reasonable value    // min and max can be used to further control the variance    float fCT;//CT - complexity reduction prior    //this is related to the number of samples needed to accept that a component    //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get    //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)    //shadow detection parameters    bool bShadowDetection;//default 1 - do shadow detection    unsigned char nShadowDetection;//do shadow detection - insert this value as the detection result - 127 default value    float fTau;    // Tau - shadow threshold. The shadow is detected if the pixel is darker    //version of the background. Tau is a threshold on how much darker the shadow can be.    //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow    //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.

The following are the foreground and background of BackgroundSubtractorMOG2 Detection:

References:

[1] KaewTraKulPong, Pakorn, and Richard Bowden. "An improved adaptive background mixture model for real-time tracking with shadow detection. "Video-Based Surveillance Systems. springer US, 2002. 135-144.
[2] Zivkovic, Zoran. "Improved adaptive Gaussian mixture model for background subtraction. "Pattern Recognition, 2004. ICPR 2004. proceedings of the 17th International Conference on. vol. 2. IEEE, 2004.



(Reprinted please indicate the author and Source: http://blog.csdn.net/xiaowei_cqu is not allowed for commercial use)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.