title: Python opencv background subtraction
body: <p>What I need to do is fairly simple:</p>
  <ol>
  <li>load a 5 frames video file</li>
  <li>detect background</li>
  <li>On every frames, one by one :
  <ol>
  <li>subtract background (create foreground mask)</li>
  <li>do some calculations on foreground mask</li>
  <li>save both original frame and foreground mask</li>
  </ol>
  </li>
  </ol>
  <p>Just to see the 5 frames and the 5 corresponding fgmasks :</p>
  <pre><code>import numpy as np
  import cv2  
  cap = cv2.VideoCapture('test.avi')
  fgbg = cv2.BackgroundSubtractorMOG()
  
  while(True):
      # Capture frame-by-frame
      ret, frame = cap.read()
  
      fgmask = fgbg.apply(frame)
      # Display the fgmask frame
      cv2.imshow('fgmask',fgmask)
      # Display original frame
      cv2.imshow('img', frame)
  
      k = cv2.waitKey(0) &amp; 0xff
      if k == 5:
          break
  
  cap.release()
  cv2.destroyAllWindows()
  </code></pre>
  <p>Every frame gets opened and displayed correctly but the showed fgmask do not correspond to the showed original frame. Somewhere in the process, the order of the fgmasks gets mixed up.</p>
  <p>The background does get subtracted correctly but I don't get the 5 expected fgmasks.</p>
  <p>What am I missing ? I feel like this should be straightforward : the while loop runs over the 5 frames of the video and fgbg.apply apply the background subtraction function to each frame.</p>
  <p>OpenCV version that I use is <code>opencv-2.4.9-3</code></p>