I am trying to determine the exact distance between each cell in a certain subpopulation in an annotation and the outer edge of the annotation. For example, to find the distance between the each violet cell and the yellow outer edge.(https://i.stack.imgur.com/Q0p7v.png)
I tried this script from another website that was supposed to measure the distance between objects and their nearest annotations:
I got two outputs from this script, one is the "distance in um to nearest null" which was 0 and the second was the "distance in um to nearest violet cell" which was also 0.(https://i.stack.imgur.com/bUPSW.png)
Update: I have also tried the "Signed distance to annotations 2D" function and gotten the same output
print "Caution, this may take some time for large numbers of objects."
print "If the time is excessive for your project, you may want to consider size thresholding some objects, or adjusting the objectsToCheck"
objectsToCheck = getAllObjects().findAll{it.isDetection() || it.isAnnotation()}
PrecisionModel PM = new PrecisionModel(PrecisionModel.FIXED)
classList = objectsToCheck.collect{it.getPathClass()} as Set
def cal = getCurrentServer().getPixelCalibration()
if (cal.pixelWidth != cal.pixelHeight) {
println "Pixel width != pixel height ($cal.pixelWidth vs. $cal.pixelHeight)"
println "Distance measurements will be calibrated using the average of these"
}
Map combinedClassObjects = [:]
classList.each{c->
currentClassObjects = getAllObjects().findAll{it.getPathClass() == c}
geom = null
currentClassObjects.eachWithIndex{o, i->
if(i==0){geom = o.getROI().getGeometry()}else{
geom = GeometryPrecisionReducer.reduce(geom.union(o.getROI().getGeometry()), PM)
//geom =(geom.union(o.getROI().getGeometry())).buffer(0)
}
}
combinedClassObjects[c] = geom
}
objectsToCheck.each{ o ->
//Store the shortest non-zero distance between an annotation and another class of annotation
def g1 = o.getROI().getGeometry().buffer(0)
//If there are multiple annotations of the same type, prevent checking distances against itself
combinedClassObjects.each{cco->
combinedGeometry = cco.value
if (o.getPathClass() == cco.key){
combinedGeometry= combinedGeometry.difference(GeometryPrecisionReducer.reduce(g1, PM))
//combinedGeometry= (combinedGeometry.difference(g1)).buffer(0)
//print "internal"
}
double distancePixels = g1.distance(combinedGeometry)
double distanceCalibrated = distancePixels * cal.getAveragedPixelSize()
o.getMeasurementList().putMeasurement("Distance in um to nearest "+cco.key, distanceCalibrated)
}
}
import org.locationtech.jts.precision.GeometryPrecisionReducer
import org.locationtech.jts.geom.PrecisionModel```