Background subtraction is a technique for separating out foreground elements from the background and is done by generating a foreground mask. This technique is used for detecting dynamically moving objects from static cameras. Background subtraction technique is important for object tracking. There are several techniques for background subtraction
In this article, we discuss the concept of Running Average. The running average of a function is used to separate foreground from background. In this concept, the video sequence is analyzed over a particular set of frames. During this sequence of frames, the running average over the current frame and the previous frames is computed. This gives us the background model and any new object introduced in the during the sequencing of the video becomes the part of the foreground. Then, the current frame holds the newly introduced object with the background. Then the computation of the absolute difference between the background model (which is a function of time) and the current frame (which is newly introduced object) is done. Running average is computed using the equation given below :
- A working web camera or a camera module for input.
- Download Python 3.x, Numpy and OpenCV 2.7.x version. Check if your OS is either 32 bit or 64 bit compatible and install accordingly.
- Check the running status of numpy and OpenCV
How Running Average method works?
The objective of the program is to detect active objects from the difference obtained from the reference frame and the current frame. We keep feeding each frame to the given function, and the function keeps finding the averages of all frames. Then we compute the absolute difference between the frames.
The function used is cv2.accumulateWeighted().
cv2.accumulateWeighted(src, dst, alpha)
The parameters passed in this function are :
- src: The source image. The image can be colored or grayscaled image and either 8-bit or 32-bit floating point.
- dst: The accumulator or the destination image. It is either 32-bit or 64-bit floating point.
NOTE: It should have the same channels as that of the source image. Also, the value of dst should be predeclared initially.
- alpha: Weight of the input image. Alpha decides the speed of updating. If you set a lower value for this variable, running average will be performed over a larger amount of previous frames and vice-versa.
As we can see clearly below, the hand blocks the background view.
Now, we shake the foreground object i.e. our hand. We begin waving our hand.
The Running average shows the background clearly below, Running Average with alpha 0.02 has caught it as a transparent hand, with main emphasis on the background
Alternatively, we can use the cv.RunningAvg() for the same task with the parameters having the same meaning as that of the parameters of the cv2.accumulateweighted().
cv.RunningAvg(image, acc, alpha)
- Python | Background subtraction using OpenCV
- Project Idea | Motion detection using Background Subtraction Techniques
- How to change the background image using jQuery ?
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Running previous command with sudo
- Wrap Around Concept and TCP Sequence Number
- Median Of Running Stream of Numbers - (using Set)
- How to make sure that Apache service keeps running in Ubuntu
- Basic Concept of Classification (Data Mining)
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- Image Processing in Java | Set 7 (Creating a random pixel image)
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- Full screen OpenCV / GtK application in C++ running on Raspberry PI
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.