Reputation: 351
So I am working on a filter that performs a blur.
When the image has an alpha channel, it is blurring incorrectly. This is because it is blurring with the colored pixels that lie on the alpha=0 sections.
I have created an example to illustrate. In the top-left image, we have the original. It has an alpha channel but I have it disabled.
In the top-right image, I am importing it with the alpha channel. It displays the pixels in the alpha channel section as black.
In the bottom-left image, I have performed my blur algorithm. You can see that the edges have a bright halo because they are blurring against the light colored background pixels shown in the first image, even though the alpha channel is there.
In the bottom-right image, I have composited it against a background so that you can see it is compositing incorrectly.
I have read that this might be a premultiplication issue. I have premultiplied the RGB pixels that lie on the alpha=0 sections by black but it is still incorrect. When I composite that version, you can see it creates a dark halo instead of a bright one.
It seems that I would need to know the RGB values of the destination image but that is impossible to know as the user will determine that.
So what am I doing wrong? How can I adjust my algorithm so that it blurs the edges correctly and not against the background brighter pixels, thus creating a bright halo?
I appreciate any help on this as I'm really stuck!
Upvotes: 0
Views: 605
Reputation:
Blurring works by averaging the pixels in some neighborhood of the target pixel. As you reach the boundaries of the foreground objects, the algorithm has little other choice than taking pixels from the background as well.
If you don't want to get any mixture with the background pixels, you have to modify the blur algorithm so that it only uses foreground pixels i.e. assigns a zero weight to any background pixel.
Anyway, doing this, you will somewhat lower the amount of blur at the edges. There is no perfect solution.
Upvotes: 0