Skip to content

Instantly share code, notes, and snippets.

View petebankhead's full-sized avatar

Pete petebankhead

View GitHub Profile
@petebankhead
petebankhead / QuPath-Show sparse server regions.groovy
Created February 10, 2022 17:28
QuPath script to create annotations corresponding to different regions in a training image
def server = getCurrentServer()
def manager = server.getManager()
def annotations = []
for (def region in manager.getRegions()) {
def roi = ROIs.createRectangleROI(region)
def server2 = manager.getServer(region, 1)
def name = server2.getMetadata().getName()
def pathObject = PathObjects.createAnnotationObject(roi)
pathObject.setName(name)
annotations << pathObject
@petebankhead
petebankhead / QuPath-Set cell names by annotation classes.groovy
Created February 5, 2022 07:51
QuPath script to set cell names to include the classifications of all annotations containing the cell
/**
* Script to set the name of a cell to include the classifications of
* all annotations that contain it, sorted by area.
* The purpose of this is to include information about all the annotations
* containing the cell in a measurement table, rather than only the immediate parent.
*
* Written for QuPath v0.3.
*/
// Get the current hierarchy and all cells
@petebankhead
petebankhead / QuPath-Convert nuclei to cells.groovy
Created January 6, 2022 17:29
QuPath-Convert nuclei to cells with measurements in QuPath v0.3.x
/**
* Convert detected nuclei to cells and add measurements.
* Written for v0.3.x to answer
* https://forum.image.sc/t/is-it-possible-to-modify-the-cell-expansion-parameter-after-cells-have-been-detected/61665
*
* Because adding intensity measurements is especially awkward, this should be improved in a future version.
*
* Note that the cell measurements are *not* equivalent to QuPath's built-in cell detection.
* They are calculated in a different way and have different names.
*
@petebankhead
petebankhead / QuPath-Apply global threshold.groovy
Created November 20, 2021 17:01
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
@petebankhead
petebankhead / QuPath-Extract photos.groovy
Created September 26, 2021 15:57
QuPath script to extract and unwarp manually-annotated rectangular regions from an image
/**
* Helper script to extract manually-annotated photos from an image,
* e.g. to help with digitising a photo album based upon scanning each page.
*
* The main thing this will do is to use the annotated polygons and perform
* a perspective transform to unwarp the photos.
*
* It can be applied in batch to many images.
*
* To use it
@petebankhead
petebankhead / QuPath-Visualize Delaunay clusters.groovy
Created July 28, 2021 07:13
Identify distinct clusters after using QuPath's 'Delaunay cluster features 2D' command
/**
* QuPath script to group together clusters identified with Delaunay clustering.
* This provides several options:
* - clusters as classifications
* - clusters as object names
* - clusters as measurement values
* You should comment out the lines you don't want.
*
* Note that this is *very* rough and not guaranteed to work.
* It was written for https://forum.image.sc/t/cell-classification-within-a-segmented-region-in-histology-images/55507/21?u=petebankhead
@petebankhead
petebankhead / QuPath-Check memory use.groovy
Created June 25, 2021 09:08
Print out various memory-related values in QuPath
/**
* Script to help track down memory leaks.
* Uses JavaCPP, some useful info at https://javadoc.io/static/org.bytedeco/javacpp/1.5.5/org/bytedeco/javacpp/Pointer.html
*
* @author Pete Bankhead
*/
import org.bytedeco.javacpp.Pointer
System.gc()
@petebankhead
petebankhead / QuPath-Find brightest circle.groovy
Created May 11, 2021 17:54
Find the 'brightest' circle in a fluorescence image in QuPath v0.2
/**
* Find the 'brightest' circle in a fluorescence image in QuPath v0.2.
*
* This could be used for creating ROIs in a TMA image where each core is a separate image.
*
* @author Pete Bankhead
*/
import ij.plugin.filter.RankFilters
@petebankhead
petebankhead / QuPath-Custom fluorescence threshold.groovy
Created May 11, 2021 17:35
One way to threshold a fluorescence multi-channel image in QuPath v0.2 to generate a new annotation
/**
* Slightly involved script to threshold a fluorescence multi-channel image
* in QuPath v0.2 to generate a new region of interest annotation.
*
* Might be used whenever there is too much variation for a threshold classifier
* to work.
*
* @author Pete Bankhead
*/
@petebankhead
petebankhead / QuPath-Set cell identifiers.groovy
Created May 10, 2021 06:32
Set IDs for cells in QuPath v0.2 / v0.3, so cells can be identified after export/import
/**
* Assign an ID to cells in QuPath using several different methods.
* This is useful if you want to export cell data from QuPath, process it elsewhere,
* and then bring the results back into QuPath again.
*
* Note: After checking the advantages/disadvantages, you should only need to use
* one of these. If it fits with your uses, my preference would be 3 or 4.
*/
// Get the cell objects (all options start here)