Created
February 12, 2024 08:36
-
-
Save lacan/d87d27e10fc665e4836aa03cfe4f7986 to your computer and use it in GitHub Desktop.
[Replace detections as cells via erosion and add measurements] Erodes existing detection objects to create cells and adds all compartment measurements #qupath #groovy #detections
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
/** | |
* Reduce Detection size to create new cell objects and add measurements | |
* @author Olivier Burri | |
* @date 20240212 | |
*/ | |
// By how much should the existing detections be reduced to create the new "nucleus" | |
def erosionDistancePx = -5 | |
// Should the intensities be measured on a downsampled image? | |
def downsample = 1.0 | |
// START OF SCRIPT | |
// Required objects for adding compartment measurements later | |
def server = getCurrentServer() | |
// Choose all intensity measurements inside all compartments | |
def measurements = ObjectMeasurements.Measurements.values() as List | |
def compartments = ObjectMeasurements.Compartments.values() as List | |
// Loop through the annotations and find the detections, shrink them, create a new Cell object and measure | |
getAnnotationObjects().each{ annotation -> | |
def oldDetections = annotation.getChildObjects().findAll{ it instanceof qupath.lib.objects.PathDetectionObject } | |
def newDetections = oldDetections.collect{ d -> | |
// Previous Detection | |
def oldGeom = d.getROI().getGeometry() | |
// Nucleus created by erosion | |
def nuc = oldGeom.buffer( erosionDistancePx ) | |
// Cell outline is the old detection | |
def cell = oldGeom | |
// Create a new cell using the original ROI as "Cell" and the eroded ROI as "Nucleus" | |
def smaller = PathObjects.createCellObject( GeometryTools.geometryToROI( cell, d.getROI().getImagePlane() ), | |
GeometryTools.geometryToROI( nuc, d.getROI().getImagePlane()) , | |
d.getPathClass() // Keep the old PathClass | |
) | |
// Add the measurements | |
ObjectMeasurements.addIntensityMeasurements( server, smaller, 1.0, measurements, compartments ) | |
return smaller | |
} | |
// Remove the old objects | |
annotation.removeChildObjects( oldDetections ) | |
// Add the new objects | |
annotation.addChildObjects( newDetections ) | |
} | |
fireHierarchyUpdate() | |
// Necessary import | |
import qupath.lib.analysis.features.ObjectMeasurements |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment