title: Background substraction using OpenCV MOG from live camera feed
body: <p>I am studying OpenCV and I'm having a lot of fun, but now I'm stuck with a problem where I'm trying to use a background subtraction algorithm to detect any changes.</p>
  
  <p>I followed <a href="https://web.archive.org/web/20150308034628/http://docs.opencv.org:80/trunk/doc/tutorials/video/background_subtraction/background_subtraction.html" rel="nofollow noreferrer">this</a> tutorial and I managed to get it working to detect changes in a video file (AVI).</p>
  
  <p>The problem I'm having right now is that it tends to incorrectly subtract the background noise and other small changes and instead fill the whole screen pretty much with white.</p>
  
  <p><a href="https://pastebin.com/VBk90XE8" rel="nofollow noreferrer">Here</a> is my implementation of the MOG algorithm on a live camera feed, but the relevant part is this:</p>
  
  <pre><code>    VideoCapture cap;
      if (argc &gt; 1)
          cap.open(argv[1]);
      else
          cap.open(0);
      cap.set(CV_CAP_PROP_FOURCC ,CV_FOURCC('D', 'I', 'V', '3') );
  ........
      Mat frame, fgMaskMOG;
  
      Ptr&lt;BackgroundSubtractor&gt; pMOG = new BackgroundSubtractorMOG();
      for (;;)
      {
          if(!cap.read(frame)) {
              cerr &lt;&lt; "Unable to read next frame." &lt;&lt; endl;
              continue;
          }
          // process the image to obtain a mask image.
          pMOG-&gt;operator()(frame, fgMaskMOG);
  
          std::string time = getDateTime();
          cv::rectangle(frame,cv::Rect(0,cap.get(CV_CAP_PROP_FRAME_HEIGHT)-25,290,20),cv::Scalar(255,255,255),-1);
          cv::putText(frame,time,cv::Point(0,cap.get(CV_CAP_PROP_FRAME_HEIGHT)-5),1,1.5,cv::Scalar(0,0,0),2);
  ........
          // show image.
          imshow("Image", frame);
          imshow("Debug",fgMaskMOG);
          int c = waitKey(30);
          if (c == 'q' || c == 'Q' || (c &amp; 255) == 27)
              break;
      }
  </code></pre>
  
  <p>This implementation works just fine for a video file as you can see:
  <a href="https://i.stack.imgur.com/yYNqR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yYNqR.png" alt="image description"></a><br>
  <sub>(source: <a href="http://answers.opencv.org//upfiles/14072745951051408.png" rel="nofollow noreferrer">opencv.org</a>)</sub>  </p>
  
  <p><a href="https://i.stack.imgur.com/xOv8d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xOv8d.png" alt="image description"></a><br>
  <sub>(source: <a href="http://answers.opencv.org//upfiles/14072746351663485.png" rel="nofollow noreferrer">opencv.org</a>)</sub>  </p>
  
  <p><br />
  But this is the result when I try to use MOG on a live camera feed:
  <a href="https://i.stack.imgur.com/6x4W2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6x4W2.png" alt="image description"></a><br>
  <sub>(source: <a href="http://answers.opencv.org//upfiles/14072748868325599.png" rel="nofollow noreferrer">opencv.org</a>)</sub>  </p>
  
  <p><a href="https://i.stack.imgur.com/QaEIP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QaEIP.png" alt="image description"></a><br>
  <sub>(source: <a href="http://answers.opencv.org//upfiles/14072749039556545.png" rel="nofollow noreferrer">opencv.org</a>)</sub>  </p>
  
  <p><br /></p>
  
  <p>(<strong>EDIT:</strong><br />
  <br />
   <strong>EXPECTED RESULT:</strong> My expectation was the same as the video file (see pictures one and two above).<br />
  <strong>ACTUAL RESULT:</strong> The actual result was far from my expected result, much noise was generated (i.e not filtered out), when one put something in front of the camera, it would be black instead of white (inverse from the result of the video file).<br />
  <br />
  - - - - <strong>SYSTEM DETAILS</strong> - - - -<br />
  <strong>OS:</strong> Windows x64bit<br />
  <strong>MEMORY AVAILABLE:</strong> 3890 MB<br />
  <strong>WEBCAM:</strong> I'm using my built-in webcam on my Satellite C660 TOSHIBA laptop.<br />
  <strong>COMPILER:</strong><br />
  <a href="https://i.stack.imgur.com/71CMP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/71CMP.png" alt="image description"></a>Microsoft Visual Studio Express 2012 for Windows Desktop<br />
  <a href="https://i.stack.imgur.com/71CMP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/71CMP.png" alt="image description"></a>Version 11.0.61030.00 Update 4 <br />
  <a href="https://i.stack.imgur.com/71CMP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/71CMP.png" alt="image description"></a>Microsoft .NET Framework <br />
  <a href="https://i.stack.imgur.com/71CMP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/71CMP.png" alt="image description"></a>Version 4.5.50948 <br />
  <strong>OpenCV Version:</strong> OpenCV V. 2.4.9, built for Windows, downloaded from <a href="https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.9/" rel="nofollow noreferrer">SourceForge</a>.<br />
  <strong>OUTPUT FROM</strong> <code>cv::getBuildInformation()</code><strong>:</strong> <a href="https://ucef145dc18ad51ef48fe68d2a58.dl.dropboxusercontent.com/cd/0/get/Ao7dHbETmGF3WUjUFNNaw0VcknuMX7o435wJvYgmYHW4jf4ElF53jnZsEHuwxes4cHxdEv03gUZAHBU-vwZQxwJ55nD7mRYLyhoS_VbdPlM-5Q/file?dl=1#" rel="nofollow noreferrer">OpenCV_BUILD.txt</a><br />
  <strong>Microsoft Visual Studio Project Property Sheet:</strong> <a href="https://pastebin.com/WD1H4Kha" rel="nofollow noreferrer">OpenCV Project Property Sheet</a><br />
  )</p>