Sign in to follow this  
Legorol

Unblend - Reverse normal blend with a chosen color

Recommended Posts

Unblend

v1.0

 

Unblend v1.0.zip

 

This plugin removes a chosen color from the image and replaces it with transparency. In other words, it performs the reverse of a normal blend operation with the chosen color.

 

The more of the chosen color is present in a pixel, the more of it is removed and more transparency is added. In technical terms, this plugin performs inverse alpha compositing. Unlike similar plugins, the result of this plugin is guaranteed to be such that when blended back on a uniform background of the chosen color, the original image is completely recovered in a pixel perfect manner.

 

This plugin can be used for example to change the background of an image to be transparent, and gives the expected result even in the presence of anti-aliasing. If there is an object in the image whose edges blend smoothly into the background, then after using this plugin, the image can be placed on a different background, and the object will smoothly blend into the new background.

 

The download contains the source code, which includes a description of the algorithm and the Maths behind it.

 

How to use

1. Set the color to be removed as the Primary Color (transparency value is ignored).

2. Select Effects->Unblend.

That's it!

 

The plugin respects the active selection, so to apply Unblend only to part of the image, select that part first.

 

Comparison with similar plugins

There are many plugins that perform Color to Alpha conversion (ColorToAlpha, AlphaMask etc.). However, typically these plugins only add alpha values (transparency) to the image, and do not alter the colors of the pixels in the image. The result is that when placed back on a uniform background of the chosen color, the original image is not recovered. This Unblend plugin does correct the colors of the pixels too, and so does not have this problem.

 

GrimColorReaper is one plugin that does alter the colors of the pixels in addition to adding transparency. However, GrimColorReaper uses an inadequate calculation of the alpha values (Eucledian distance in RGB space). This Unblend plugin uses a mathematically correct calculation. GrimColorReaper's alpha calculation can under some circumstances result in a tint and doesn't always give the expected result.

 

Technical comment

Inverse alpha compositing is an underdefined problem, because the alpha value of the result is not unique. A pixel could be made more intense with a lower alpha, or less intense with a higher alpha, and still give the correct result when blended back on the background color. The Unblend plugin uses a greedy algorithm, whereby it always uses the minimum permitted value of alpha for each pixel, so that as much of the background color is removed as possible.

 

Examples

Starting with an image of an anti-aliased letter A, the white background is removed with Unblend. The result is then placed on a blue background:

IICDHX8.png => YxnpFxJ.png => WM2uyeZ.png

Magnified:

QkKQgdX.png => nqYS1d5.png => ALPfCo2.png

 

Start with a red circle, anti-aliased on a blue background. A selection is made with the Magic Wand at 80% tolerance outside the circle, so that the selection covers the outside of the circle including anti-aliased pixels. The blue background is removed with Unblend. The result is placed on a green background:

uQZTIsv.png => uDSc7pQ.png => NyDkbjZ.png

Magnified:

noCrZOv.png => cTKMdnt.png => dnGiSYy.png

Notice how outside the circle, red is anti-aliased with green, but inside the circle, red is anti-aliased with blue. For comparison, here is what you would get if you simply tried to use the Paint Bucket tool to replace the blue with green at various levels of tolerance:

7AS0M8a.png Zr1KE4Q.png HZ2j7Ot.png

Tolerance 0%                                     Tolerance 50%                                     Tolerance 80%

Notice how at 0% and 50% you are left with some blue-ish pixels outside the circle. At 80%, they are all gone, but so is the anti-aliasing. The Paint Bucket is not the right tool for this kind of color replacement.

 

Download

Unblend v1.0.zip

Edited by Legorol
  • Upvote 4

Share this post


Link to post
Share on other sites

Thanks for the welcome. I have just recently discovered Paint.NET, and I really like it.

 

I wanted to be able to make the background of images transparent, or be able to change one color to another, but I ran into problems with anti-aliasing. Some Googling made it clear that this is a common problem. I tried out various plugins, but none of them did exactly what I wanted, so I wrote my own.

 

Based on how common this problem is, I'm hoping that my plugin will be able to help a lot of people.

Edited by Legorol

Share this post


Link to post
Share on other sites

Thanks for sharing.

 

Something like this was on my ToDo list. I'm happy that you did it.

Plus: You explained in the source how you are solving the issue that there is no unique solution of the problem.

 

A variant may be to respect the surrounding pixels to decide whether you are unblending a pixel or not.

Share this post


Link to post
Share on other sites

This is showing up directly in the Effects menu. I would recommend placing it in one of the submenus - for they are legion and good for keeping stuff organized :P

How about Effects > Color?  Or even Adjustments?

BTW: Codelab supports an information header in your *.cs files, so it's possible to provide a bit of info about the plugin.  This is used in v4.0 to populate the tooltip when you hover over the plugin in the menu.  The header looks like this:

 

// Submenu: Color
// Name: Unblend
// Author: Legorol
// URL: http://www.getpaint.net/redirect/plugins.html

Your code here......
Impressive description of the plugin function in your *.cs file. Nice touch! :star:
  • Upvote 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this