title: How to get moving object's mask using OpenCV BackgroundSubtractorMOG2
body: <p>I want to mask the moving objects from video.
   I found that OpenCV has some built-in BackgroundSubtractors which could possibly saving my time a lot. However, according to the <a href="http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html?highlight=background#BackgroundSubtractorMOG2%20:%20public%20BackgroundSubtractor" rel="nofollow noreferrer">official reference</a>, the function:</p>
  
  <pre><code>void BackgroundSubtractorMOG2::operator()(InputArray image, OutputArray fgmask, double learningRate=-1)
  </code></pre>
  
  <p>should output a mask, fgmask, but it doesn't. The fgmask variable will contain the "contour of the mask" instead after invoking above method. That's weird. All I want is a simple closed region filled with white color(for example) to represent the moving objects. How could I do that?</p>
  
  <p>Any reply or recommendation would be very appreciate. Thanks a lot.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      cv::BackgroundSubtractorMOG2 bg = BackgroundSubtractorMOG2(30,16.0,false);
      cv::VideoCapture cap(0);
      cv::Mat frame, mask, _frame, _fmask;
      cvNamedWindow("mask", CV_WINDOW_AUTOSIZE);
      for(;;)
      {
          cap &gt;&gt; frame;
          bg(frame,fmask,-1);
  
          _frame = IplImage(frame);
          _fmask = IplImage(fmask);
  
          cvShowImage("mask", &amp;_fmask);
          if(cv::waitKey(30) &gt;= 0) break;
      }
      return 0;
  }
  </code></pre>
  
  <p>A snapshot of the output video is:
  <img src="https://i.stack.imgur.com/hkN67.png" alt="enter image description here"></p>
  
  <p>p.s. My working environment is OpenCV2.4.3 on OSX 10.8 and XCode 4.5.2 with apple LLVM compiler 4.1.</p>