Background Separation (BS) is a common technique for generating a foreground mask (a binary image containing pixels belonging to moving objects in the scene) by using a static camera
As the name suggests, BS calculates the foreground mask, performing a subtraction operation between the current frame and the background model, which contains the static part of the scene, which can be viewed as as background for everything.
Background modeling includes two main steps:
cv2.BackgroundSubtractorMOG2 will be used to generate the foreground mask.
from __future__ import print_function import cv2 import argparse parser = argparse.ArgumentParser( description='This program shows how to use background subtraction methods provided by OpenCV. You can process both videos and images.') parser.add_argument('--input', type=str, help='Path to a video or a sequence of image.', default='vtest.avi') parser.add_argument('--algo', type=str, help='Background subtraction method (KNN, MOG2).', default='MOG2') args = parser.parse_args() ## [create] # create Background Subtractor objects if args.algo == 'MOG2': backSub = cv2.createBackgroundSubtractorMOG2() else: backSub = cv2.createBackgroundSubtractorKNN() ## [create] ## [capture] capture = cv2.VideoCapture(args.input) if not capture.isOpened(): print('Unable to open: ' + args.input) exit(0) ## [capture] while True: ret, frame = capture.read() if frame is None: break ## [apply] # update the background model fgMask = backSub.apply(frame) ## [apply] ## [display_frame_number] # get the frame number and write it on the current frame cv2.rectangle(frame, (10, 2), (100,20), (255,255,255), -1) cv2.putText(frame, str(capture.get(cv2.CAP_PROP_POS_FRAMES)), (15, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0)) ## [display_frame_number] ## [show] # show the current frame and the fg masks cv2.imshow('Frame', frame) cv2.imshow('FG Mask', fgMask) ## [show] keyboard = cv2.waitKey(30) if keyboard == 'q' or keyboard == 27: break
The object will be used to generate the foreground mask code. In this example, default parameters are used, but specific parameters can also be declared in the create
function.
# create Background Subtractor objects KNN or MOG2 if args.algo == 'MOG2': backSub = cv2.createBackgroundSubtractorMOG2() else: backSub = cv2.createBackgroundSubtractorKNN()
Object is used to read input video or input image sequence
capture = cv2.VideoCapture(args.input) if not capture.isOpened: print('Unable to open: ' + args.input) exit(0)
. If you want to change the learning rate used to update the background model, you can set a specific learning rate by passing arguments to the apply method
# update the background model fgMask = backSub.apply(frame)
object and punched in the upper left corner of the current frame. Use a white rectangle to highlight the black frame number
# get the frame number and write it on the current frame cv2.rectangle(frame, (10, 2), (100,20), (255,255,255), -1) cv2.putText(frame, str(capture.get(cv2.CAP_PROP_POS_FRAMES)), (15, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))
# show the current frame and the fg masks cv2.imshow('Frame', frame) cv2.imshow('FG Mask', fgMask)
The above is the detailed content of How does python OpenCV use the background separation method?. For more information, please follow other related articles on the PHP Chinese website!