Error when exporting region of the image in script

Hi all,
Currently I try to export a region of the image using following script. The annotation is already created.

`
import qupath.lib.images.servers.LabeledImageServer

def server = getCurrentServer()

def imageData = getCurrentImageData()
def name = GeneralTools.getNameWithoutExtension(imageData.getServer().getMetadata().getName())
def pathOutput = buildFilePath(PROJECT_BASE_DIR, 'export', name)
mkdirs(pathOutput)
def outputPath = buildFilePath    (pathOutput, 'original ' + '.png')
// Write the region of the image corresponding to the currently-selected object
def roi = getSelectedROI()
def requestROI = RegionRequest.createInstance(server.getPath(), 1, roi)
writeImageRegion(server, requestROI,outputPath)

However, this scrpit won’t work if I dont manually select this annotation. Here are the error message.

ERROR: GroovyRuntimeException at line 13: Ambiguous method overloading for method qupath.lib.regions.RegionRequest#createInstance.

I’ve tried command selectAnnotations() ,anyway it doesn’t work in this way.

I believe this must be a quick question however I 've struggle for quite a long time.
Thanks in advance and looking forward for any help and suggestions.

Shan

Currently your roi is from getSelectedROI(), so it makes sense that you need to have one object selected for the script to work.

More complex options require more complex solutions - for example, if you use selectAnnotations, you get a list of annotations, regardless of whether that list has only one object. And if you have two annotations, would you want to overwrite the first annotation with the second? Or are you completely sure you will always have only one annotation?

def server = getCurrentServer()

def imageData = getCurrentImageData()
def name = GeneralTools.getNameWithoutExtension(imageData.getServer().getMetadata().getName())
def pathOutput = buildFilePath(PROJECT_BASE_DIR, 'export', name)
mkdirs(pathOutput)

// Cycle through all existing annotations and export one original image per object
annotations = getAnnotationObjects()
annotations.eachWithIndex{a,i->
def outputPath = buildFilePath    (pathOutput, 'original '+ i + '.png')
def roi = a.getROI()
def requestROI = RegionRequest.createInstance(server.getPath(), 1, roi)
writeImageRegion(server, requestROI,outputPath)

}

This version cycles through all annotations - and omits the labelserver import since that did not seem to be used.

1 Like

Hi @Shan,

@Research_Associate describes the basic problem: getSelectedROI() is ambiguous if you have more than one annotation selected.

QuPath really has two different kinds of selection. One is a collection of (zero or more) selected objects, and the other is the ‘main’ selected object (zero or one, but never more). If you have a main selected object, it’s always also included as part of the collection. The main selected object is also the one you can usually edit in the viewer (assuming it’s an annotation, and it isn’t locked).

getSelectedROI() gets you the ROI from the main selected object.

The trouble with selectAnnotations() is that it sets the collection of selected objects only… but it doesn’t designate any of them as the main selected object. Arguably it should – at least if there’s just one annotation there – but currently it doesn’t.

So if you know you’ll only ever have one annotation in your image, you can use

def roi = getAnnotationObjects()[0].getROI()

to access it.

Alternatively, you can use a script based on the one here: Exporting annotations — QuPath 0.2.3 documentation
That should loop through all your annotations – exporting the regions, regardless of how many annotations are present.

1 Like

Thanks a lot for your quick reply and explanation. Now the new code works perfectly with the code given by @Research_Associate.

Just a quick further question, just can’t figure it out myself. As written in the link, does the annotation have to be defined to one classification?
If my annotation doesn’t belong to any classifications, how should I adjust the code accordingly?
I always get pure white image exported using the method showed in the link…
Thank you in advance:)
Shan

The link was to a labelserver - outputting a mask. That only applies if you have classified annotations. Unclassified annotations have the class null, but do not have a text representation. You can quickly set annotations to a class using the interface, Annotations tab, or default classify all annotations as a class in the export script:

getAnnotationObjects().each{it.setPathClass(getPathClass("Default"))}

The script in the posts above was for an original image. What are you trying to do?

Thanks for your reply. I was trying to export a image of the region I annotated, which is already perfectly solved with your code above.

I still want to export a overview image which includes the image of the whole sample with the annotation (only contour shows)to show what is selected for ROI and what is not.
It’s quite easy to export a snapshot, however is it possible to do it by scripting?

That should be doable using a renderedImageServer, see:Exporting images — QuPath 0.2.3 documentation

and: QuPath Measurement Maps Image Export Script - #2 by Research_Associate

Thanks! I believe all my questions are now addressed.
Greatful for your time and all your suggestions!

2 Likes