Converting a Polyline annotation into a Polygon

Hey everyone,

We have a lot of work of annotations done using the Polyline tool instead of the Polygon tool. Both seem to implement a list of vertices, just that polygons are closed and correctly get exported during another later step. Is there a way to write a script that would convert polyline annotations to a polygon type while maintaining the label? I know the overall sketch is something like:

annots = getAnnotationsObjects()
# Loop over all, check if polyline
x,y = annot.getVerticies
label = annot.getClassLabel()
new roi = new PolygonROI(x,y, label)
addObject(roi)
deleteObject(annot)

But for the life of me I cannot figure out how to do this in groovy…I’m new to scripting with qupath. Hopefully someone can help!

Andy

This should do it for all lines in the image (I assume they are polylines, but don’t check…):

def lineObjects = getAnnotationObjects().findAll {it.getROI().isLine()}
def polygonObjects = []
for (lineObject in lineObjects) {
    def line = lineObject.getROI()
    def polygon = ROIs.createPolygonROI(line.getAllPoints(), line.getImagePlane())
    def polygonObject = PathObjects.createAnnotationObject(polygon, lineObject.getPathClass())
    polygonObject.setName(lineObject.getName())
    polygonObject.setColorRGB(lineObject.getColorRGB())
    polygonObjects << polygonObject
}
removeObjects(lineObjects, true)
addObjects(polygonObjects)
1 Like

Thank you immensely!!

1 Like