Table of Contents

Method GetBackgroundMask

Namespace
LMKit.Segmentation
Assembly
LM-Kit.NET.dll

GetBackgroundMask(ImageBuffer, CancellationToken)

Generates the background mask of an image synchronously.

public ImageBuffer GetBackgroundMask(ImageBuffer image, CancellationToken cancellationToken = default)

Parameters

image ImageBuffer

The input image buffer.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

ImageBuffer

An ImageBuffer containing the alpha mask of the background as an 8-bit grayscale (8bpp Gray) bitmap. Pixel intensity represents transparency.

Examples

using LMKit.Media.Image;
using LMKit.Segmentation;

// Load the model (U²-Net for segmentation)
var backgroundDetector = new BackgroundDetection(
    LM.LoadFromModelID("u2net"));

var input = ImageBuffer.LoadAsRGB("D:\\input.png");
var mask = backgroundDetector.GetBackgroundMask(input);
mask.SaveAsPng("D:\\mask.png"); // saves alpha mask