Table of Contents

Method GetBackgroundMaskAsync

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

GetBackgroundMaskAsync(ImageBuffer, CancellationToken)

Generates the background mask of an image asynchronously.

public Task<ImageBuffer> GetBackgroundMaskAsync(ImageBuffer image, CancellationToken cancellationToken = default)

Parameters

image ImageBuffer

The input image buffer.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<ImageBuffer>

A task that represents the asynchronous operation. The task result contains the alpha mask of the background as an ImageBuffer in 8-bit grayscale (8bpp Gray) format.

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 = await backgroundDetector.GetBackgroundMaskAsync(input);
mask.SaveAsPng("D:\\mask.png"); // saves alpha mask