Post Reply 

Proper subject extraction, background removal, and background painting

Dec 29, 2011, 20:56 (This post was last modified: Mar 4, 2013 01:11 by ofnuts.)
Post: #1
Proper subject extraction, background removal, and background painting
Beginners very often wonder why a faint border remains around sharp items such as letter or logos when they remove or bucket-fill the background around them.

The explanation: when a border isn't a straight vertical or horizontal, it will look jagged /pixellated, if done with plain pixels because pixels on a computer display are big enough to be visible. To avoid this "anti-aliasing" is used: instead of having only two colors for the pixels (subject color or background color), pixels along the edge of the subject are made partially colored or partially transparent:

   

When you wand-select the background or bucket-fill, you specify a threshold that says how close are the colors you want to select. If the threshold is low, you don't select the anti-aliasing pixels, so you leave a faint border uncolored or uncut:

   

But if you use a high threshold, you apply the color (or select) bluntly to the anti-aliasing pixels so they disappear and you are left with a jagged border:

   

So, obviously, these anti-aliased pixels are the most precious part of your picture and you should never, ever, use any processing that destroys them. And to do so, there is only one solution: draw the shape on a transparent layer. This way, the anti-aliasing is done using partially transparent pixels... The next post will show why this is is important.

[Image: 2Tvo0.png]

Visit this user's website Find all posts by this user
Quote this message in a reply
Dec 29, 2011, 21:01 (This post was last modified: Dec 29, 2011 21:02 by ofnuts.)
Post: #2
RE: Proper subject extraction and background painting
Now, if you want the background color to blend smoothly, you just have to paint *under* these pixels, and their partial transparency will do the rest... And since where the pixels are fully opaque the underlying layer won't show, the best way to paint the background around the subject is to add a layer below and fill it with the color:

   

With some combination of colors this may create a slight visually dark outline (it is the case above). This can be alleviated by using the "Levels" tool on the alpha channel of the layer on top, to alter the transparency of the anti-aliased pixels. By making them a bit more transparent (drag the middle cursor to the left), the dark outline disappears before the jagged look sets in:

   

[Image: 2Tvo0.png]

Visit this user's website Find all posts by this user
Quote this message in a reply
Dec 29, 2011, 21:07 (This post was last modified: May 12, 2013 16:50 by ofnuts.)
Post: #3
RE: Proper subject extraction and background painting
Now, the $1000 question is: you have this nice logo already on a colored background, how do you get it on a transparent layer with the proper anti-aliasing?

In other words, how do you replace the border pixels with mixed colors by partially transparent pixels? Well, Gimp has a magical tool for this, which is called "Color to alpha" (the "alpha channel" is the channel, that, next to Red/Green/Blue ones, controls the opacity of a pixel). Here is how you can use it:

First, you use the Magic Wand to select the background, with a low threshold (you will notice that only the palest anti-aliasing pixels get included in the selection):

   

Then you grow the selection by one pixel. This makes it include all the anti-aliasing pixels(*):

   

Then, you apply "Colors/Color to alpha", using the background color as the reference color (in the picture below I put a white layer under the transparent layer to better show the recovered transparency on the border pixels):

   

And you can repaint that underlying background, to show how well this works:

   

Now, all this was done using several layers, but sometimes it's more convenient to work on just one layer. And Gimp also allows this, using two specific paint modes, "Color erase" and "Behind" that are complementary.

- The "Color erase" mode does exactly the same thing as Color-to-alpha, it replaces the painted color by transparency. Bucket-filling the selection in "Color erase" mode give identical results to Color-to-alpha. Like Color to alpha is is best used with a selection that covers the anti-aliasing pixels but protects the rest. In the image below the yellow brush strokes magically produce semi-transparent blue pixels:

   

- The "Behind" mode produces the same visual result as painting on a layer below the active layer. The paint is fully applied, where the active layer is transparent, partially applied where it is partially transparent, and no painting occurs where the layer is fully opaque. In this mode you can just paint around your subject and the painting will magically blend into the opaque parts:

   

(*) The smart reader will ask: why do we grow the selection instead of selecting everything? It's because Color-to-alpha faces an ambiguity. There are several ways to produce a given color, depending on the opacity: a light gray on white can be either:
  • a fully opaque light gray
  • a somewhat transparent dark gray
  • a very transparent black
