Color detection

Right, first of all I’d like to say Hi since I’m new to the forums.

So to get to the point, I need to substract a certain color out of an image and store it inside another image.

I know this can be done by using Bitmaps and BitmapData objects. However I have a problem using it since, in my case, I need to substract a color out of a live feed … to be more specific a webcam image.

What I’m trying to do is filter out 1 color (range) so I can track it, in other words using a color for motion tracking. The problem with webcam images is that 1 color will not stay the same color, since there’s brightness, contrast etc to keep in mind.

Having said this I would like to know if there’s a way to filter out a range of colors out of an image. (for example: I want to track something which is bright blue, but there are a lot of bright blue tones)

The only thing I found and could understand was the getColorBounds() method for BitmapData. For example (I’ve taken this out of the livedocs)


var bmd:BitmapData = new BitmapData(80, 40, false, 0xFFFFFF);
var rect:Rectangle = new Rectangle(0, 0, 80, 20);
bmd.fillRect(rect, 0xFF0000);

var maskColor:uint = 0xFFFFFF; 
var color:uint = 0xFF0000;  
var redBounds:Rectangle = bmd.getColorBoundsRect(maskColor, color, true);
trace(redBounds); // (x=0, y=0, w=80, h=20)

var notRedBounds:Rectangle = bmd.getColorBoundsRect(maskColor, color, false);
trace(notRedBounds); // (x=0, y=20, w=80, h=20)


The problem with this it only uses a fixed color, while I need a range of colors.

I must say I am very new to image manipulation (only started out a couple of days ago). I did read up on bitwise operators and Color Matrix (and filters) but I must admit it’s a bit much for me at the moment. Any help or tips regarding this would be greatly appreciated.

P.S. : maybe to make things a bit more clear I’ll write some pseudo-code


// original = image the function recieves from the cam
var bmd:BitmapData = filterColor(original, myColor, threshold);
var myBM:Bitmap = new Bitmap(bmd);
// myBM is the bitmap as a result of filtering out myColor (which has to be a range of colors) 
// threshold makes the range bigger or smaller for example

Thanks!