This method is used to learn what belongs to the background of the image and what belongs to the foreground. The static cameras that monitor the area can very easily recognize, what is part of the image that is always here or there is something that is new and moving over the background. 

Background subtraction Visual studio 2019 project setup

If you have Opencv 4+ compiled or installed only steps you need to do is set the include directory with OpenCV header files. Set the Additional library Directories that point to \lib folder. Just note that Visual Studio 2019 should use VC16\lib. Finally, As additional dependencies, specify the libs used to resolve the function implementation in the code. The list for Opencv 420 is here. The different version of opencv is using different numbering for example opencv 440 will use 
opencv_core440.lib
opencv_bgsegm420.lib
opencv_core420.lib
opencv_videoio420.lib
opencv_imgproc420.lib
opencv_highgui420.lib
opencv_video420.lib 
Opencv Additional Include DIrectories
Opencv additional library directories
Additional Dependencies

Background subtraction C++ video

Opencv 4 C++ background subtractor full source code

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <vector>
#include <fstream>
#include <iostream>
#include <math.h>
#include <Windows.h>
#include "opencv2/video/background_segm.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
using namespace std;
int main(int argcconst char** argv)
{
  // Init background substractor
  Ptr<BackgroundSubtractor> bg_model = createBackgroundSubtractorMOG2(500,16.0,true);
 // Create empy input img, foreground and background image and foreground mask.
 Mat img, foregroundMask, backgroundImage, foregroundImg;
  // capture video from source 0, which is web camera, If you want capture video 
//file just replace //by  VideoCapture cap("videoFile.mov")
  VideoCapture cap(0);
  // main loop to grab sequence of input files
  for (;;) {
    bool ok = cap.grab();
    if (ok == false) {
      std::cout << "Video Capture Fail" << std::endl;
    }
    else {
      // obtain input image from source
      cap.retrieve(img, CAP_OPENNI_BGR_IMAGE);
      // Just resize input image if you want
      resize(img, img, Size(640480));
      // create foreground mask of proper size
      if (foregroundMask.empty()) {
        foregroundMask.create(img.size(), img.type());
      }
      // compute foreground mask 8 bit image
      // -1 is parameter that chose automatically your learning rate
      bg_model->apply(img, foregroundMask, true ? -1 : 0);
      // smooth the mask to reduce noise in image
      GaussianBlur(foregroundMask, foregroundMask, Size(1111), 3.53.5);
      // threshold mask to saturate at black and white values
      threshold(foregroundMask, foregroundMask, 10255, THRESH_BINARY);
      // create black foreground image
        foregroundImg = Scalar::all(0);
      // Copy source image to foreground image only in area with white mask
      img.copyTo(foregroundImg, foregroundMask);
      //Get background image
      bg_model->getBackgroundImage(backgroundImage);
      // Show the results
      imshow("foreground mask", foregroundMask);
Background extraction in opencv
      imshow("foreground image", foregroundImg);
      int key6 = waitKey(40);
      if (!backgroundImage.empty()) {
        imshow("mean background image", backgroundImage);
      int key5 = waitKey(40);
      }
    }
  }
  return EXIT_SUCCESS;
}