PDA

View Full Version : How to replace colors in an image?


Codester99
07-31-2009, 02:35 AM
I don't know if I'm not searching for the right words or what, but I've spent the past week trying to find a solution to this seemingly straight forward hurdle....

I have an image of, say an eyeball, where I want the white of the eyeball to remain white, but the iris to change to a specified color. If I set the default color of the iris to the RGB values of 255,0,255, can I replace that Magenta color with something else programatically using ActionScript? An added bonus would be to be able replace all shades of Magenta (1,0,1 through 255,0,255) with another color similarly shaded.

Any help you guys can lend would be incredibly helpful! Thanks in advance!

Peter Cowling
07-31-2009, 08:20 AM
If I set the default color of the iris to the RGB values of 255,0,255, can I replace that Magenta color with something else programatically using ActionScript?

The answer is yes, although there are degrees of difficulty.

First, I am not sure which of these really best applies to your situation:


change one known color to another - easy
determine if the known color is an iris or part of something else - easy if the same image is used, not so easy otherwise, but you can analyze for whiteness etc.


The basic approach is to scan an image's pixel by pixel using BitmapData.getPixel. This gives you the ability to identify an RGB value, and if required where it is located. You can use setPixel to make the change - either straight away of following the 'scan'.


An added bonus would be to be able replace all shades of Magenta (1,0,1 through 255,0,255) with another color similarly shaded.

This is where things do get more difficult. RGB is not a natural definer of named colors, and therefore does not help efforts to pre-define color ranges for equivalency testing. You can convert to another color space, and then allocate colors; I have done that before, with acceptable enough results - but it is reasonably difficult to get precisely right if you do not already know a reasonable amount about color spaces (at least it was for me).

So you can do all of this, with the upper end of the difficulty range being something like this: analyze any image, and find the iris; change that multicolored iris from its current color range to some other named color range, which is based on the entire color space spectrum.

Codester99
07-31-2009, 03:17 PM
Thanks, Peter! I was hoping there was already some function or class out there that did this for me, but I basically came to the same conclusion as you and have begun writing the code to inspect the image pixel-by-pixel to determine if it's one I need to alter.

Peter Cowling
08-02-2009, 08:41 AM
Cool; good luck.