Skip to content

Instantly share code, notes, and snippets.

View petebankhead's full-sized avatar

Pete petebankhead

View GitHub Profile
@petebankhead
petebankhead / Create PyTorch launcher (Windows).groovy
Last active November 16, 2023 15:28
QuPath script to create a .bat file to launch QuPath using paths from a conda environment
/**
* Groovy script to generate a batch script to launch QuPath while setting the PATH
* from a conda environment.
*
* This is useful to add GPU support with Deep Java Library (DJL) without needing to install
* CUDA system-wide.
*
* This script was written for QuPath v0.5.
* You should read the top part and change the variables to match what you need.
*/
@petebankhead
petebankhead / QuPath-Threshold pixel classification.groovy
Created June 14, 2023 08:58
QuPath script to threshold pixel classification outputs with fixed (non-default) threshold values
/**
* QuPath v0.4.3 script to threshold probability outputs from a pixel classifier.
*
* By default, converting the pixel classifier output to probabilities will always
* apply a softmax operation and take the class with the highest probability.
*
* This script provides an alternative, whereby you can specify a probability threshold
* for one or more channels, and threshold to create objects from that.
* Using a higher threshold can then restrict the object creation of more confident predictions.
*
@petebankhead
petebankhead / QuPath-Export images for annotation bounding boxes.groovy
Created May 29, 2023 12:06
QuPath script to export images corresponding to all the annotation bounding boxes in an image
/**
* QuPath script to export images corresponding to all the annotation bounding boxes in an image.
* Written for QuPath v0.4.3.
*/
// Export at full resolution (or change this value)
double downsample = 1.0
// Export to a subdirectory of the current project
def dir = buildPathInProject("export")
@petebankhead
petebankhead / QuPath-Create surrounding annotation.groovy
Last active May 11, 2023 16:52
Interactively create a convex or concave hull annotation from the selected objects in QuPath v0.4.
/**
* Create a convex or concave hull annotation from the selected objects.
*
* The calculations are performed using Java Topology Suite's 'ConcaveHullOfPolygons' class -
* see https://locationtech.github.io/jts/javadoc/org/locationtech/jts/algorithm/hull/ConcaveHullOfPolygons.html
*
* This was written for QuPath v0.4.3.
* If it's useful enough, a similar feature might be added to QuPath directly in the future.
*
* @author Pete Bankhead
@petebankhead
petebankhead / QuPath-Find splits along ROI contours.groovy
Created April 13, 2023 13:42
Create a point annotation showing equally-spaced splits along a polyline or simple polygon
/**
* Create a point annotation showing equally-spaced splits
* along a polyline or simple polygon (i.e. no holes or
* self-intersections).
*
* See https://forum.image.sc/t/divide-free-hand-lines-at-regular-interval/79845/4
*
* Written for QuPath v0.4.3 using JTS.
*
* @author Pete Bankhead
@petebankhead
petebankhead / QuPath-Create rectangles for points.groovy
Last active December 11, 2023 15:47
Create fixed-sized square annotations around the points of a selected point annotation in QuPath
/**
* Create fixed-sized square annotations around the points of a selected
* point annotation.
*
* Written for QuPath v0.4 and v0.5.
* See https://forum.image.sc/t/is-it-possible-to-create-annotations-on-click/79270
*
* Edited 12/2023 to support multiple selected point annotations (not just one).
*
* @author Pete Bankhead
@petebankhead
petebankhead / QuPath-Intersect annotations.groovy
Created February 13, 2023 17:11
Constrain all selected annotations so that they fall inside unselected annotations
/**
* Constrain all selected annotations so that they fall inside unselected annotations.
*
* This is intended for a case where
* - you have some annotations that define a region of interest (e.g. all tissue)
* - some other annotations that partially overlap the first annotations, but which *should* be inside
*
* If you select the second annotations and run this script, it should remove the parts that are outside the first
* annotations - effectively cleaning up the image.
*
@petebankhead
petebankhead / QuPath-Convex hull around selected objects.groovy
Created February 4, 2023 05:52
Create a convex hull annotation around all selected objects in QuPath
/**
* Create a convex hull annotation surrounding all selected objects.
*
* Note that this
* - was written using QuPath v0.4.x (but probably works in some earlier releases)
* - doesn't work very well for ellipse objects, but should be ok with other shapes
* - assumes that you only have a 2D image (you'd need to change the 'image plane' part for z-stacks or timeseries)
*
* It was written for https://forum.image.sc/t/qupath-script-command-to-draw-polygon-annotation-from-points/76833
*
@petebankhead
petebankhead / QuPath-Extract hyperstack and overlay.groovy
Created January 19, 2023 19:05
QuPath script to extract a hyperstack + overlay and send it to ImageJ (written for v0.4.2)
/**
* QuPath script to extract a stack/hyperstack + overlay and send it to ImageJ.
*
* This will either extract a hyperstack for the bounding box of the current selected object,
* or for the entire image if no object is selected.
*
* Written for https://forum.image.sc/t/qupath-how-to-send-multiple-annotations-containing-more-annotations-to-imagej-with-all-z-slices-and-re-name/76107
*
* Tested (to an extent...) with QuPath v0.4.2
*
@petebankhead
petebankhead / QuPath-Add pixel classifier measurements to cells.groovy
Last active February 26, 2024 16:49
Add pixel classifier measurements to all cells in an image using QuPath
/**
* Add pixel classifier measurements to all cells in an image.
*
* This is intended to overcome the fact that measurements can be quite slow because they aren't parallelized.
*
* Written for QuPath v0.3.2.
* See https://forum.image.sc/t/qupath-measure-pixel-classifier-area-per-cell-detection-for-wsis/72701
*
* Warning! This hasn't been extensively tested - please report any problems on the forum.
*