Created
April 1, 2022 13:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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