- Import the necessary libraries: You will need to import OpenCV and NumPy libraries in your Python code. You can install them using the following command:
pip install opencv-python numpy
- Read the input video: Use the cv2.VideoCapture() function to read the input video file into your Python code.
import cv2
# Read the input video
video = cv2.VideoCapture('input_video.mp4')
- Create the background model: Create an initial background model by reading the first few frames of the input video and averaging their pixel values. You can use the cv2.createBackgroundSubtractorMOG2() function to create the background model.
# Create the background model
background_model = cv2.createBackgroundSubtractorMOG2(history=50, varThreshold=25, detectShadows=False)
# Read the first few frames and update the background model
for i in range(50):
ret, frame = video.read()
if not ret:
break
foreground_mask = background_model.apply(frame)
- Apply background subtraction to each frame: Apply the background subtraction technique to each frame of the input video to get the foreground mask. The foreground mask will have a value of 255 for the foreground objects and 0 for the background.
while True:
# Read the frame from the input video
ret, frame = video.read()
if not ret:
break
# Apply background subtraction to the frame
foreground_mask = background_model.apply(frame)
# Morphological operations to remove noise and smooth the mask
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
foreground_mask = cv2.morphologyEx(foreground_mask, cv2.MORPH_OPEN, kernel)
foreground_mask = cv2.morphologyEx(foreground_mask, cv2.MORPH_CLOSE, kernel)
# Invert the mask to get the background
background = cv2.bitwise_not(frame, frame, mask=foreground_mask)
- Write the reconstructed video: Use the cv2.VideoWriter() function to write the reconstructed video to an output file.
# Define the output video codec and frame rate
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = int(video.get(cv2.CAP_PROP_FPS))
# Create the VideoWriter object
output_video = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame.shape[1], frame.shape[0]))
# Write the reconstructed video frame by frame
while True:
ret, frame = video.read()
if not ret:
break
foreground_mask = background_model.apply(frame)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
foreground_mask = cv2.morphologyEx(foreground_mask, cv2.MORPH_OPEN, kernel)
foreground_mask = cv2.morphologyEx(foreground_mask, cv2.MORPH_CLOSE, kernel)
background = cv2.bitwise_not(frame, frame, mask=foreground_mask)
output_video.write(background)
# Release the input and output video objects
video.release()