Class 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));
-
Constructor Summary
ConstructorsConstructorDescriptionSegmentationMask(int width, int height, float[] confidence) Creates a row-major confidence mask without backend metadata.SegmentationMask(int width, int height, float[] confidence, VisionMetadata metadata) Creates a row-major confidence mask with backend diagnostics. -
Method Summary
Modifier and TypeMethodDescriptionKeeps the foreground of an image and makes the rest transparent.float[]floatgetConfidenceAt(int x, int y) Reads one mask pixel.intintgetWidth()toMaskImage(int rgb) Renders the mask itself as a translucent tint, for drawing over the frame it describes as a foreground highlight.
-
Constructor Details
-
SegmentationMask
public SegmentationMask(int width, int height, float[] confidence) Creates a row-major confidence mask without backend metadata.- Parameters:
width- mask width in pixelsheight- mask height in pixelsconfidence- one foreground probability per pixel, defensively copied- Throws:
IllegalArgumentException- if either dimension is negative, the pixel count exceeds the maximum Java array length, orconfidencedoes not contain exactly one value per pixel
-
SegmentationMask
Creates a row-major confidence mask with backend diagnostics.- Parameters:
width- mask width in pixelsheight- mask height in pixelsconfidence- one foreground probability per pixel, defensively copiedmetadata- backend details, ornull- Throws:
IllegalArgumentException- if either dimension is negative, the pixel count exceeds the maximum Java array length, orconfidencedoes not contain exactly one value per pixel
-
-
Method Details
-
getWidth
public int getWidth()- Returns:
- mask width, which may differ from source image width
-
getHeight
public int getHeight()- Returns:
- mask height, which may differ from source image height
-
getConfidence
public float[] getConfidence()- Returns:
- defensive copy of row-major foreground confidences
-
getConfidenceAt
public float getConfidenceAt(int x, int y) Reads one mask pixel.- Parameters:
x- column in the range 0 togetWidth()- 1y- row in the range 0 togetHeight()- 1- Returns:
- foreground probability in the range 0..1
- Throws:
IndexOutOfBoundsException- if the position is outside the mask
-
cutOut
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 belowthresholdbecome fully transparent; the rest keep their original color and alpha.- Parameters:
source- the image the mask was computed fromthreshold- minimum foreground confidence to keep, clamped to 0..1- Returns:
- a new image with the background removed
- Throws:
NullPointerException- ifsourceisnullIllegalArgumentException- if this mask has no pixels
-
toMaskImage
Renders the mask itself as a translucent tint, for drawing over the frame it describes as a foreground highlight.
Each pixel takes
rgbas 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:
rgb- tint color as0xRRGGBB; any alpha in the value is ignored in favor of the mask- Returns:
- a new image at this mask's resolution
- Throws:
IllegalArgumentException- if this mask has no pixels
-
getMetadata
- Returns:
- backend metadata, or
nullwhen manually constructed
-