Skip to content

Instantly share code, notes, and snippets.

@petebankhead
Last active December 11, 2023 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petebankhead/9741973f1a25b09919f1287bfe8de27b to your computer and use it in GitHub Desktop.
Save petebankhead/9741973f1a25b09919f1287bfe8de27b to your computer and use it in GitHub Desktop.
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
*/
// Define the size of the region to create
double sizeMicrons = 50
// We need a point ROI selected
def selectedPointObjects = getSelectedObjects().findAll {it.getROI()?.isPoint()}
if (selectedPointObjects.isEmpty()) {
println "Please select a ROI with points!"
return
}
// Prepare to convert size in microns to pixels - QuPath ROIs are defined in pixel units of the full-resolution image
def server = getCurrentServer()
double sizePixels = Math.round(sizeMicrons / server.getPixelCalibration().getAveragedPixelSizeMicrons())
// Create and add rectangles
def toAdd = []
// Loop through all the selected objects
for (def selected in selectedPointObjects) {
for (def p : selected.getROI().getAllPoints()) {
def roi = ROIs.createRectangleROI(p.x-sizePixels/2, p.y-sizePixels/2, sizePixels, sizePixels, selected.getROI().getImagePlane())
def annotation = PathObjects.createAnnotationObject(roi, selected.getPathClass())
toAdd << annotation
}
}
// Add the rectangle annotations
addObjects(toAdd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment