Background Subtraction VS Frame Differencing

Background Subtraction

  1.     // Create MOG Background Subtractor object
  2.     pMOG= cv::bgsegm::createBackgroundSubtractorMOG();//new BackgroundSubtractorMOG();
  3.  
  4.     // Create MOG2 Background Subtractor object
  5.     pMOG2 = createBackgroundSubtractorMOG2(20, 16, true);//new BackgroundSubtractorMOG2();
  6.  
  7.         // Capture the current frame
  8.         cap >> frame;
  9.  
  10.         // Resize the frame
  11.         resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
  12.  
  13.         // Update the MOG background model based on the current frame
  14.         pMOG->apply(frame, fgMaskMOG);
  15.  
  16.         // Update the MOG2 background model based on the current frame
  17.         pMOG2->apply(frame, fgMaskMOG2);
  18.  
  19.         // Show the current frame
  20.         //imshow("Frame", frame);
  21.  
  22.         // Show the MOG foreground mask
  23.         //imshow("FG Mask MOG", fgMaskMOG);
  24.  
  25.         // Show the MOG2 foreground mask
  26.         imshow("FG Mask MOG 2", fgMaskMOG2);

Frame Differencing

  1. Mat frameDiff(Mat prevFrame, Mat curFrame, Mat nextFrame)
  2. {
  3.     Mat diffFrames1, diffFrames2, output;
  4.  
  5.     // Compute absolute difference between current frame and the next frame
  6.     absdiff(nextFrame, curFrame, diffFrames1);
  7.  
  8.     // Compute absolute difference between current frame and the previous frame
  9.     absdiff(curFrame, prevFrame, diffFrames2);
  10.  
  11.     // Bitwise "AND" operation between the above two diff images
  12.     bitwise_and(diffFrames1, diffFrames2, output);
  13.  
  14.     return output;
  15. }

Leave a Reply

Your email address will not be published. Required fields are marked *