This project demonstrates real-time background subtraction using the mean frame technique. It captures video from a webcam, computes the mean of the last 50 frames to estimate the background, and extracts the foreground by comparing the current frame with the computed background. A trackbar is provided to dynamically adjust the threshold for foreground detection.
- Real-time background subtraction using a webcam feed.
- Dynamic threshold adjustment using a trackbar.
- Efficient handling of frames using a rolling buffer (last 50 frames).
- Visualization of the original frame, computed background, and extracted foreground.
- Python: Core programming language.
- OpenCV: For video capture, image processing, and visualization.
- NumPy: For efficient array operations and mean computation.
Follow these steps to set up and run the project in VS Code:
Open a terminal in VS Code and run the following command to clone the repository:
git clone https://github.com/agarwal-prakhar/Background-Subtraction-Application.git
cd Background-Subtraction-ApplicationCreate and activate a virtual environment to manage dependencies:
python3 -m venv venv
source venv/bin/activatepython -m venv venv
venv\Scripts\activateInstall the required Python packages using pip:
pip install -r requirements.txtOpen the script Gausian_Mixture_Model.py , Frame_Differencing.py ,and others in VS Code and run it:
- Click the Run button in the top-right corner of VS Code.
- Alternatively, use the terminal to run the script:
python Gausian_Mixture_Model.py
python Frame_Differencing.py- A window will open showing the live video feed, computed background, and extracted foreground.
- Use the trackbar to adjust the threshold for foreground detection.
- Press the q key to exit the application.
- Ensure your webcam is connected and accessible. Test it using other applications like Zoom or your system's camera app.
- Make sure all dependencies are installed correctly. If you encounter issues, try reinstalling them:
pip install --upgrade opencv-python numpy- If you encounter indentation errors, ensure your editor is configured to use 4 spaces for indentation (not tabs).

