Skip to content

Instantly share code, notes, and snippets.

@petebankhead
Created November 20, 2021 17:01
Show Gist options
  • Save petebankhead/27c1f8cd950583452c756f3a2ea41fc0 to your computer and use it in GitHub Desktop.
Save petebankhead/27c1f8cd950583452c756f3a2ea41fc0 to your computer and use it in GitHub Desktop.
QuPath script to apply a global threshold to an image channel, without relying on a saved PixelClassifier
/**
* QuPath script to threshold a single channel of an image.
* This can also be useful to convert a binary image into QuPath annotations.
*
* First written for https://forum.image.sc/t/rendering-wsi-as-overlay-on-top-of-another-wsi/52629/24?u=petebankhead
*
* @author Pete Bankhead
*/
int channel = 0 // 0-based index for the channel to threshold
double threshold = 0.5 // Threshold value
int level = 2 // 0-based resolution level for the image pyramid (choosing 0 may be slow)
def belowClass = getPathClass('Ignore*') // Class for pixels below the threshold
def aboveClass = getPathClass('Thresholded') // Class for pixels above the threshold
// Create a single-resolution server at the desired level, if required
def server = getCurrentServer()
if (level != 0) {
server = qupath.lib.images.servers.ImageServers.pyramidalize(server, server.getDownsampleForResolution(level))
}
// Create a thresholded image
def thresholdServer = PixelClassifierTools.createThresholdServer(server, channel, threshold, belowClass, aboveClass)
// Create annotations and add to the current object hierarchy
def hierarchy = getCurrentHierarchy()
PixelClassifierTools.createAnnotationsFromPixelClassifier(hierarchy, thresholdServer, -1, -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment