This project implements a custom Gaussian Mixture Model (GMM) for background subtraction in images and videos. GMM is a probabilistic model that represents a mixture of multiple Gaussian distributions, commonly used for modeling pixel distributions in background subtraction tasks.
This implementation focuses on using a GMM for background subtraction in visual data. The primary steps include:
- Building the GMM Class: Implements GMM from scratch with methods for initialization, expectation-maximization (E-step and M-step), and prediction.
- Video Frame Processing: Helper functions to display video frames or save them to a file, enabling analysis and visualization of GMM-based background subtraction.
- Application to Background Subtraction: Uses pixel distributions modeled by GMM to separate foreground (moving objects) from the background.
To run this project, you will need the following dependencies:
pip install numpy matplotlib opencv-python
-
Implement the GMM:
- The
GMM
class provided inBackground Subtraction.ipynb
contains methods for GMM-based background subtraction.
- The
-
Run the Notebook:
- Execute the notebook to fit the GMM model to visual data. The notebook includes:
- Class definitions for the GMM.
- Helper functions for frame display and saving.
- Execute the notebook to fit the GMM model to visual data. The notebook includes:
-
Display and Save Frames:
- The helper functions
display_frames
andsave_frames
help visualize and save the background subtraction results.
- The helper functions
# Instantiate and fit GMM
gmm = GMM(n_components=3, tol=1e-4, max_iter=100)
gmm.fit(data)
# Display results as video
display_frames(frames, fps=10)
# Save results
save_frames(frames, fps=10, output_path='./results', file_name='background_subtraction_result')
Background Substraction.ipynb # Main notebook with GMM implementation and example usage
README.md # Project README file
Example output showing background subtraction results using GMM and averaging:
Background Subtraction Results - Frame Averaging
Background Subtraction Results - GMM
- Gaussian Mixture Models - scikit-learn
- Understanding GMMs - YouTube Video 1
- Introduction to GMMs - YouTube Video 2