In this article, we discuss the concept of a moving average . The moving average of the function is used to separate the foreground from the background. In this concept, a video sequence is analyzed over a specific set of frames. During this sequence of frames, a moving average is calculated for the current frame and previous frames. This gives us a background model, and any new object introduced during the video sequence becomes part of the foreground. Then the current frame contains the newly entered object with the background. It then calculates the absolute difference between the background model (which is a function of time) and the current frame (which is the newly entered object). The moving average is calculated using the equation below:
- A working webcam or camera module for input.
- Download Python 3.x, Numpy and OpenCV 2.7.x versions. Check if your OS is 32-bit or 64-bit compatible and install accordingly.
- Check the status of NumPy and OpenCV
How does the moving average method work?
The purpose of the program is to detect active objects from the difference obtained from the keyframe and the current frame. We continue to feed each frame to this function, and the function continues to find the average of all frames. We then calculate the absolute difference between frames.
Function used — cv2.accumulate>Weighted () .
cv2.accumulateWeighted (src, dst, alpha)
Parameters passed to this function:
- CSI: Original image. The image can be color or grayscale, or 8-bit or 32-bit floating point.
- dst : accumulator or target image. It is 32 bit or 64 bit floating point.
NOTE. It should have the same channels as the original image. In addition, the dst value must be pre-declared initially.
- alpha : The weight of the input image. Alpha decides the update rate. If you set a smaller value for this variable, the moving average will be performed for more previous frames and vice versa.
As we can see below, the hand is blocking the background image.
T Now we shake the foreground object, i.e. our hand. We start to wave our hand.
The Moving Average shows the background below, and the Moving Average with alpha 0.02 perceives it as a transparent hand with the main emphasis on the background.
Alternatively we can use cv.RunningAvg () for that same task with parameters that have the same meaning as cv2.accumulateweighted () parameters.
cv.RunningAvg (image, acc, alpha)
- https://docs.opencv.org/3.2.0/d1/ dc5 / tutorial_background_subtraction.html