Created
April 9, 2020 05:53
-
-
Save GenevieveBuckley/15599c8bd75a38cba4ff34f22393aad6 to your computer and use it in GitHub Desktop.
OpenCV background subtraction example - https://github.com/opencv/opencv/blob/3.4/samples/python/tutorial_code/video/background_subtraction/bg_sub.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import cv2 as cv | |
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 = cv.createBackgroundSubtractorMOG2() | |
else: | |
backSub = cv.createBackgroundSubtractorKNN() | |
## [create] | |
## [capture] | |
capture = cv.VideoCapture(cv.samples.findFileOrKeep(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 | |
cv.rectangle(frame, (10, 2), (100,20), (255,255,255), -1) | |
cv.putText(frame, str(capture.get(cv.CAP_PROP_POS_FRAMES)), (15, 15), | |
cv.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0)) | |
## [display_frame_number] | |
## [show] | |
#show the current frame and the fg masks | |
cv.imshow('Frame', frame) | |
cv.imshow('FG Mask', fgMask) | |
## [show] | |
keyboard = cv.waitKey(30) | |
if keyboard == 'q' or keyboard == 27: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment