Using console to create annotation box in specific location of image

Hi all,

I’m very new to programming and still not very good at this.

I’m trying to use the console to create annotation box in specific areas of my images.

For example like this. I’m trying to separate the image into 4 sections as the intensity of the image is not even so I cannot use a general threshold setting for the image.

I’ve tried following the guide from pete

but I get an error
ERROR: MissingMethodException at line 14: No signature of method: qupath.lib.images.servers.bioformats.BioFormatsImageServer.getAveragedPixelSizeMicrons() is applicable for argument types: () values:

ERROR: org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(
java.base/java.util.concurrent.Executors$ Source)
java.base/ Source)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.base/java.util.concurrent.ThreadPoolExecutor$ Source)
java.base/ Source)

Can anyone advice me on this?



That script is from earlier versions of QuPath. Pixel size can now be found or set slightly differently.
The readthedocs site should be the first place to check as it is probably going to be the most up to date.


double pixelSizeMicrons= getCurrentImageData().getServer().getPixelCalibration().getAveragedPixelSizeMicrons();

Thanks. I finally got it to work as intended.

1 Like

After a few days I’m stuck at this next part.

I got the script to draw 4 annotation box in my image to split it into 4 sections. I’m now trying to figure out how to label each annotation box with a different classification.

import qupath.lib.objects.PathObjects
import qupath.lib.roi.ROIs
import qupath.lib.regions.ImagePlane

def imageData = getCurrentImageData()
def plane = getCurrentViewer().getImagePlane()
def server = imageData.getServer()
int tileSize = 2000 

def tiles = []
for (int y = 0; y < server.getHeight() - tileSize; y += tileSize) {
    for (int x = 0; x < server.getWidth() - tileSize; x += tileSize) {
        def roi = ROIs.createRectangleROI(x, y, tileSize, tileSize, plane)
        tiles << PathObjects.createAnnotationObject(roi)

//rename each box to different class (eg 1, 2, 3 and 4)

My next part is to select each annotation box and perform cell detection. As each area of the image have different brightness, I have to adjust the threshold for each annotation box.

//Selecting box based on classification 
def label    = PathClassFactory.getPathClass("this is going to be the name of the annotation box")
selectObjectsByClassification("this is going to be the name of the annotation box")
//Cell detection script from the workflow
runPlugin('qupath.imagej.detect.cells.WatershedCellDetection', '{"detectionImage": "Ch1-T4",  "requestedPixelSizeMicrons": 0.5,  
"backgroundRadiusMicrons": 8.0,  "medianRadiusMicrons": 0.0,  "sigmaMicrons": 1.5,  "minAreaMicrons": 10.0,  "maxAreaMicrons": 400.0,  
"threshold": 500.0,  "watershedPostProcess": true,  "cellExpansionMicrons": 5.0,  "includeNuclei": true,  "smoothBoundaries": true,  "makeMeasurements": true}')

Finally I’m going to runObjectClassifier to highlight the cells that I want. Unfortunately QuPath’s workflow does not include the command. Can someone please give me a hint so I can adjust this?

Thanks for your help.


The class can be set when creating the annotation, instead of just passing the roi, pass the roi and a class.
PathObjects.createAnnotationObject(roi, getPathClass("Tumor"))
You can also adjust the threshold within a script -

Thanks for the code but I’m trying to assign a different class to each annotation.

Yes, that is how you would do it - the Tumor was just an example. You may want to use an integer as a string or something else entirely, you just need to iterate it along with the rest of your for loop.