title: OpenCV MOG2 Foreground Extraction
body: <p>I am new to opencv and I am currently working on a project to extract the background and foreground from a video using MOG2.</p>
  <p>I have been able to extract the background but I am having some trouble with the Foreground.</p>
  <p>Here's my code:</p>
  <pre><code>import numpy as np
  import cv2
  
  
  cap = cv2.VideoCapture('capture.avi')
  fgbg = cv2.createBackgroundSubtractorMOG2()
  
  while True:
          _, frame = cap.read()
  
          if frame is None:
              break
  
          fgMask = fgbg.apply(frame)
          bg = fgbg.getBackgroundImage(frame)
  
          fg = cv2.absdiff(frame, bg)
  
          cv2.imshow('frameMask', fgMask)
          cv2.imshow('Background', bg)
          cv2.imshow('Foreground', fg)
  
  </code></pre>
  <p>I can get the <code>background</code> and <code>frameMask</code> correctly, but the foreground is completely black</p>
  <p><img src="https://i.stack.imgur.com/ImcFa.jpg" alt="enter image description here" /></p>
  <p><a href="https://i.stack.imgur.com/YWgC9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YWgC9.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/yzDnL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yzDnL.png" alt="enter image description here" /></a></p>