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(ScriptBytecodeAdapter.java:70)
org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
Script40.run(Script40.groovy:15)
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:155)
qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:926)
qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:859)
qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:782)
qupath.lib.gui.scripting.DefaultScriptEditor$2.run(DefaultScriptEditor.java:1271)
java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
java.base/java.util.concurrent.FutureTask.run(Unknown Source)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.base/java.lang.Thread.run(Unknown Source)

Can anyone advice me on this?

Thanks.

Jason

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.
https://qupath.readthedocs.io/en/latest/docs/scripting/overview.html#accessing-image-metadata

setPixelSizeMicrons(Xsize,Ysize)

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

Thanks. I finally got it to work as intended.

1 Like

Hi,
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)
    }
}
addObjects(tiles)

//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.

Jason

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 -
https://gist.github.com/Svidro/5e4c29630e8d2ef36988184987d1028f#file-variable-into-string-commands-groovy

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.