title: OpenCV: how to use createBackgroundSubtractorMOG
body: <p>I was trying to go through this tutorial på OpenCV.org:</p>
  
  <p><a href="http://docs.opencv.org/trunk/doc/tutorials/video/background_subtraction/background_subtraction.html#background-subtraction" rel="noreferrer">http://docs.opencv.org/trunk/doc/tutorials/video/background_subtraction/background_subtraction.html#background-subtraction</a></p>
  
  <p>The MOG pointer is initialized as</p>
  
  <pre><code>Ptr&lt;BackgroundSubtractor&gt; pMOG; //MOG Background subtractor
  </code></pre>
  
  <p>and in main, it is used in the following manner:</p>
  
  <pre><code>pMOG = createBackgroundSubtractorMOG();
  </code></pre>
  
  <p>However, this yields the following error:</p>
  
  <pre><code>    Error: Identifier "createBackgroundSubtractorMOG" is undefined
  </code></pre>
  
  <p>Also, when the background model is to be updated, the following command is used:</p>
  
  <pre><code>pMOG-&gt;apply(frame, fgMaskMOG);
  </code></pre>
  
  <p>Which in turn yields the following error:</p>
  
  <pre><code>    Error: class "cv::BackgroundSubtractor" has no member "apply"
  </code></pre>
  
  <p>Any idea of what can be done about this?
  Many thanks in advance!</p>
  
  <p>Here is the entire tutorial code:</p>
  
  <pre class="lang-c prettyprint-override"><code>//opencv
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/video/background_segm.hpp&gt;
  //C
  #include &lt;stdio.h&gt;
  //C++
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  
  using namespace cv;
  using namespace std;
  
  //global variables
  Mat frame; //current frame
  Mat fgMaskMOG; //fg mask generated by MOG method
  Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
  Ptr&lt;BackgroundSubtractor&gt; pMOG; //MOG Background subtractor
  Ptr&lt;BackgroundSubtractor&gt; pMOG2; //MOG2 Background subtractor
  int keyboard;
  
  //function declarations
  void help();
  void processVideo(char* videoFilename);
  void processImages(char* firstFrameFilename);
  
  void help()
  {
    cout
    &lt;&lt; "--------------------------------------------------------------------------"  &lt;&lt; endl
    &lt;&lt; "This program shows how to use background subtraction methods provided by "   &lt;&lt; endl
    &lt;&lt; " OpenCV. You can process both videos (-vid) and images (-img)."              &lt;&lt; endl
                                                                                     &lt;&lt; endl
    &lt;&lt; "Usage:"                                                                      &lt;&lt; endl
    &lt;&lt; "./bs {-vid &lt;video filename&gt;|-img &lt;image filename&gt;}"                          &lt;&lt; endl
    &lt;&lt; "for example: ./bs -vid video.avi"                                            &lt;&lt; endl
    &lt;&lt; "or: ./bs -img /data/images/1.png"                                            &lt;&lt; endl
    &lt;&lt; "--------------------------------------------------------------------------"  &lt;&lt; endl
    &lt;&lt; endl;
  }
  
  int main(int argc, char* argv[])
  {
    //print help information
    help();
  
    //check for the input parameter correctness
    if(argc != 3) {
      cerr &lt;&lt;"Incorret input list" &lt;&lt; endl;
      cerr &lt;&lt;"exiting..." &lt;&lt; endl;
      return EXIT_FAILURE;
    }
  
    //create GUI windows
    namedWindow("Frame");
    namedWindow("FG Mask MOG");
    namedWindow("FG Mask MOG 2");
  
    //create Background Subtractor objects
    pMOG = createBackgroundSubtractorMOG(); //MOG approach
    pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach
  
    if(strcmp(argv[1], "-vid") == 0) {
      //input data coming from a video
      processVideo(argv[2]);
    }
    else if(strcmp(argv[1], "-img") == 0) {
      //input data coming from a sequence of images
      processImages(argv[2]);
    }
    else {
      //error in reading input parameters
      cerr &lt;&lt;"Please, check the input parameters." &lt;&lt; endl;
      cerr &lt;&lt;"Exiting..." &lt;&lt; endl;
      return EXIT_FAILURE;
    }
    //destroy GUI windows
    destroyAllWindows();
    return EXIT_SUCCESS;
  }
  
  void processVideo(char* videoFilename) {
    //create the capture object
    VideoCapture capture(videoFilename);
    if(!capture.isOpened()){
      //error in opening the video input
      cerr &lt;&lt; "Unable to open video file: " &lt;&lt; videoFilename &lt;&lt; endl;
      exit(EXIT_FAILURE);
    }
    //read input data. ESC or 'q' for quitting
    while( (char)keyboard != 'q' &amp;&amp; (char)keyboard != 27 ){
      //read the current frame
      if(!capture.read(frame)) {
        cerr &lt;&lt; "Unable to read next frame." &lt;&lt; endl;
        cerr &lt;&lt; "Exiting..." &lt;&lt; endl;
        exit(EXIT_FAILURE);
      }
      //update the background model
      pMOG-&gt;apply(frame, fgMaskMOG);
      pMOG2-&gt;apply(frame, fgMaskMOG2);
      //get the frame number and write it on the current frame
      stringstream ss;
      rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
                cv::Scalar(255,255,255), -1);
      ss &lt;&lt; capture.get(CAP_PROP_POS_FRAMES);
      string frameNumberString = ss.str();
      putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
              FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
      //show the current frame and the fg masks
      imshow("Frame", frame);
      imshow("FG Mask MOG", fgMaskMOG);
      imshow("FG Mask MOG 2", fgMaskMOG2);
      //get the input from the keyboard
      keyboard = waitKey( 30 );
    }
    //delete capture object
    capture.release();
  }
  
  void processImages(char* fistFrameFilename) {
    //read the first file of the sequence
    frame = imread(fistFrameFilename);
    if(!frame.data){
      //error in opening the first image
      cerr &lt;&lt; "Unable to open first image frame: " &lt;&lt; fistFrameFilename &lt;&lt; endl;
      exit(EXIT_FAILURE);
    }
    //current image filename
    string fn(fistFrameFilename);
    //read input data. ESC or 'q' for quitting
    while( (char)keyboard != 'q' &amp;&amp; (char)keyboard != 27 ){
      //update the background model
      pMOG-&gt;apply(frame, fgMaskMOG);
      pMOG2-&gt;apply(frame, fgMaskMOG2);
      //get the frame number and write it on the current frame
      size_t index = fn.find_last_of("/");
      if(index == string::npos) {
        index = fn.find_last_of("\\");
      }
      size_t index2 = fn.find_last_of(".");
      string prefix = fn.substr(0,index+1);
      string suffix = fn.substr(index2);
      string frameNumberString = fn.substr(index+1, index2-index-1);
      istringstream iss(frameNumberString);
      int frameNumber = 0;
      iss &gt;&gt; frameNumber;
      rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
                cv::Scalar(255,255,255), -1);
      putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
              FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
      //show the current frame and the fg masks
      imshow("Frame", frame);
      imshow("FG Mask MOG", fgMaskMOG);
      imshow("FG Mask MOG 2", fgMaskMOG2);
      //get the input from the keyboard
      keyboard = waitKey( 30 );
      //search for the next image in the sequence
      ostringstream oss;
      oss &lt;&lt; (frameNumber + 1);
      string nextFrameNumberString = oss.str();
      string nextFrameFilename = prefix + nextFrameNumberString + suffix;
      //read the next frame
      frame = imread(nextFrameFilename);
      if(!frame.data){
        //error in opening the next image in the sequence
        cerr &lt;&lt; "Unable to open image frame: " &lt;&lt; nextFrameFilename &lt;&lt; endl;
        exit(EXIT_FAILURE);
      }
      //update the path of the current frame
      fn.assign(nextFrameFilename);
    }
  }
  </code></pre>