a. The average method
I Have created this method taking as a fact that we could compute the number of pixels that have changed between to frames. To do movement detection the basic idea is to query a frame then query another and compare them two to find the difference. So this method works as follow:
- A first frame is taken. All the following frames are compared with the previous one using the Absdiff function
- The result is an image we more the two pixel difference is high more the pixel is white
- Then multiples operations are applied to reduce the noise and refine the moving objects
- Smooth: Intent to delete the noise in the image
- MorphologyEx: OPEN and then CLOSE to get more smooth shapes
- To finish a threshold is applied so that we get a nice shapes of moving object in black
- Then to decide either something has moved or not, a threshold is set (by default 5%). So if more than 5% of pixels are black and so have changed a message is triggered
- To calculate the percentage of black pixels we have to iterate the entire matrix, count the number of black pixels and calculate the percentage with the total number of pixels
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
After Absdiff:
After treatment:
b. Adaptative method
The second method inspired from an OpenCV book is a prety eficient method that learn the background overtime. It means that if an object come in the scene it is firstly considered as a foreground element. And become a background element overtime. Basically background frames are acumulate into an image that constitute the background. The example below use a video as input but you can switch it to your webcam.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
The video used is the same than the previous chapter one (with the small piece of plastic crossing the screen).
<<Tracking Moving Objects | Home | Officials OpenCV Python Samples>>