ImageEn, unit imageenproc

TImageEnProc.RemoveChromaKey

TImageEnProc.RemoveChromaKey


Declaration

procedure RemoveChromaKey(KeyColorRGB: TRGB; Tolerance: double; Saturation: integer; FeatherEdges: Integer;
                          HueReduction : Integer = 0; RemoveNoise: boolean = False); overload;
procedure RemoveChromaKey(KeyPixelX, KeyPixelY: integer; Tolerance: double; Saturation: integer; FeatherEdges: Integer;
                          HueReduction : Integer = 0; RemoveNoise: boolean = False); overload;


Description

ChromaKey is the removal of a background for a source image so it can be replaced by an alternative image.


ChromaKey Source Image




ChromaKey Image on Background




ChromaKey Image on Background




RemoveChromaKey removes the KeyColor of an image and sets it to transparent. Although KeyColor is a single color, RemoveChromaKey will remove a range of colors that are similar to cope with light variance in photography.

Parameter Description
KeyColorRGB (Overload 1) A typical color within the Chromakey background, e.g. as returned by GuessChromaKeyColor or CalcAverageRGB
KeyPixelX, KeyPixelY (Overload 2) A pixel within the Chromakey background to be used as the key color. If both are set to -1 then GuessChromaKeyColor is used to guess the key color
Tolerance The level of tolerance when seeking similar colors to our key color. Range: 0.0 to 1.0. Typical value: 0.21
Saturation If a pixel is of a hue within our keycolor range, limit removal to the specified saturation. Range: 0 to 100. Typical value: 30
FeatherEdges Blurs the edge of the image to smooth the result and remove trace ChromaKey pixels (uses FeatherAlphaEdges). Typical value: 2
HueReduction Pixels of a similar hue to our key color but outside the level of tolerance will be reduced in saturation (color intensity). 0 is no hue reduction, 1 is minimal, 5 is maximum. Settings of 0, 2 or 3 are best
RemoveNoise Removes stray pixels missed by the removal

For more information on ChromaKey: en.wikipedia.org/wiki/Chroma_key

Typically ChromaKey removal is performed as follows:

1. Select a region of the background and use CalcAverageRGB to determine the Key color
2. Adjust Tolerance until foreground subject is fully visible
3. Adjust Saturation to remove any remaining background
4. If necessary, adjust FeatherEdges, HueReduction or RemoveNoise

Note:
If the image PixelFormat is not one ie24RGB or ie1g, it will be converted
An AlphaChannel will be added to the image, if it does not exist
A UI for this is available to your users in the Image Processing dialog


Demos

Demo  Demos\ImageEditing\ChromaKey\ChromaKey.dpr
Demo  Demos\ImageEditing\EveryMethod\EveryMethod.dpr


Example

// Load background
ImageEnView1.IO.LoadFromFile( sBackgroundFile );

// Load our ChromaKey (foreground) image as a foreground layer
ImageEnView1.LayersAdd( sChromaKeyFile );

// Get the average color of the selection (if the user has selected a background region) or guess the background color
if ImageEnView1.Selected then
  KeyColorRGB := ImageEnView1.Proc.CalcAverageRGB( 100 );
else
  KeyColorRGB := ImageEnView1.Proc.GuessChromaKeyColor();

// Remove Chromakey from foreground layer (will become transparent)
ImageEnView1.Proc.RemoveChromaKey( KeyColorRGB, 0.21, 30, 2, 0 );

// Refresh our view
ImageEnView1.Update();


See Also

GuessChromaKeyColor
SelectChromaKey
CreateRGB
TRGB2TColor
TColor2TRGB