Export annotations downsample

Hi,

I need to export annotations with a downsample factor something like this https://qupath.readthedocs.io/en/latest/docs/advanced/exporting_annotations.html#imagej-rois

However, I have annotations with multiple classes and need to maintain their classes.

To save the annotations I have been using the following code:

def path = buildFilePath(PROJECT_BASE_DIR, getProjectEntry().getImageName() + '_annotations' + '.txt')

double downsample = 8

def annotations = getAnnotationObjects().collect {new qupath.lib.objects.PathAnnotationObject(it.getROI(), it.getPathClass())}

new File(path).withObjectOutputStream {
    it.writeObject(annotations)
}

print 'Done!'

Can you please help me adapt the code above to include the downsample factor?

Thank you,
Daniela

Hi @dchan , I edited your post to format the script and make it easier to read.

It’s not clear to me why you want to export the annotations. The method you are using serializes them in a QuPath-only way… so you will only be able to read them back into QuPath.

Is that your intention? Or can you explain in more detail what your next intended step is after exporting annotations?

There are many many ways to interpret ‘export annotations’, so more details are really needed to be able to give a useful answer.

1 Like

Hi Pete,

I exported a downsampled version of my images to try some color normalization methods. Since I already have the original images annotated I wanted to reuse the annotations done in the original images in the downsampled version to train a classifier.

In the readthedocs I saw that when exporting ImageJ ROIs it was possible to downsample the annotations.

I was wondering if it is also possible to edit the code that I posted to export the annotations downsampled (as when exporting ImageJ ROIs) but preserving the annotations class. In the end, I want to read the annotations back to QuPath.

Thanks, the following script might help:

def path = buildFilePath(PROJECT_BASE_DIR, getProjectEntry().getImageName() + '_annotations' + '.txt')

double downsample = 8

def transform = java.awt.geom.AffineTransform.getScaleInstance(1.0/downsample, 1.0/downsample)

def annotations = getAnnotationObjects().collect {
    PathObjectTools.transformObject(it, transform, true)
}

new File(path).withObjectOutputStream {
    it.writeObject(annotations)
}

print 'Done!'

I haven’t tried it myself to export/import annotations, but it shows the process of transforming existing objects.

2 Likes

Thanks, @petebankhead! It works!!! :grinning:

2 Likes