At first, everything worked fine - although we'd hard-coded the colour range to look for, we figured this could easily be made into a parameter. But during the course of the day, our colour detection started to behave differently.
At the start of the day, the detection routine correctly placed the centre-point right in the middle of the object it was tracking. But as the day wore on, the centre-point slowly drifted south! When it became noticeable, we looked at the raw input image and discovered that as the sunlight had shifted, coming into the room, it was casting shadows in different places on our tracking object.
We needed a way of filtering out the shadows.
As we can't control the light conditions of the room in which our object tracking will ultimately be used, it essentially means not using visible light. Which, of course, means IR (infrared).
Cheap webcams are great for tracking infrared light.
Most cameras on mobile phones show infrared light - if you shine a remote control at your iPhone camera, you can see it blinking on and off. But some webcams don't. That's because they're usually fitted with an infrared filter, to try to maintain the right colour balance, using different light sources.
Really cheap webcams simply use a cover over the sensor to take out infrared from any incoming image. It's this idea that we're going to exploit, to make our webcam see infrared.
Here's our really cheap webcam (about £6 off Amazon). We took off the lens to expose the image sensor. Shock horror, no IR filter....
Not to worry - it was fixed over the lens.
Simply removing the IR filter means that our webcam can now see infrared light. But that's only half the story. Seeing previously invisible light means we've actually got more content to track, not less. What we really need, now we can see the invisible IR light, is to remove all the visible light entering the webcam sensor.
You can use polaroid/polarising filters, but just as good (and more easily sourced) is developed camera film. Remember when you used to load a film into your camera, take your snaps, then have them developed at a chemist or camera shop? Well, it's that sort of film.
What we need is just a piece of photography film, fully opaque (black) and developed. The easiest way to achieve this is to get a black-and-white film, pull a length of it out and hold it out in the sunshine for about five minutes. Then take the film to be developed the "old school way".
We had to tell the developing company that we didn't actually want the photos (they'd just be bright white pieces of paper anyway) and to develop whatever was on the film - some chemists might return your film with "error" or some comment about it being faulty. Pre-warning them that the pictures are actually junk means that they don't waste time trying to put right something that isn't there!
Then we simply cut a small square of developed film and used it in place of the IR filter.The result? Our webcam can see absolutely nothing. An entirely black image appeared on the screen. But when we flashed an IR LED in front of it....