Created
February 13, 2023 17:11
-
-
Save petebankhead/ea90db2edd8bc0879c1713bb223b6096 to your computer and use it in GitHub Desktop.
Constrain all selected annotations so that they fall inside unselected annotations
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
/** | |
* Constrain all selected annotations so that they fall inside unselected annotations. | |
* | |
* This is intended for a case where | |
* - you have some annotations that define a region of interest (e.g. all tissue) | |
* - some other annotations that partially overlap the first annotations, but which *should* be inside | |
* | |
* If you select the second annotations and run this script, it should remove the parts that are outside the first | |
* annotations - effectively cleaning up the image. | |
* | |
* Written for v0.4.2. | |
* Note that this only works for 2D images (it isn't designed to support z-slices/timepoints). | |
* | |
* @author Pete Bankhead | |
*/ | |
import qupath.lib.roi.RoiTools | |
import static qupath.lib.gui.scripting.QPEx.* | |
// Get all selected annotations - these can be modified | |
def annotationsToIntersect = getSelectedObjects().findAll(p -> p.isAnnotation()) | |
// Get all the other annotations that aren't selected - these define the overall region | |
def otherAnnotations = getAnnotationObjects().findAll(p -> !annotationsToIntersect.contains(p)) | |
// Merge all the other annotations to create a single ROI | |
// Note that this assumes all annotations are on a single 2D plane! | |
def mergedROI = RoiTools.union(otherAnnotations.collect(p -> p.getROI())) | |
// Update all the selected annotations | |
for (def annotation : annotationsToIntersect) { | |
def roi = annotation.getROI() | |
def roi2 = RoiTools.intersection(mergedROI, roi) | |
// If we've got an empty ROI, remove hte annotation | |
if (roi2.isEmpty()) | |
removeObject(annotation, true) | |
else | |
annotation.setROI(roi2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment