Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Replace (selected) annotations with ROIs containing holes with filled-in versions.
- *
- * @author Pete Bankhead
- */
- import qupath.lib.objects.PathAnnotationObject
- import qupath.lib.roi.AreaROI
- import qupath.lib.roi.PathROIToolsAwt
- import qupath.lib.scripting.QPEx
- // Get selected objects
- // If you're willing to loop over all annotation objects, for example, then use getAnnotationObjects() instead
- def pathObjects = QPEx.getSelectedObjects()
- // Create a list of objects to remove, and their replacements
- def toRemove = []
- def toAdd = []
- for (pathObject in pathObjects) {
- def roi = pathObject.getROI()
- // AreaROIs are the only kind that might have holes
- if (roi instanceof AreaROI) {
- // Extract exterior polygons
- def polygons = PathROIToolsAwt.splitAreaToPolygons(roi)[1] as List
- // If we have multiple polygons, merge them
- def roiNew = polygons.remove(0)
- for (temp in polygons)
- roiNew = PathROIToolsAwt.combineROIs(roiNew, temp, PathROIToolsAwt.CombineOp.ADD)
- // Create a new annotation
- toAdd << new PathAnnotationObject(roiNew, pathObject.getPathClass())
- toRemove << pathObject
- }
- }
- // Remove & add objects as required
- def hierarchy = QPEx.getCurrentHierarchy()
- hierarchy.getSelectionModel().clearSelection()
- hierarchy.removeObjects(toRemove, true)
- hierarchy.addPathObjects(toAdd, false)
Add Comment
Please, Sign In to add comment