Guest User

Untitled

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. /**
  2. * Replace (selected) annotations with ROIs containing holes with filled-in versions.
  3. *
  4. * @author Pete Bankhead
  5. */
  6.  
  7. import qupath.lib.objects.PathAnnotationObject
  8. import qupath.lib.roi.AreaROI
  9. import qupath.lib.roi.PathROIToolsAwt
  10. import qupath.lib.scripting.QPEx
  11.  
  12. // Get selected objects
  13. // If you're willing to loop over all annotation objects, for example, then use getAnnotationObjects() instead
  14. def pathObjects = QPEx.getSelectedObjects()
  15.  
  16. // Create a list of objects to remove, and their replacements
  17. def toRemove = []
  18. def toAdd = []
  19. for (pathObject in pathObjects) {
  20. def roi = pathObject.getROI()
  21. // AreaROIs are the only kind that might have holes
  22. if (roi instanceof AreaROI) {
  23. // Extract exterior polygons
  24. def polygons = PathROIToolsAwt.splitAreaToPolygons(roi)[1] as List
  25. // If we have multiple polygons, merge them
  26. def roiNew = polygons.remove(0)
  27. for (temp in polygons)
  28. roiNew = PathROIToolsAwt.combineROIs(roiNew, temp, PathROIToolsAwt.CombineOp.ADD)
  29. // Create a new annotation
  30. toAdd << new PathAnnotationObject(roiNew, pathObject.getPathClass())
  31. toRemove << pathObject
  32. }
  33. }
  34.  
  35. // Remove & add objects as required
  36. def hierarchy = QPEx.getCurrentHierarchy()
  37. hierarchy.getSelectionModel().clearSelection()
  38. hierarchy.removeObjects(toRemove, true)
  39. hierarchy.addPathObjects(toAdd, false)
Add Comment
Please, Sign In to add comment