This part of the tutorial is also very simple to do, thanks to OpenCV.
A background subtractor, as its name sugests, lets you identify the foreground and background of and image. A background is considered to be as anything constant in a series of images, anything that stays static. The foreground is everything that changes (moves).
Doing background substraction in OpenCV onlyt requires 2 lines:
Running this code:
In the new image black represents the background, white are objects in the foreground and gray are shadows cast by those objects.
The good thing about using the MOG2 substractor in OpenCV is that the background is constantly being calculated, meaning that subtle changes in lighting (such as those caused by the Sun) won´t affect your calculations over time.
This is really the first step in making a people counter. Hope you like it.
Next, we’ll clean the image produced by the substractor to be able to use it in the actual counting.