Created
May 1, 2024 07:59
-
-
Save petebankhead/9175c3def653d5acf18a8c4c0e28c3a1 to your computer and use it in GitHub Desktop.
Script to convert all polyline annotations to polygons in QuPath.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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