# User:Sahima Badir/Removing backgrounds from images needing translucency

 This page is currently under construction.The information contained within should not be considered fully accurate and/or complete.

## Understanding the theory

Before we can start removing backgrounds from images effectively, we first need to understand the basics of how games and websites render translucency. Although there are different ways to blend two images together, the most basic and fundamental method is alpha blending, and is the only method we need to understand in this context. Alpha blending is represented as

${\displaystyle \mathrm {original} =\mathrm {background} *(1-\mathrm {alpha} )+\mathrm {foreground} *\mathrm {alpha} }$
From left to right: original, background, foreground, and a

where

${\displaystyle \mathrm {original} }$ is the unedited image
${\displaystyle \mathrm {background} }$ is the area of the image behind the model
${\displaystyle \mathrm {foreground} }$ is the colour of the model with translucency turned off
${\displaystyle \mathrm {alpha} }$ is the alpha value of the pixel, applied to the ${\displaystyle \mathrm {foreground} }$ colour

This formula can be memorised using the mnemonic

Barrows 1 Awards F--- All

Memorising this equation is vital, and our goal will be to first recreate the image's background, then recreate either the alpha channel or the model's colour, so that we can then calculate the remaining elements of the image by rearranging our formula.

### Rearranging the formula

Most images fall into one of two categories. Either the foreground colours are easy to predict, or the alpha values are easy to predict. In both situations, we'll want to rearrange our formula for the remaining variable.

When our image we are working with contains a particularly complex texture, or the translucent areas may otherwise be difficult to properly estimate or recreate, it is preferable to estimate the alpha channel. Rearranging our formula therefore for ${\displaystyle \mathrm {foreground} }$ gives us

${\displaystyle \mathrm {foreground} ={\frac {\mathrm {original} -\mathrm {background} *(1-\mathrm {alpha} )}{\mathrm {alpha} }}}$

When our image uses translucency whose colour can be predicted but contains varying alpha values, such as models that glow, it is preferable to estimate the colour of the translucent areas. Our rearranged formula is

${\displaystyle \mathrm {alpha} ={\frac {\mathrm {background} -\mathrm {original} }{\mathrm {background} -\mathrm {foreground} }}}$

but this value will be calculated for each channel separately and must therefore be handled appropriately. Our first option is to apply the calculation in greyscale so that we end up with a single result. Our second is to perform the calculations in full colour, then multiply our image by itself and add the channels together, using either the Calculations feature in Photoshop, or the Decompose / Recompose feature in GIMP. This option is more accurate, but also more involved.

Although we have no immediate need to calculate the background, it can occasionally be useful to estimate the alpha and foreground values first, then use them to calculate the background, thus allowing us to recalculate the first two more precisely. This concept won't be covered, but should the need arise, our rearranged formula would be

${\displaystyle \mathrm {background} ={\frac {\mathrm {original} -\mathrm {foreground} *\mathrm {alpha} }{1-\mathrm {alpha} }}}$

### Blend modes

Having rearranged our formula, we'll now need a way of calculating the remaining element within our chosen editing software. Unfortunately, there are no features that calculate these for us, so we have to use blending.

The four blend modes we'll need to remember are

Blending mode Formula
Linear Dodge ${\displaystyle \mathrm {target} +\mathrm {blend} }$
Difference ${\displaystyle |\mathrm {target} -\mathrm {blend} |}$
Multiply ${\displaystyle \mathrm {target} *\mathrm {blend} }$
Color Dodge ${\displaystyle {\frac {\mathrm {target} }{1-\mathrm {blend} }}}$

where ${\displaystyle \mathrm {blend} }$ is our current layer, and ${\displaystyle \mathrm {target} }$ is the blended result of all the layers below.

A painter's algorithm is traditionally used when calculating blending, so we can assume that the further down a layer is, the earlier it will be calculated. This means that if we had layers ${\displaystyle a,b,c}$ where ${\displaystyle a}$ is the top layer and ${\displaystyle c}$ is the bottom layer, and we were to apply a Multiply to ${\displaystyle a}$ and a Linear Dodge to ${\displaystyle b}$, this would be the same as the calculation ${\displaystyle a*(b+c)}$, since ${\displaystyle b}$ would be calculated before ${\displaystyle a}$.

Blending our layers together will allow us to calculate the formulae we rearranged earlier, with each mode providing us access to our basic operators - addition, subtraction, multiplication, and division.

#### Layer setup

So, let's have a go at converting our foreground formula into layers.

${\displaystyle \mathrm {foreground} ={\frac {\mathrm {original} -\mathrm {background} *(1-\mathrm {alpha} )}{\mathrm {alpha} }}}$

Firstly, to simplify the problem, we'll place brackets around our variables to explicitly define the order of operations. Every time we apply a blend mode to a layer or layer group, it will represent a single calculation being performed on two sets of brackets, thus we wish to structure our equation in such a way that we can easily see which operations need to be performed first.

${\displaystyle \mathrm {foreground} =(\mathrm {original} -(\mathrm {background} *(1-\mathrm {alpha} )))/(\mathrm {alpha} )}$

As we can see when we look at our equation, the order we would usually take when calculating the formula is

${\displaystyle 1:(1-\mathrm {alpha} )}$
${\displaystyle 2:(\mathrm {background} *(1-\mathrm {alpha} ))}$
${\displaystyle 3:(\mathrm {original} -(\mathrm {background} *(1-\mathrm {alpha} )))}$
${\displaystyle 4:(\mathrm {original} -(\mathrm {background} *(1-\mathrm {alpha} )))/(\mathrm {alpha} )}$

