ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Understanding Background Subtraction in Python with OpenCV

author
Generated by
Krishna Adithya Gaddam

06/12/2024

background subtraction

Sign in to read full article

What is Background Subtraction?

Background subtraction is a technique used in computer vision to separate foreground objects from the background in video sequences. It’s particularly important for motion detection, surveillance systems, and human-computer interaction applications.

The basic idea is to maintain a model of the background, then compare each new frame captured by a camera against this model to identify moving objects or changes. When a change is detected, the corresponding pixels are marked as part of the foreground.

Understanding the Process

  1. Modeling the Background: The first step involves creating an accurate model of the background. This typically includes various methods to adapt to changes in lighting or environmental conditions.

  2. Foreground Detection: Once the background model is established, each incoming frame is analyzed for differences from the background model. The pixels that differ significantly from the model are marked as foreground.

  3. Post-processing: The detected foreground, often noisy, may go through morphological operations like erosion and dilation to enhance the results.

Background Subtraction Techniques in OpenCV

OpenCV provides several algorithms for background subtraction. The most notable ones include:

  • BackgroundSubtractorMOG2: A Gaussian Mixture-based background model that can handle shadows well.
  • BackgroundSubtractorKNN: A K-nearest neighbor-based background model that also accounts for foreground shadow detection.
  • BackgroundSubtractorMOG: An old version of the Gaussian Mixture model that can successfully extract foreground but may struggle with shadows.

Let’s dive deeper into how to implement these techniques in Python.

Installation

First, you need to make sure you have OpenCV installed. You can install it via pip if it’s not already on your system:

pip install opencv-python

Code Implementation

Here’s a simple example that demonstrates the use of BackgroundSubtractorMOG2.

import cv2 # Create a VideoCapture object to read from your camera (0 for the default camera) video_capture = cv2.VideoCapture(0) # Create a background subtractor object background_subtractor = cv2.createBackgroundSubtractorMOG2() while True: # Read a new frame ret, frame = video_capture.read() if not ret: break # Apply the background subtractor to get the foreground mask foreground_mask = background_subtractor.apply(frame) # Use morphological operations to clean up the mask kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) cleaned_mask = cv2.morphologyEx(foreground_mask, cv2.MORPH_OPEN, kernel) # Display the original frame and the cleaned mask side by side cv2.imshow('Original Frame', frame) cv2.imshow('Foreground Mask', cleaned_mask) # Exit if the 'q' key is pressed if cv2.waitKey(30) & 0xFF == ord('q'): break # Release the video capture object and close the OpenCV windows video_capture.release() cv2.destroyAllWindows()

Understanding the Code

  1. Capture Video: The VideoCapture object initializes the camera feed.
  2. Create Background Subtractor: cv2.createBackgroundSubtractorMOG2() initializes the background subtractor model.
  3. Processing Loop:
    • Each frame is extracted from the video stream.
    • The background subtractor is applied, resulting in a foreground mask showing movement.
    • Morphological operations are employed to remove noise and enhance the classification of the foreground.
  4. Display the output: The original frame and the processed foreground mask are displayed using OpenCV’s imshow.

Enhancements with Other Methods

You can swap BackgroundSubtractorMOG2 with BackgroundSubtractorKNN to see how the output varies:

background_subtractor = cv2.createBackgroundSubtractorKNN()

This change allows you to experiment with different algorithms to see which best suits your application. Each algorithm has its strengths and weaknesses, so understanding the context of your application will help you choose the right one.

Conclusion

As you can see, background subtraction is an accessible yet powerful tool in the realm of computer vision. With just a few lines of Python code, you can detect motion within a video stream and manipulate that data for further analysis or applications.

Whether it’s for surveillance, improving video calls, or engaging interactive applications, mastering these techniques opens up a vast array of opportunities in the exciting field of computer vision. Keep experimenting with different parameters and models to fine-tune your results even further!

Popular tags

background subtractionOpenCVcomputer vision

Share now!

Like & bookmark

Related collections

  • Python with MongoDB: A Practical Guide

    08/11/2024 · Python

  • Python with Redis Cache

    08/11/2024 · Python

  • Advanced Python Mastery: Techniques for Experts

    15/01/2025 · Python

  • Python Basics: Comprehensive Guide

    21/09/2024 · Python

  • TensorFlow Mastery: From Foundations to Frontiers

    06/10/2024 · Python

Related articles

  • Exploring Machine Learning with OpenCV in Python

    06/12/2024 · Python

  • Crafting Custom Named Entity Recognizers in spaCy

    22/11/2024 · Python

  • Automating File Management with Python

    08/12/2024 · Python

  • Unlocking Motion Analysis

    06/12/2024 · Python

  • Optimizing Redis Performance with Python

    08/11/2024 · Python

  • Threading and Concurrency in Python

    13/01/2025 · Python

  • Generators and Coroutines

    13/01/2025 · Python

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design