Transfer of Annotations Problem

I am using the script below for saving the annotations;

def path = buildFilePath(PROJECT_BASE_DIR, ‘annotations’)
def annotations = getAnnotationObjects().collect {new qupath.lib.objects.PathAnnotationObject(it.getROI(), it.getPathClass())}
new File(path).withObjectOutputStream {
print ‘Done!’

and this one for applying the saved annotations onto the other images

def path = buildFilePath(PROJECT_BASE_DIR, ‘annotations’)
def annotations = null
new File(path).withObjectInputStream {
annotations = it.readObject()
print 'Added ’ + annotations

However, transfered ROI annotations sometimes go to black zones outside the slide area and I am not able to move them onto the slide area.
Is there a way to change the coordinates of the ROIs in order to move them onto the related areas?

I think this is generally the function of the alignment tool and script found in this thread:

Specifically the script here:

Otherwise your tissue images are likely each on different coordinate systems. I’m not sure about moving objects that are outside the image, but it might be possible to remove and recreate them with a script.

It’s a fairly extreme approach, but the following script will reposition all annotations within an image to the ‘origin’ (i.e. the top left):

def annotations = getAnnotationObjects()
def movedAnnotations = annotations.collect {
    def roi = it.getROI()
    return PathObjects.createAnnotationObject(
        roi.translate(-roi.getBoundsX(), -roi.getBoundsY()),
removeObjects(annotations, true)

Use with caution in its current form, but hope it helps!


I sometimes use that script however it moves all the objects and it is problem now for me since I have segmented millions of cells…
Thanks again, both you and Pete are brilliant.


Sounds good! If you ever want to change how the script functions, the line to change is this:

def pathObjects = otherHierarchy.getRootObject().getChildObjects()

You would want something like otherHierarchy.getRootObject().getChildObjects().findAll{it.isAnnotation()} or some class, or… whatever.

1 Like