And color-to-alpha always takes the solution that assumes the maximum transparency (it will choose the very transparent black above). So if you select everything 1) any area inside the object that will have a color close to the background will become quite transparent, and even the whole subject can become slightly transparent. The "grow-the-background-selection" method prevents this. This is also why you should use the Magic Wand to select the background and not the Color selection tool, since the latter could select part of the subject.

[Image: 2Tvo0.png]

Visit this user's website Find all posts by this user
Quote this message in a reply
Dec 29, 2011, 21:49
Post: #4
RE: Proper subject extraction and background painting
One last thing... you can of course wonder how you can repaint the subject and keep these precious anti-aliasing pixels partially transparent... it happens that there is something for this called "alpha lock". When this box is checked (see red circle below), the alpha channel of the whole layer remains unaffected by paint operations. This mean that if you want to apply one single color you can bucket-fill the whole subject layer, the background will remain transparent, and the anti-aliasing pixels will get the new color while remaining partially transparent:

   

[Image: 2Tvo0.png]

Visit this user's website Find all posts by this user
Quote this message in a reply
Jan 5, 2012, 04:34
Post: #5
RE: Proper subject extraction and background painting
Awesome! It worked and it looks so good.

Basically you just:

1. Use the wand and select the background
2. Grow by 1 pixel
3. Color>Color to Alpha

Mine was on a transparent layer, and I chose the color white, and I am assuming since the original background color was white, this is why it cleared up the smudges? Well excellent work Ofnuts just what we needed!

[Image: banana.gif]
Find all posts by this user
Quote this message in a reply
Oct 5, 2012, 20:22
Post: #6
RE: Proper subject extraction, background removal, and background painting
This is not proper extraction, this is stupid workarounds cause fill bucket doesnt do its job and its laughable in 2012 year that nbody took care of it yet.Now people are forced to use lahyers to do simple stuff which takes one click in photoshop, thats just pathetic.
Find all posts by this user
Quote this message in a reply
Oct 4, 2013, 18:03
Post: #7
RE: Proper subject extraction, background removal, and background painting
A quick script to automate some of this:

See replace-foreground-by-background in my script collection

[Image: 2Tvo0.png]

Visit this user's website Find all posts by this user
Quote this message in a reply
Oct 6, 2013, 05:16 (This post was last modified: Oct 6, 2013 05:16 by iForStyle007.)
Post: #8
RE: Proper subject extraction, background removal, and background painting
(Oct 4, 2013 18:03)ofnuts Wrote:  A quick script to automate some of this:

See replace-foreground-by-background in my script collection

Nice! I'll test it out maybe this week/next When you have the time, PM me a test photo and I'll post my results with how it goes/

@2backbar "lahyers" wow...lack of respect & spelling. A very decent tutorial imo.
Workarounds are for obvious bugs, this is not a bug just a minor defect(image extraction)!
PS only does such a good job too, no matter what some manual-touchups are needed.
If you ever comeback, you should make a tutorial on a better extraction >_>
Visit this user's website Find all posts by this user
Quote this message in a reply
Dec 14, 2013, 22:38
Post: #9
RE: Proper subject extraction, background removal, and background painting
What do we do about this if the background is transparent? Is this question already previously answered above? Thankx Heart you O.N.

[Image: banana.gif]
Find all posts by this user
Quote this message in a reply
Dec 14, 2013, 22:51
Post: #10
RE: Proper subject extraction, background removal, and background painting
This whole topic is about making the background transparent, so if it's already that way, there is nothing to do. Or I didn't understand your question?

[Image: 2Tvo0.png]

Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Sponge Painting Cartoon Landscapes Wendy Black 7 794 Aug 25, 2013 14:56
Last Post: ofnuts
  How to Make the New Youtube Background MikeDaviesDMD 4 879 Jun 11, 2013 05:46
Last Post: MikeDaviesDMD
  transparent background musicstuff 3 2,146 Jan 28, 2013 15:22
Last Post: Majik Imaje
  Need to Change Background Colour srinirex 1 713 Dec 21, 2012 08:52
Last Post: ofnuts
  Exporting Elliptical/Round Selections WITHOUT Transparent Background Boxes dsvendse 0 580 Dec 19, 2012 07:19
Last Post: dsvendse

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS