Skip to content

Instantly share code, notes, and snippets.

View petebankhead's full-sized avatar

Pete petebankhead

View GitHub Profile
@petebankhead
petebankhead / QuPath-Import binary masks.groovy
Created March 13, 2018 12:27
Script to import binary masks & create annotations (see also QuPath-Export binary masks.groovy)
/**
* Script to import binary masks & create annotations, adding them to the current object hierarchy.
*
* It is assumed that each mask is stored in a PNG file in a project subdirectory called 'masks'.
* Each file name should be of the form:
* [Short original image name]_[Classification name]_([downsample],[x],[y],[width],[height])-mask.png
*
* Note: It's assumed that the classification is a simple name without underscores, i.e. not a 'derived' classification
* (so 'Tumor' is ok, but 'Tumor: Positive' is not)
*
@petebankhead
petebankhead / QuPath-Exporting annotations as labelled images.groovy
Created March 14, 2018 23:01
Script to export pixels & annotations for whole slide images, optionally with tiling.
/**
* Script to export pixels & annotations for whole slide images.
*
* The image can optionally be tiled during export, so that even large images can be exported at high resolution.
* (Note: In this case 'tiled' means as separate, non-overlapping images... not a single, tiled pyramidal image.)
*
* The downsample value and coordinates are encoded in each image file name.
*
* The annotations are exported as 8-bit labelled images.
* These labels depend upon annotation classifications; a text file giving the key is written for reference.
@petebankhead
petebankhead / QuPath-Exporting annotations as labelled images (TMA version).groovy
Created March 14, 2018 23:03
Script to export pixels & annotations for tissue microarrays
/**
* Script to export pixels & annotations for TMA images.
*
* The downsample value and coordinates are encoded in each image file name.
*
* The annotations are exported as 8-bit labelled images.
* These labels depend upon annotation classifications; a text file giving the key is written for reference.
*
* The labelled image can also optionally use indexed colors to depict the colors of the
* original classifications within QuPath for easier visualization & comparison.
@petebankhead
petebankhead / QuPath-Count project annotations.groovy
Created March 15, 2018 22:12
Count the annotations for each classification, across all images in a project.
/**
* Count the annotations for each classification, for all images in a project.
*
* This works by looping through all the images in a project, and checking for the existence of a data file.
*
* If a data file is found, read the hierarchy (no need to open the whole image), and print the number of objects,
* the total number of annotations & the number of annotations split by classification.
*
* @author Pete Bankhead
*/
@petebankhead
petebankhead / QuPath-Update project paths.groovy
Created June 21, 2018 18:37
Update the paths to images in a QuPath project
/**
* Update paths within a QuPath project to handle images that have been moved.
*
* This works by first finding image paths that don't point to any existing files,
* and then prompting the user to select a base directory below which the script
* will search for image files with the same names.
*
* The process is a bit cumbersome, partly because there is no easy way to set
* the path for an existing ProjectImageEntry.
*
@petebankhead
petebankhead / QuPath_custom_classifier_demo.groovy
Created June 30, 2018 07:33
Create a QuPath classifier by scripting, rather than the 'standard' way interactively.
/**
* Create a QuPath classifier by scripting, rather than the 'standard' way with annotations.
*
* This selects training regions according to a specified criterion based on staining,
* and then creates a classifier that uses other features.
*
* The main aim is to show the general idea of creating a classifier by scripting.
*
* @author Pete Bankhead
*/
@petebankhead
petebankhead / QuPath_Objects_to_IJ_ROIs.groovy
Created June 30, 2018 14:30
Export ROIs made in QuPath as .zip files that can be imported into ImageJ
/**
* Export ROIs made in QuPath as .zip files that can be imported into ImageJ.
*
* This makes several assumptions:
* - the rois apply to the whole image, without offsets or downsampling
* - only the outer ROI for cells is export (rather than the outer ROI + nucleus)
* - the image is 2D (i.e. it doesn't attempt to set z-slice properties)
*
* The ROIs will be put into a 'rois' subdirectory of the QuPath project directory.
*
@petebankhead
petebankhead / QuPath-Extract_label.groovy
Last active March 31, 2021 18:50
Try to extract the label from an image in QuPath v0.2 and display it in ImageJ with enhanced contrast
/**
* Try to extract the label from an image and show this with enhanced contrast using ImageJ as the viewer.
*
* This is mostly intended for .czi files, where currently the label seems to be 16-bit
* and displays as entirely black in QuPath.
*
* It assumes that the label exists as an 'associated image' within the server.
*
* This has been tested with QuPath v0.2 - it may not work with other versions.
*
@petebankhead
petebankhead / QuPath-Fill_annotation_holes.groovy
Created July 18, 2018 18:38
Replace (selected) annotations with ROIs containing holes with filled-in versions.
/**
* Replace (selected) annotations with ROIs containing holes with filled-in versions.
*
* @author Pete Bankhead
*/
import qupath.lib.objects.PathAnnotationObject
import qupath.lib.roi.AreaROI
import qupath.lib.roi.PathROIToolsAwt
import qupath.lib.scripting.QPEx
@petebankhead
petebankhead / QuPath-Show whole slide overlay.groovy
Created July 20, 2018 15:44
Proof-of-concept script showing one whole slide image as an overlay on top of another within QuPath.
/**
* Proof-of-concept script showing one whole slide image as an overlay on top
* of another within QuPath.
*
* Note that this script simply overlays the *same* image that is currently open;
* therefore it may not be immediately obvious that anything has happened.
*
* Uncomment the line
* g2d.translate(1000, 1000)
* if you want to convince yourself that something *has* actually happened,