Write masks to files

Description

Predictions are only half useful while they are an array in a session. This writes them as ordinary PNGs that open in anything - ImageJ, QuPath, a viewer, a colleague’s machine - and returns the paths, so it can sit at the end of a pipeline.

Usage

save_masks(masks, dir, colormap, names = NULL, suffix = "")

Arguments

masks

Class indices from predict.platypus_fit() - an image x height x width array, or one height x width matrix. Probabilities are accepted too and reduced to the most likely class.

For volumes use save_volumes(), which writes NIfTI carrying the geometry of the scan each mask came from. This function cannot reliably tell a stack of volumes from a stack of images with a class axis - the ranks are identical - so it does not try; it refuses only the case a shape can prove.

For overlapping classes use save_class_masks(). A prediction where some pixel is in two classes at once is refused here rather than reduced: one pixel gets one colour, so writing it as a colour map would mean choosing one of the classes it is in and leaving nothing in the file to say that happened.
dir Directory to write into; created if it does not exist.
colormap A list of RGB triples, one per class.
names File names, or the source image paths to take names from. Defaults to mask_0001.png and so on.
suffix Added before the extension, so predictions from different models can sit in one directory without overwriting each other.

Details

Files go where you say, not beside the images they came from. Writing into somebody’s dataset directory is a surprising thing for a function to do, and a second run against a different model would quietly mix its results in with the first.

Value

The paths written, invisibly.

Examples

library("rplatypus")

masks <- predict(fit, "unet", split = "test")
save_masks(masks, "predictions", binary_colormap, suffix = "_unet")