Skip to content

Instantly share code, notes, and snippets.

@lacan
Created April 1, 2022 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacan/060f09d0a20be4f11e2c91e58f6a9969 to your computer and use it in GitHub Desktop.
Save lacan/060f09d0a20be4f11e2c91e58f6a9969 to your computer and use it in GitHub Desktop.
[Save PathObjects instances as label masks] Saves the selected type of PathObject as a label image. Useful for training Deep Models where the ground truth is annotated in QuPath#QuPath
// Save the whole Image
def downsample = 4
// Save the whole Image
def request = RegionRequest.createInstance( getCurrentServer(), downsample )
def imageData = getCurrentImageData()
// Get some data to know where to save and how to name the file
def imageName = GeneralTools.getNameWithoutExtension( getProjectEntry().getImageName() )
def saveDirectory = new File( getProjectBaseDirectory()+"/masks" )
saveDirectory.mkdirs()
// Create a LabeledImageServer
def labelServer = new LabeledImageServer.Builder( imageData )
.backgroundLabel(0, ColorTools.BLACK)
.multichannelOutput(false)
.useInstanceLabels()
.useCellNuclei() // Choose 1
// .useDetections() // Choose 1
// .useCells() // Choose 1
// .useAnnotations() // Choose 1
.build()
// Final mask file location
File maskFile = new File(saveDirectory, imageName + "_masks.tif")
// Yay, writing stuff
ImageWriterTools.writeImageRegion(labelServer, request, maskFile.getAbsolutePath())
logger.info("Saved image {}", maskFile.getName());
import qupath.lib.objects.PathCellObject;
import qupath.lib.images.servers.LabeledImageServer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment