Skip to content

Instantly share code, notes, and snippets.

@petebankhead
Created September 4, 2024 16:44
Show Gist options
  • Save petebankhead/6af031eeea3f176a32d3f9953323a06f to your computer and use it in GitHub Desktop.
Save petebankhead/6af031eeea3f176a32d3f9953323a06f to your computer and use it in GitHub Desktop.
QuPath-Create concentric rings around an annotation
/**
* QuPath script to create concentric rings around an annotation.
*/
// Number of rings to create
int nRings = 5
// Define distance in µm, then convert (or just go straight to defining in pixels)
double distanceMicrons = 100
double distancePixels = distanceMicrons / getCurrentServer().getPixelCalibration().getAveragedPixelSizeMicrons()
def currentROI = getSelectedROI()
def mergedROI = null
def newAnnotations = []
for (int i = 0; i < nRings; i++) {
// Expand the ROI
def nextROI = RoiTools.buffer(currentROI, distancePixels)
// Accumulate all the ROIs so that we can subtract them together
mergedROI = mergedROI == null ? currentROI : RoiTools.union(mergedROI, currentROI)
// Subtract the accumulated ROIs to get an outer ring
nextROI = RoiTools.subtract(nextROI, mergedROI)
// Create an annotation and add it to a list
newAnnotations << PathObjects.createAnnotationObject(nextROI)
// Prepare for the next iteration
currentROI = nextROI
}
// Add all our annotations
addObjects(newAnnotations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment