Table of Contents

Class SegmentationMask<T>

Namespace
AiDotNet.Augmentation.Image
Assembly
AiDotNet.dll

Represents a segmentation mask for pixel-level annotations.

public class SegmentationMask<T>

Type Parameters

T

The numeric type for mask values.

Inheritance
SegmentationMask<T>
Inherited Members

Remarks

Segmentation masks provide pixel-level object delineation, used for: - Semantic segmentation (what class is each pixel) - Instance segmentation (which object instance is each pixel) - Panoptic segmentation (both semantic and instance)

For Beginners: While a bounding box draws a rectangle around an object, a segmentation mask precisely outlines the object's shape. When you rotate or flip an image, the mask must be transformed identically.

Constructors

SegmentationMask()

Creates an empty segmentation mask.

public SegmentationMask()

SegmentationMask(IList<IList<(double x, double y)>>, int, int, MaskType)

Creates a segmentation mask from polygon vertices.

public SegmentationMask(IList<IList<(double x, double y)>> polygons, int width, int height, MaskType type = MaskType.Binary)

Parameters

polygons IList<IList<(double x, double y)>>

The polygon vertices.

width int

The image width.

height int

The image height.

type MaskType

The mask type.

SegmentationMask(int[], int, int, MaskType)

Creates a segmentation mask from RLE encoding.

public SegmentationMask(int[] rleCounts, int width, int height, MaskType type = MaskType.Binary)

Parameters

rleCounts int[]

The RLE counts.

width int

The mask width.

height int

The mask height.

type MaskType

The mask type.

SegmentationMask(T[,], MaskType, int)

Creates a segmentation mask from dense data.

public SegmentationMask(T[,] maskData, MaskType type = MaskType.Binary, int classIndex = 0)

Parameters

maskData T[,]

The 2D mask array [height, width].

type MaskType

The mask type.

classIndex int

The class index.

Properties

ClassIndex

Gets or sets the class index (for semantic/instance masks).

public int ClassIndex { get; set; }

Property Value

int

ClassName

Gets or sets the class name.

public string? ClassName { get; set; }

Property Value

string

Confidence

Gets or sets the confidence score (if from a segmenter).

public T? Confidence { get; set; }

Property Value

T

Encoding

Gets or sets the current encoding format.

public MaskEncoding Encoding { get; set; }

Property Value

MaskEncoding

Height

Gets or sets the mask height.

public int Height { get; set; }

Property Value

int

InstanceId

Gets or sets the instance ID (for instance/panoptic masks).

public int InstanceId { get; set; }

Property Value

int

MaskData

Gets or sets the mask data as a 2D array [height, width].

public T[,]? MaskData { get; set; }

Property Value

T[,]

Remarks

For binary masks: 0 = background, 1 = foreground. For semantic masks: each value is a class index. For instance masks: each value is an instance ID.

Metadata

Gets or sets additional metadata.

public IDictionary<string, object>? Metadata { get; set; }

Property Value

IDictionary<string, object>

Polygons

Gets or sets the polygon vertices (if using polygon encoding).

public IList<IList<(double x, double y)>>? Polygons { get; set; }

Property Value

IList<IList<(double x, double y)>>

Remarks

List of polygons, each polygon is a list of (x, y) coordinates.

RLECounts

Gets or sets the RLE-encoded mask (if using RLE encoding).

public int[]? RLECounts { get; set; }

Property Value

int[]

Type

Gets or sets the mask type.

public MaskType Type { get; set; }

Property Value

MaskType

Width

Gets or sets the mask width.

public int Width { get; set; }

Property Value

int

Methods

Area()

Calculates the area (number of foreground pixels).

public int Area()

Returns

int

The mask area in pixels.

Clone()

Creates a deep copy of this mask.

public SegmentationMask<T> Clone()

Returns

SegmentationMask<T>

A new mask with copied data.

Dice(SegmentationMask<T>)

Calculates the Dice coefficient with another mask.

public double Dice(SegmentationMask<T> other)

Parameters

other SegmentationMask<T>

The other mask.

Returns

double

The Dice coefficient between 0 and 1.

GetBoundingBox()

Calculates the bounding box of the mask.

public (int xMin, int yMin, int xMax, int yMax) GetBoundingBox()

Returns

(int xMin, int yMin, int xMax, int yMax)

The bounding box (xMin, yMin, xMax, yMax).

IoU(SegmentationMask<T>)

Calculates the IoU (Intersection over Union) with another mask.

public double IoU(SegmentationMask<T> other)

Parameters

other SegmentationMask<T>

The other mask.

Returns

double

The IoU value between 0 and 1.

ToDense()

Converts this mask to dense format.

public T[,] ToDense()

Returns

T[,]

The dense mask data.

ToRLE()

Converts this mask to RLE format.

public int[] ToRLE()

Returns

int[]

The RLE counts.