public final class SegmentationMask
- Object
- SegmentationMask
Dense per-pixel foreground confidence mask. The confidence array is
row-major with exactly width * height values in the range 0..1.
It is defensively copied on construction and access.
The mask’s resolution is the segmentation model’s, not the source image’s,
so it is normally smaller than the frame it describes.
cutOut(Image, float) and toMaskImage(int) rescale it for
you; when reading getConfidence() directly, index it with
getWidth() and getHeight() rather than the frame size.
SelfieSegmenter segmenter = new SelfieSegmenter();
segmenter.process(VisionImage.encoded(jpeg)).ready(mask -> {
Image person = mask.cutOut(EncodedImage.create(jpeg), 0.6f);
background.getStyle().setBgImage(person);
}).except(error -> Log.e(error));
Constructors
public SegmentationMask(int width, int height, float[] confidence) | Creates a row-major confidence mask without backend metadata. |
public SegmentationMask(int width, int height, float[] confidence, VisionMetadata metadata) | Creates a row-major confidence mask with backend diagnostics. |
Methods
public int getWidth() | |
public int getHeight() | |
public float[] getConfidence() | |
public float getConfidenceAt(int x, int y) | Reads one mask pixel. |
public Image cutOut(Image source, float threshold) | Keeps the foreground of an image and makes the rest transparent. |
public Image toMaskImage(int rgb) | Renders the mask itself as a translucent tint, for drawing over the frame it describes as a foreground highlight. |
public VisionMetadata getMetadata() |
Inherited methods
Constructor details
SegmentationMask
public SegmentationMask(int width, int height, float[] confidence)Parameters
widthint- mask width in pixels
heightint- mask height in pixels
confidencefloat[]- one foreground probability per pixel, defensively copied
Throws
IllegalArgumentException- if either dimension is negative, the
pixel count exceeds the maximum Java array length, or
confidencedoes not contain exactly one value per pixel
SegmentationMask
public SegmentationMask(int width, int height, float[] confidence, VisionMetadata metadata)Parameters
widthint- mask width in pixels
heightint- mask height in pixels
confidencefloat[]- one foreground probability per pixel, defensively copied
metadataVisionMetadata- backend details, or
null
Throws
IllegalArgumentException- if either dimension is negative, the
pixel count exceeds the maximum Java array length, or
confidencedoes not contain exactly one value per pixel
Method details
getWidth
public int getWidth()Returns
getHeight
public int getHeight()Returns
getConfidence
public float[] getConfidence()Returns
getConfidenceAt
public float getConfidenceAt(int x, int y)Parameters
xint- column in the range 0 to
getWidth()- 1 yint- row in the range 0 to
getHeight()- 1
Returns
Throws
IndexOutOfBoundsException- if the position is outside the mask
cutOut
public Image cutOut(Image source, float threshold)Keeps the foreground of an image and makes the rest transparent.
The mask is sampled with nearest-neighbour scaling, so the result has
source’s dimensions no matter what resolution the segmentation
model produced. Pixels whose confidence is below threshold
become fully transparent; the rest keep their original color and alpha.
Parameters
sourceImage- the image the mask was computed from
thresholdfloat- minimum foreground confidence to keep, clamped to 0..1
Returns
Throws
NullPointerException- if
sourceisnull IllegalArgumentException- if this mask has no pixels
toMaskImage
public Image toMaskImage(int rgb)Renders the mask itself as a translucent tint, for drawing over the frame it describes as a foreground highlight.
Each pixel takes rgb as its color and the mask confidence as
its alpha, so a fully confident foreground pixel is opaque and a
background pixel is invisible. The result has the mask’s own
resolution; draw it scaled to the frame’s bounds.
Parameters
rgbint- tint color as
0xRRGGBB; any alpha in the value is ignored in favor of the mask
Returns
Throws
IllegalArgumentException- if this mask has no pixels
getMetadata
public VisionMetadata getMetadata()Returns
null when manually constructed