Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Exported Aperio Image Scope Annotations to QuPath Annotations
import qupath.lib.scripting.QP
import qupath.lib.geom.Point2
import qupath.lib.roi.PolygonROI
import qupath.lib.objects.PathAnnotationObject
import qupath.lib.images.servers.ImageServer
//Aperio Image Scope displays images in a different orientation
def rotated = true
def server = QP.getCurrentImageData().getServer()
def h = server.getHeight()
def w = server.getWidth()
// need to add annotations to hierarchy so qupath sees them
def hierarchy = QP.getCurrentHierarchy()
//Prompt user for exported aperio image scope annotation file
def file = getQuPath().getDialogHelper().promptForFile('xml', null, 'aperio xml file', null)
def text = file.getText()
def list = new XmlSlurper().parseText(text)
list.Annotation.each {
it.Regions.Region.each { region ->
def tmp_points_list = []
region.Vertices.Vertex.each{ vertex ->
if (rotated) {
X = vertex.@Y.toDouble()
Y = h - vertex.@X.toDouble()
}
else {
X = vertex.@X.toDouble()
Y = vertex.@Y.toDouble()
}
tmp_points_list.add(new Point2(X, Y))
}
def roi = new PolygonROI(tmp_points_list)
def annotation = new PathAnnotationObject(roi)
hierarchy.addPathObject(annotation, false)
}
}
@DanaCase

This comment has been minimized.

Copy link
Owner Author

DanaCase commented Jan 24, 2018

Before running this script for the first time, you need to drag the groovy-xml.jar (found in the groovy-sdk lib folder) onto the QuPath window. This copies the jar into QuPath's jar directory.

@sankrish93

This comment has been minimized.

Copy link

sankrish93 commented Mar 9, 2018

Hi ! thank you so much for this code ! however when I run this code, the annotations seem to be appearing outside the image(specifically above the image instead of on it) In your opinion, do you think this might be due to an internal error or is there some fix to be made to the code before running it for the first time?

@DanaCase

This comment has been minimized.

Copy link
Owner Author

DanaCase commented Mar 14, 2018

It seems like somehow there is a mismatch between your aperio annotations x, y coords and the QuPath x, y coords? I would guess either scaling or rotation? It's kinda hard to tell without seeing the image.

Do you have two 'images' in the QuPath folder? Sometimes I have to open the actual image instead of the 'maco' image depending on the file type.

It may also be that QuPath is rotating the image? You can set the rotate param near the top of the script to false to try that out.

The last thing that i can think of is that there is some scaling factor? You can try to figure out what it is and multiply the x and y by it?

Let me know what you come up with :)

@gagarin37

This comment has been minimized.

Copy link

gagarin37 commented Apr 19, 2018

Dear DanaCase,

thanks a lot for this srcipt. This imports the annotations very nice.
However I have the same problem as sankrish93. They are all above the image, and rotation seems not to be the case.
I also attach the image.
I would very appreciate if you can give me some ideas how to improve the situation..
screenshot from 2018-04-19 21-57-10

@gagarin37

This comment has been minimized.

Copy link

gagarin37 commented Apr 21, 2018

Dear DanaCase,

the rotation seems to be the problem. It seems to be so, that several images go with rotated = "false" and several with "true" and than the correspondence is perfect.

Thank's a lot once more time.

All best.

@JohnKPan

This comment has been minimized.

Copy link

JohnKPan commented Jan 14, 2019

I modified this script to import NDPA files from NDPVIEW.

It however involves some really hacky stuff because the NDPA coordinate system is relative to physical slide center, and not top left of scanned image. It works though. Hopefully someone will figure out how to get the necessary information from OpenSlide to make it simpler.

You will need the Groovy-xml.jar as well.

https://groups.google.com/forum/#!searchin/qupath-users/import$20annotation%7Csort:date/qupath-users/xhCx_nhbWQQ/0kW38lEXCAAJ

@SteveBoothHLI

This comment has been minimized.

Copy link

SteveBoothHLI commented Nov 20, 2019

Hi there, this is working perfectly for me now! However, in Aperio we have given our annotations names for the classes ("Airway", "Vessel"), these are stored in the xml file under name attributes. Is there a way to automatically set the classes of the imported annotations to match those from the Aperio xml?

Also I noticed that the order of the annotations is different each time I run the import - is this expected?
Thanks,
Steve

@Sabrina1948

This comment has been minimized.

Copy link

Sabrina1948 commented May 13, 2020

Hi all,
I have lots of xmls in folder. i want to import them on corresponding images. A batch import i mean. Would please help me in this regard? the script can only import one xml at a time while i got 300+ xmls.
Many thanks in advance.
Sincerely,
Sabrina

@Svidro

This comment has been minimized.

Copy link

Svidro commented May 21, 2020

In case @SteveBoothHLI or others want the name import included, Sabrina1948 opened a post on image.sc that ended up resolving the issue.
https://forum.image.sc/t/labelled-annotation-import/37787/11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.