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 {
it.writeObject(annotations)
}
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()
}
addObjects(annotations)
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()),
        it.getPathClass()
    )
}
removeObjects(annotations, true)
addObjects(movedAnnotations)

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

2 Likes

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.

2 Likes

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