Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petebankhead/9175c3def653d5acf18a8c4c0e28c3a1 to your computer and use it in GitHub Desktop.
Save petebankhead/9175c3def653d5acf18a8c4c0e28c3a1 to your computer and use it in GitHub Desktop.
Script to convert all polyline annotations to polygons in QuPath.
/**
* Script to convert all polyline annotations to polygons in QuPath.
* Written for QuPath v0.5 (may work in other versions).
*/
def lineObjects = getAnnotationObjects().findAll {it.getROI().isLine()}
def polygonObjects = []
def selectedObjects = getSelectedObjects() as List
for (lineObject in lineObjects) {
def line = lineObject.getROI()
def points = line.getAllPoints()
if (points.size() <= 2) {
println "Skipping line with <= 2 points"
continue
}
def polygon = ROIs.createPolygonROI(line.getAllPoints(), line.getImagePlane())
def polygonObject = PathObjects.createAnnotationObject(polygon, lineObject.getPathClass())
polygonObject.setName(lineObject.getName())
polygonObject.setColor(lineObject.getColor())
polygonObjects << polygonObject
// Update the selected objects
if (selectedObjects.remove(lineObject)) {
selectedObjects << polygonObject
}
}
removeObjects(lineObjects, true)
addObjects(polygonObjects)
selectObjects(selectedObjects)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment