Track a color is a pretty interesting and useful feature that OpenCV allow to do via multiples operations. The central function is InRangeS, it takes to values and all the pixels whose value is within the two values is set to white and all the others to black. At this point we have a picture where all pixels that match the wanted color are white.
Another important point is that we do not use the BGR or RGB colorspace because the pixel value is highly dependant of the luminosity and few others factors so to get the better result, the image is firstly converted to the HSV colorspace (hue, saturation, and intensity).
Then to compare a frame and the next we have to use the moments functionnalities that allow to calculate the Spatial moment and get the central point. Finally a line can be easily drawn between the current central point and the one of the previous frame.
Result :
Thresholded image (with InRangeS):
The Code:
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 50 51 52 53 54 55 56 57 58 59 |
|
This source file is also hosted on github.
<<Officials OpenCV Python Samples | Home | Motion detection>>