Now that we've sorted out the order of operations we need to perform, let's have a look at the blend modes. Since layers further down are calculated first, we assume that ${\displaystyle 1}$ is our bottom layer, and ${\displaystyle 4}$ is our top layer.

Layer ${\displaystyle 1}$ - our bottom layer - is inverted. Although we can calculate this by placing a layer filled with the value 1 - white - above the alpha channel, it would be easier just to invert our alpha channel and use it as it is. It therefore requires no blending.

Layer ${\displaystyle 2}$ is a simple multiplication. As we have our inverted alpha channel on the bottom, all there is to do is place the background layer above it, and set its blending to Multiply.

Layer ${\displaystyle 3}$ is just as simple. We place the original image above the background layer, then set its blending to Difference. Because Difference uses the absolute value, the order of the layers in the subtraction is irrelevant.

Layer ${\displaystyle 4}$ is the division. This is a little trickier. Color Dodge will give us our division, but we first need to deal with the inversion in the denominator. As with before, it would be possible to use a white layer with the blending set to Difference to perform an inversion, but we're again working with just a single layer, so we should just invert it. When Color Dodge inverts it again, it will be the same as calculating ${\displaystyle 1-(1-\mathrm {alpha} )}$ which, of course, would resolve to ${\displaystyle \mathrm {alpha} }$. With Color Dodge placing the ${\displaystyle target}$ on top and the ${\displaystyle blend}$ on the bottom, this makes it a lot easier for us since we won't have to create a layer group to apply blending to. Our inverted alpha channel now becomes our top layer, with a blending of Color Dodge.

We are left with our layer setup

Layer description Layer blending
${\displaystyle 4}$ Inverted ${\displaystyle \mathrm {alpha} }$ Color Dodge
${\displaystyle 3}$ ${\displaystyle \mathrm {original} }$ Difference
${\displaystyle 2}$ ${\displaystyle \mathrm {background} }$ Multiply
${\displaystyle 1}$ Inverted ${\displaystyle \mathrm {alpha} }$ None

A list of layer set ups can be found in the Quick References section at the bottom of this article.

## Examples

 This page is currently under construction.The information contained within should not be considered fully accurate and/or complete.

Need to write this up properly, but here's a load of pretty images and some regurgitated words

### Set up

1. Create an original image layer for back up and comparison; place this on top so we can toggle between our attempt and the original image. Our goal is to have our attempt (with the estimated background layer visible) as close to the original image as possible.
2. Duplicate the layer so that we may use this for the opaque areas of our image (right now, this will be the same as our original image)
3. Create two layers for the background, one using the RS Wiki's Light theme background; one using the Wiki's Dark theme background (need to figure out how to link these, given they're both data:images and not URLs) Place these two on the bottom, and hide or display as necessary.

### Step 1: Cutting out the opaque sections

First, we'll remove all areas of the image that we know aren't opaque.

1. Going around our model with the polygonal marquee, we select the model itself, attempting to get the selection as accurate as possible.
2. Use this selection to create a layer mask which we'll apply to the opaque layer we created when setting up.
3. Using the dark or light background layers for contrast, clean up the edges of the model with the Clone Stamp tool, the Brush tool, or the Smudge tool, depending on practicality

### Step 2: Recreating the background

Recreating the background varies in complexity from image to image. Every time we do this, we want to ensure that it's as close as we can get it, which will reduce artefacts later on. In this example, we have a screenshot taken by a lovely person who understands the true value of a solid green background, so recreating it should be simple. We only need to concern ourselves with parts of the image where we know or suspect translucency exists.

### Step 3: Putting our theory to use

(Need to write up how to evaluate this properly, but here's a simple example) Now that we have our background, there are multiple possibilities. What we do next will depend entirely on the image we're working with, but imma explain that here in a later draft of this guide kthx

1. We take the difference between the original image and the background to see if there's anything we can take from it (there usually is, but it's not guaranteed). In our example here, we can see that it looks as though we have our translucent areas on a black background (and a load of green stuff where the opaque areas are - this is to be ignored for now). This is good, but we still need to separate the two.
2. Merging our layers, we use the Desaturate tool on the resulting layer to get a reasonable estimation of the alpha. Seriously, I'd be happy if even a tenth of the images were this bloody easy.
3. With our alpha channel and background estimated, we mosey on down to the Quick References section of this guide, and set up our layers to the layer set up for calculating ${\displaystyle \mathrm {foreground} }$, remembering to invert our alpha channel for the calculations, and to apply the proper blend modes to each layer.
4. Look at this hot mess, there be our foreground colours. If it was good enough for Jesus, it'll be good enough for you.
5. Merge our layers together, not forgetting to back up our alpha channel and invert it back to what it was originally, then apply the alpha channel as a mask to the resulting layer. Show the foreground layer, which is now showing us our translucency, and the opaque layer, and we now have our image. Nearly.

### Step 4: Cleaning up yo ship

In all likelihood, our background won't have been perfect. In this case it was, because of the lovely solid green background, but we'll usually need to clean up whatever it was we calculated (foreground in this case).

1. This example is so uncharacteristically easy that our colours are entirely non-problematic, but we've also ended up calculating the opaque areas as well, since we didn't specifcy the translucent areas exactly, so we're going to need to clean that up by Smudging our translucency alpha values into the model, to hide them behind the opaque layer.

## Quick References

Here is picture of Karamja, yes?