/** * 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