https://github.com/qengineering/fast-background-substraction
Ultra fast background substraction
https://github.com/qengineering/fast-background-substraction
background-subtraction cpp jetson-nano linux opencv raspberry-pi windows x86-64
Last synced: 7 months ago
JSON representation
Ultra fast background substraction
- Host: GitHub
- URL: https://github.com/qengineering/fast-background-substraction
- Owner: Qengineering
- License: bsd-3-clause
- Created: 2021-08-04T16:22:16.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-12-07T11:41:23.000Z (about 4 years ago)
- Last Synced: 2025-04-13T18:55:23.535Z (10 months ago)
- Topics: background-subtraction, cpp, jetson-nano, linux, opencv, raspberry-pi, windows, x86-64
- Language: C++
- Homepage:
- Size: 7.49 MB
- Stars: 11
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fast Background Substraction
https://user-images.githubusercontent.com/44409029/128219655-52ff1c72-8ad9-4323-a0a4-0eba52268144.mp4
## A fast background substraction in C++
[](https://opensource.org/licenses/BSD-3-Clause)
This C++ application filters the background from a static image. It's written for a Raspberry Pi but can be used on any machine with OpenCV. It's a fast algorithm to detect moving objects in an image given a static background. The available OpenCV routines were too slow for our Raspberry Pi Zero, hence this faster solution.
The given times are with a Raspberry Pi 4 with a 32-bit OS, running at 1500 MHz.
------------
## Dependencies.
To run the application, you have to:
- OpenCV installed. [Install OpenCV 4.5](https://qengineering.eu/install-opencv-4.5-on-raspberry-64-os.html)
- Code::Blocks installed. (`$ sudo apt-get install codeblocks`)
------------
## Installing the app.
To extract and run the application in Code::Blocks
$ mkdir *MyDir*
$ cd *MyDir*
$ wget https://github.com/Qengineering/Fast-Background-Substraction/archive/refs/heads/main.zip
$ unzip -j master.zip
Remove master.zip, LICENSE and README.md as they are no longer needed.
$ rm master.zip
$ rm LICENSE
$ rm README.md
Your *MyDir* folder must now look like this:
768x576.avi (example movie)
Background.cbp (code::blocks project file)
MainQeng.cpp (C++ source file with the new algorithm)
MainMOG2.cpp (C++ source file with exisiting OpenCV algorithms)
------------
## Running the app.
To run the application load the project file Background.cbp in Code::Blocks.
You can load and run either MainQeng.cpp or MainMOG2.cpp in the IDE.
The code speaks for itself. Here the bare bones.
### Qeng
```
//get black/white
cv::cvtColor(frame, bw_frame, COLOR_BGR2GRAY);
//go to float
bw_frame.convertTo(fImage, CV_32FC1);
//calculate Tau
f=Tcnt; f/=Xcnt;
Tcnt--; if(Tcnt<1) Tcnt=1;
//weighted background
cv::addWeighted(fImage, f, sum_frame, (1.0-f), 0.0, sum_frame);
//substrac current image from static background
cv::subtract(sum_frame,fImage,sub_frame);
//get a 8 bit black and white image
cv::convertScaleAbs(sub_frame,finalImage,1.5);
```
### MOG2
```
//create Background Subtractor objects
Ptr pBackSub;
pBackSub = createBackgroundSubtractorMOG2();
....
//update the background model
pBackSub->apply(frame, finalImage);
```
### KNN
```
//create Background Subtractor objects
Ptr pBackSub;
pBackSub = createBackgroundSubtractorKNN();
....
//update the background model
pBackSub->apply(frame, finalImage);
```
------------
## Tau
Our algorithm adapts an average background over time. Every single frame updates this `sum_frame` a little bit. The adaptation is needed for slowly changing (lighting) conditions like moving shadows. It will also remove static objects from the scene. A parked car will vanish after some time in the background. Until, of course, it drives away. The constant Tau defines the adaption rate. The higher this number, the faster something disappears in the background.
Rule of the thumb: **T = 5/(Tau * FPS)**
Given an FPS (Frames Per Second) of 30 and a Tau of 0.05, an object will completely vanish in the background in 3.3 Sec.
The given percentage is the ratio of white pixels, in other words, the moving object.
Because the algorithm works with grey values, it is sensitive to contrast. A person wearing a green sweater on the green grass will give a not that high response as the same person walking on the street. However, we gladly pay this small price for the speed the routine achieves.
------------
## RaspiCam.
If you want to use a camera please alter line 253 in main.cpp to
`cv::VideoCapture cap(0); //RaspiCam`
If you want to run a movie please alter line 253 in main.cpp to
`cv::VideoCapture cap( "768x576.avi"); //Movie`
------------
## Papers.
- Zoran Zivkovic and Ferdinand van der Heijden. Efficient adaptive density estimation per image pixel for the task of background subtraction. Pattern recognition letters, 27(7):773–780, 2006. [link](https://www.academia.edu/33302633/Efficient_adaptive_density_estimation_per_image_pixel_for_the_task_of_background_subtraction)
- Zoran Zivkovic. Improved adaptive gaussian mixture model for background subtraction. In Pattern Recognition, 2004. ICPR 2004. Proceedings of the 17th International Conference on, volume 2, pages 28–31. IEEE, 2004. [link](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.1.4658)
------------
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CPZTM5BB3FCYL)