views:

33

answers:

1

I am processing images to change their color from black to red, blue, green etc based on the requirement. I use SetPixel methods to change color of each pixel of the image from black to say red.

It works mostly fine except the borders and some curves within the image. Let's say I've circled image filled with black color. Circled image color is changed but still when I zoom, I can see blackish dots around border which is not completely replaced with red color. I tried to dig around and found that it has something to do with anti-aliasing.

Has anything faced similar problem or have thoughts/suggestions on how to fix this issue?

Many thanks in advnace for your help!

Regards, Tanush

A: 

It can be related with anti-aliasing. Anti-aliasing essence is that the more pixel is closer to the edge (boundary of something) the more pixel color is blended with background color (or we can say that it is more 'transparent'). So the problem may be that you need not only to replace source color to destination color, but also pixels which were blended from source color to background color. To achieve this you need:

1) Run edge detection algorithm of some kind - it may be simple or advanced as you want.

2) If pixel is near edge and pixel is near other pixel of your source color, then calculate it's opacity (1-transparency) factor- which will be

opacity = (pixel_color-background_color)/(source_color-background_color)

3) Now calculate your color to which you must replace current anti-aliased pixel:

new_color = background_color * (1-opacity) + opacity * target_color

And put this new_color instead of antialiased pixel.

In summary: You need to detect antialiased pixels and replace them with your version of antialiased pixels.

Hardest part of algorithm is detection of antialiased pixels - because you can't be sure that you found all edge pixels with 100% probability. Also you can't be sure was pixel antialiased or was just made initially of such color). Because of this you may get some color noise in final product. But in any case it should be better than just sit and wait :)

good luck

0x69