Class SegmentationMask<T>
- Namespace
- AiDotNet.Augmentation.Image
- Assembly
- AiDotNet.dll
Represents a segmentation mask for pixel-level annotations.
public class SegmentationMask<T>
Type Parameters
TThe 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
polygonsIList<IList<(double x, double y)>>The polygon vertices.
widthintThe image width.
heightintThe image height.
typeMaskTypeThe 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
rleCountsint[]The RLE counts.
widthintThe mask width.
heightintThe mask height.
typeMaskTypeThe 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
maskDataT[,]The 2D mask array [height, width].
typeMaskTypeThe mask type.
classIndexintThe class index.
Properties
ClassIndex
Gets or sets the class index (for semantic/instance masks).
public int ClassIndex { get; set; }
Property Value
ClassName
Gets or sets the class name.
public string? ClassName { get; set; }
Property Value
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
Height
Gets or sets the mask height.
public int Height { get; set; }
Property Value
InstanceId
Gets or sets the instance ID (for instance/panoptic masks).
public int InstanceId { get; set; }
Property Value
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
Polygons
Gets or sets the polygon vertices (if using polygon encoding).
public IList<IList<(double x, double y)>>? Polygons { get; set; }
Property Value
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
Width
Gets or sets the mask width.
public int Width { get; set; }
Property Value
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
otherSegmentationMask<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
IoU(SegmentationMask<T>)
Calculates the IoU (Intersection over Union) with another mask.
public double IoU(SegmentationMask<T> other)
Parameters
otherSegmentationMask<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.