ContiguousBackgroundRemoval Class

Makes the background of an image transparent.

Namespace: Aurigma.GraphicsMill.Transforms
Assembly: Aurigma.GraphicsMill (in Aurigma.GraphicsMill.dll)

Syntax

C#
public sealed class ContiguousBackgroundRemoval : Transform

Remarks

The background is the part of the image that can be reached from the image border through pixels of a similar color. The transform finds it with a flood fill that starts from the whole border. The alpha of every background pixel becomes 0. Other pixels do not change.

Pixels of the background color that the subject encloses stay opaque, for example, the inner parts of letters or a white shirt against a white wall. This makes the transform a good choice for logos, product photos, and portraits. To make a color transparent everywhere in the image, use the Transparentize transform instead.

Colors are compared as CIE L*a*b* distances (delta E, CIE76). The image is converted to Lab with its color profile, or with the default profile of its color model. This conversion is used only to find the background. The colors of the result are not converted.

The background color is BackgroundColor. If it is null, the median color of the opaque border pixels is used. Set it explicitly when the subject touches the image border.

A pixel joins the background when two conditions are met. Its color differs from the background color by no more than Tolerance. Its color also differs from a neighboring background pixel by no more than StepTolerance. So Tolerance limits the whole color range of the background, and StepTolerance limits how fast the background color can change from one pixel to the next. A smooth gradient or a vignette stays within StepTolerance, so it is removed. A sharp edge of the subject exceeds it and stops the flood fill, even if the color of the subject is within Tolerance.

The colors are compared on a slightly blurred copy of the image, so that noise, such as JPEG artifacts, does not make the edge ragged. The blur can make a thin line look like background. So after the flood fill, the original color of each background pixel is compared with the typical color of the background around it. If the difference is larger than DetailThreshold, the pixel becomes a part of the subject again. Pixels next to the edges of the subject are not checked.

To remove more of the background, increase Tolerance and StepTolerance. If parts of the subject are removed, decrease them, first of all StepTolerance. If thin lines of the subject disappear, decrease DetailThreshold. If specks of noise stay in the background, increase it.

The transform accepts all pixel formats except the spot ones. The result has the alpha version of the source pixel format with the same bit depth. For example, RGB becomes ARGB, and CMYK becomes ACMYK. A source with an alpha channel keeps its pixel format. Its transparent pixels count as background whatever their color. Indexed images become Format32bppArgb.

The alpha of the result is binary: 0 for the background, unchanged for the rest.

Examples

C#
using (var reader = ImageReader.Create(@"Images\Product.jpg"))
using (var removal = new ContiguousBackgroundRemoval(RgbColor.White))
using (var writer = ImageWriter.Create(@"Images\Output\ProductNoBackground.png"))
{
    // Pixels that differ from white by more than 20 (delta E) belong to the product.
    removal.Tolerance = 20f;

    Pipeline.Run(reader + removal + writer);
}

The original image and the result with the background removed.

The original image and the result with the background removed.

Inheritance Hierarchy

System.Object
L Aurigma.GraphicsMill.PipelineElement
L Aurigma.GraphicsMill.Transforms.Transform
L Aurigma.GraphicsMill.Transforms.ContiguousBackgroundRemoval

See Also

Reference