Skip to main content

Python | Background Subtraction using OpenCV



 Background subtraction is any technique that allows an image's foreground to be extracted for further processing


Many applications do not need to know everything about the evolution of movement in a video sequence, but only require the information of changes in the scene, because an image's regions of interest are objects(humans, cars, text, etc) in its foreground. After the stage of image processing(which may include image denoising, post-processing like morphology, etc.) object localization is required which may make use of this technique


So let's create this most valuable project with python so easily.


Modules:

  1. OpenCV
  2. Numpy

Code:

import numpy as np
import cv2

cap = cv2.VideoCapture('walking.avi')

# Initlaize background subtractor
foreground_background = cv2.createBackgroundSubtractorMOG2()

while True:
   
    ret, frame = cap.read()

    # Apply background subtractor to get our foreground mask
    foreground_mask = foreground_background.apply(frame)

    cv2.imshow('Output', foreground_mask)
    if cv2.waitKey(1) == 13:
        break

cap.release()
cv2.destroyAllWindows()



Note:

You can get an error for the file so if you want the file and also the code then click here

Follow my socials:

Youtube:https://www.youtube.com/channel/UCU1qNFntn7dCi9uqyvrGKOg

Instagram: https://www.instagram.com/python.math/

Twitter:https://twitter.com/Pritish369


For any question or coding discussion, you can join my discord or telegram:

Discord:https://discord.gg/be7MmSuV

Telegram:https://t.me/Python_Math_Community

Comments

Popular posts from this blog

Age and Gender Detection In Python - Creation Code

Gender Identity: Gender identity is the personal sense of one's own  gender . Gender identity can correlate with a person's  assigned sex  or can differ from it. In most individuals, the various biological determinants of sex are congruent and consistent with the individual's gender identity.  Gender expression  typically reflects a person's gender identity, but this is not always the case. While a person may express behaviors, attitudes, and appearances consistent with a particular  gender role , such expression may not necessarily reflect their gender identity. The term gender identity was coined by  Robert J Stoller  in 1964. Modules and assets: In this project, we will use the assetsOpenCV library which will help us to play with the camera  We also use some datasets to identify the faces and ages you can find those datasets  here Explanation: We will first train our models test.py import cv2 def faceBox ( net , frame , conf_thresho...

Create Ping Pong Game in Python

  Ping Pong Game: Table tennis , also known as  ping-pong  and  whiff-whaff , is a sport in which two or four players hit a lightweight ball, also known as the ping-pong ball, back and forth across a table using small rackets. The game takes place on a hard table divided by a net. Except for the initial serve, the rules are generally as follows: players must allow a ball played toward them to bounce once on their side of the table and must return it so that it bounces on the opposite side at least once. A point is scored when a player fails to return the ball within the rules. Play is fast and demands quick reactions. Spinning the ball alters its trajectory and limits an opponent's options, giving the hitter a great advantage. We can make it using pygame but I keep it more simple we will create this game using only the turtle module so let's drive into the code without wasting any time Code: # Import required library import turtle # Create screen sc = turtle . Scre...

How To Draw BMW Logo - In Python

 I know I don't need to introduce BMW as it is a very popular luxury car. Today we gonna draw the BMW logo in python. I know that you can draw it using a pencil and other tools like AutoCAD etc. But we are programmers we talk with computers so let's tell our computer to draw this logo for use with the help of python. Module The only module we will use is that turtle Code: import turtle as t t.begin_fill() t.fillcolor( '#008ac9' ) for i in range ( 50 ):     t.forward( 4 )     t.left( 2 ) t.right(- 80 ) t.forward( 116 ) t.right(- 90 ) t.forward( 132 ) t.end_fill() t.penup() t.pendown() t.right( 90 ) for i in range ( 50 ):     t.forward( 4 )     t.left(- 2 ) t.right( 80 ) t.forward( 116 ) t.forward(- 116 ) t.right( 90 ) t.begin_fill() t.fillcolor( '#008ac9' ) for j in range ( 45 ):     t.forward(- 4 )     t.left(- 2 ) t.right(- 90 ) t.forward( 116 ) t.end_fill() t.right( 180 ) t.forward( 116 ) t.right( 90 ) for i in ...