How does python OpenCV use the background separation method?

王林
Release: 2023-05-16 21:09:15
forward
1271 people have browsed it

Theory

  • 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:

    • ##1. Background initialization

    • 2. Background update The first step is to calculate the initial model of the background. In the second step, the model is updated to adapt to possible changes in the scene

Implementation

Let the user choose to process video files or image sequences. In this example,

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
Copy after login

Code analysis

Analyze the main part of the above code:

  • ##cv2.BackgroundSubtractor

    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()
    Copy after login
  • cv2.VideoCapture

    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)
    Copy after login
  • Each frame is used to calculate the foreground mask and update the background

    . 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)
    Copy after login
    The current frame number can be extracted from the
  • cv2.Videocapture

    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))
    Copy after login
    Display the current input frame and results
  • # show the current frame and the fg masks
        cv2.imshow('Frame', frame)
        cv2.imshow('FG Mask', fgMask)
    Copy after login

    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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template