#OpenCV – Background Subtraction in a camera feed 🎦 using MOG2

Hi !

Yesterday I wrote a post about how to remove the background from my camera feed. To remove the background, I used a python library “cvzone”.

While talking with some friends, someone asked about using some of the out-of-the-box features included in OpenCV, like MOG2 or KNN. So I read a little about them and I get this sample up and running.

OpenCV Remove Background from Camera using MOG2

As you can see the output is completely different than before, and not even close to what I’m looking for. And it makes sense, the official OpenCV documentation explains how this method works here: [How to Use Background Subtraction Methods].

Background subtraction (BS) is a common and widely used technique for generating a foreground mask (namely, a binary image containing the pixels belonging to moving objects in the scene) by using static cameras.

As the name suggests, BS calculates the foreground mask performing a subtraction between the current frame and a background model, containing the static part of the scene or, more in general, everything that can be considered as background given the characteristics of the observed scene.

Background_Subtraction_Tutorial_Scheme.png

Background modeling consists of two main steps:

  1. Background Initialization;
  2. Background Update.

In the first step, an initial model of the background is computed, while in the second step that model is updated in order to adapt to possible changes in the scene.

And the sample code:

# Copyright (c) 2022
# Author : Bruno Capuano
# Create Time : 2022 June
# Change Log :
# – Open a local camera feed using OpenCV
# – Show the original camera feed and the background removed feed using MOG2
# – Reference: https://docs.opencv.org/4.x/d1/dc5/tutorial_background_subtraction.html
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import cv2
backSub_mog = cv2.createBackgroundSubtractorMOG2()
# open camera
cap = cv2.VideoCapture(1)
while True:
# read image
ret, img = cap.read()
#resize office to 640×480
img = cv2.resize(img, (320, 240))
imgNoBg = backSub_mog.apply(img)
# show both images
cv2.imshow('office',img)
cv2.imshow('office no bg MOG2',imgNoBg)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# close camera
cap.release()
cv2.destroyAllWindows()

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.


Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: