logo
home plugins ownwork contact links impressum



photoshop plugins

chrome erazor

The princible of chroma keying with "chrome erazor"

the basic princible of chroma keying

If you know how a special plugin works you can use it better and abuse it for special purposes. That's why the whole principle of chromakeying and the algorithm "chrome erazor" uses is descriped here in detail.

Chroma keying or blue screen (or green screen) is a known effect from TV and film. A persons acts in front of a blue or green background and this background is replaced later by an other background. The persons seems now to act in the new background, e.g. a computer generated scene. This plug in let you do the same in photoshop in a better way than with other selection tools.

The principle of chroma keying is quite easy. From a foreground image a specific color (means nostly actually only HUE) is selected and a blending mask or key signal is generated. In areas where this key color is found in this forground image the key signal is maximum (1) and there where is real foreground the key signal is 0. The new background image is multiplied by this key signal and the foreground image is muliplied by the invers key signal. Both images will be added together then. The process consist of four steps.
1. extracting the key signal (originally a simple threshhold in the blue channel),
2. processing foreground (desaturation, applying invers mask)
3. processing background (applying mask)
4. add both together


chrome erazor
picture 1: principle of chroma keying for TV

This is how chroma keying works in TV. Note that both images - even the background - has to be proceeded and the result is calculated immediately as a new image. This is fine for TV, but we are working with photoshop! That means that we used to work with layers. Only the foreground layer should be proceeded. It would give you much more flexibiliy when not combining two images directly but simply make the foreground transparent by removing the areas with key color.
The algorithm for photoshop consist of three steps.
1. extracting the key signal
2. processing foreground color (removing key color)
3. apply transparency on the foreground layer
The background layer keeps unchanged. You can have more than one background layer. There is no need to combine them before.

chrome erazor
picture 2: principle of chroma keying for photoshop

principle of chrome erazor

At a first thought chromakeying seems to be very simple: Look at every pixel in te image, if a color is regognized as background remove this pixel and add a new background or make the pixel transparent.
But the real problem are the crossing areas where each color are blended together! Lets look at picture 3. It shows a perfect red ball in front of a perfect blue screen. At a closer look you'll see the blending area (picture 4).

red ball red ball magnified red ball magnified key
picture 3: red (255, 0, 0) ball in front of a perfect blue screen (0, 0, 255)
picture 4: starke vergrößerung
picture 5: key signal derived from this image


Even in this primitve image with two perfect colors there are not only red or blue pixels. There are a gradient from red to blue with nearly all steps in between. How this pixel should be handled? The answer seems to be simply, too. We just blend it. We generate the key signal in the following way: blue is background, therefore the key signal (the mask) is 0, red pixels are foreground, the key signal is 1 (maximal value) there. Colors in between are blended togethere linearily. On a magenta pixel in the middle (128, 0, 128) the keysignal is 0.5. (half). In principle this is how the keysignal is generated. And how "chrome erazor" calculate it. (you can just let to generate only this keymask, by using color handling "key" the result is a black and white image showing this key mask).

"chrome erazor" needs only the invers key signal where background is 0 and foreground maximal value. In the following discussion key mask refers always to this inverted key signal. Just like a photoshop layer mask.

red ball red ball magnified
picture 3: red (255, 0, 0) ball in front of a perfect blue screen (0, 0, 255)
picture 4: starke vergrößerung


Picture 5 is just the same but the red ball is slightely transparent to the border, just like it would be a red gas ball. We have a larger gradient there for better illustrating the effects on such a color blending area. Picture4 shows the keysignal generated from it. Simple chromakeyer will just add the wighted foreground and background images. Cout - the new color of this pixel is calculated by the following fomular: Cout = Foreground * key + Background * (1 - key) Let assume our new backgroud is white, we'll get this Picture5 + Picture6 = Picture7 We are working with photoshop so we only look at the foreground layer. Here the key signal is the transparency.

red ball
picture 3:

See what happens? The blue color still remains in the image! The red gas ball has a blue is border now. In this case more a magenta border but this is the same. I know you can see such things every day in TV, where a blonde person has a blueish border around the hair. But do we want this? No! (but of course you _can_ reproduce this with "chrome erazor", just select color mode "no".) That's why another method was invented. The foreground color that is actually the key color is removed from the foreground. Look again at our magenta pixel in the middle. It has a key signal of 0.5 and 128 blue. The real key color is 255 blue. The idea is to subtract the keycolor mulitplied by the keysignal (actually the invers keysignal). Creduced = Foreground - (1 - key) * Keycolor Our Keycolor is (0, 0, 255) blue, key is 0.5. Multiplied together (0, 0, 128). And this subtracted from (128, 0, 128) we get (128, 0, 0). Seems to be perfect. The blue color is completely removed. Picture9 shows this result.

original foreground desaturated foreground
picture 3: original foreground
picture 3: foreground desaturated


What we expect is that our pixel are (128, 0, 0) now. Or combined with the white background (the white is half visible) (255, 128, 128). This is the case only when we add the background image mulitplied by invers key directly to the foreground. A chromakeyer used in TV-stations or virtual studios work this way.

red ball red ball magnified red ball magnified key
picture 3: red (255, 0, 0) ball in front of a perfect blue screen (0, 0, 255)
picture 4: starke vergrößerung
picture 5: key signal derived from this image


But rememeber: we are working with photoshop! We do not want to combine it directly. We want to make the foreground transparent. If we multiply the foreground with the key mask we get: (64, 0, 0) Picture12 shows the result, how the two layers will look together. We have only changed are blue border to a black border. This can be better, but it's still far from ideal.

red ball
picture 3:

"chrome erazor" uses a special algorithm to recalculate the color of the foreground pixels if there were not transparent. In our case this is a perfect red. Picture13 shows the result of "color restoring". This may look a little bit strange, expecially the border in completely wrong colors due to rounding errors an lower value. But appling the transparency mask it will look perfectly. Or red gas ball stand completely alone without any wrong colors. You can use any background without color artefacts.

red ball red ball magnified red ball magnified key
picture 3: red (255, 0, 0) ball in front of a perfect blue screen (0, 0, 255)
picture 4: starke vergrößerung
picture 5: key signal derived from this image


The "color restoring" algorithm works mostly good. But sometimes it can be useful to use other color handling methods provided by "chrome erazor". E.g. if your new background is dark it can be useful to have a black border around you foreground. If the new background is blue (e.g. water, sky) it can be useful not to remove the blue color to get a more realistic blending. Think this thought to the end you'll come to the comclusion that is can be useful to blend it against every other color. Exactly the color your new background has. This will produce the illusion of reflected light from the background. If the new background is a red brink wall, then it can be useful to blend it to this color. That's why a third color handling algorithm exist: it blends to the current photoshop background value.

red ball red ball magnified red ball magnified key
picture 3: red (255, 0, 0) ball in front of a perfect blue screen (0, 0, 255)
picture 4: starke vergrößerung
picture 5: key signal derived from this image




copyright 2007 by Thomas Thiele last actualisation 5th August 2007