SegmentationMask class

A segmentation probability mask indicating foreground vs background.

Contains per-pixel probabilities (0.0-1.0) where higher values indicate greater likelihood that the pixel belongs to a person/foreground.

Memory Management

Mask data is stored as Float32List. For large images, prefer:

  • Use toBinary or toUint8 to reduce memory by 4x
  • Use upsample with maxSize parameter to limit output
  • Process masks promptly and let GC collect

Thread Safety

SegmentationMask is immutable. The underlying data buffer is exposed as an unmodifiable view.

Example

final mask = await segmenter(imageBytes);

// Get model-resolution mask values
final probability = mask.at(128, 128);

// Upsample to original image size (capped at 1080)
final fullMask = mask.upsample(maxSize: 1080);

// Convert to binary for compositing
final binary = fullMask.toBinary(threshold: 0.5);
Implementers

Constructors

SegmentationMask({required Float32List data, required int width, required int height, required int originalWidth, required int originalHeight, List<double> padding = const [0.0, 0.0, 0.0, 0.0]})
Creates a segmentation mask with validation.
factory
SegmentationMask.fromMap(Map<String, dynamic> map)
Creates a SegmentationMask from a serialized map (isolate deserialization).
factory

Properties

data Float32List
Returns a copy of the raw probability data (row-major order).
no setter
hashCode int
The hash code for this object.
no setterinherited
height int
Mask height in pixels.
final
originalHeight int
Original source image height.
final
originalWidth int
Original source image width.
final
padding List<double>
Padding applied during letterboxing [top, bottom, left, right]. All zeros if no letterboxing was used.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
width int
Mask width in pixels.
final

Methods

at(int x, int y) double
Returns probability at (x, y) in mask coordinates.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toBinary({double threshold = 0.5}) Uint8List
Converts to binary mask (0 or 255) using threshold.
toMap() Map<String, dynamic>
Serialization for isolate transfer.
toRgba({int foreground = 0xFFFFFFFF, int background = 0x00000000, PixelFormat format = PixelFormat.rgba, double threshold = 0.5}) Uint8List
Converts to RGBA image with configurable colors.
toString() String
A string representation of this object.
override
toUint8() Uint8List
Converts to 8-bit grayscale mask (0-255).
upsample({int? targetWidth, int? targetHeight, int maxSize = 2048}) SegmentationMask
Upsamples mask to target dimensions using bilinear interpolation.

Operators

operator ==(Object other) bool
The equality operator.
inherited