use of io.github.mianalysis.mia.object.Measurement in project mia by mianalysis.
the class RelateManyToOne method calculateFractionalDistance.
public static void calculateFractionalDistance(Obj childObject, Obj parentObject, double minDist) {
// Calculating the furthest distance to the edge
if (parentObject.getMeasurement("MAX_DIST") == null) {
// Creating an image for the parent object
Image parentImage = parentObject.getAsImage("Parent", false);
Image distImage = DistanceMap.process(parentImage, "Distance", true, DistanceMap.WeightModes.WEIGHTS_3_4_5_7, true, false);
Image projectedImage = ProjectImage.projectImageInZ(distImage, "Projected", ProjectImage.ProjectionModes.MAX);
double maxDist = projectedImage.getImagePlus().getStatistics().max;
parentObject.addMeasurement(new Measurement("MAX_DIST", maxDist));
}
// Adding measurement
double maxDist = parentObject.getMeasurement("MAX_DIST").getValue();
double frac = Math.abs(minDist / maxDist);
String measurementName = getFullName(Measurements.DIST_CENT_SURF_FRAC, parentObject.getName());
childObject.addMeasurement(new Measurement(measurementName, frac));
}
use of io.github.mianalysis.mia.object.Measurement in project mia by mianalysis.
the class RelateManyToOne method applyLinkMeasurements.
static void applyLinkMeasurements(Objs parentObjects, Objs childObjects) {
String parentMeasurementName = getFullName(Measurements.WAS_LINKED, childObjects.getName());
String childMeasurementName = getFullName(Measurements.WAS_LINKED, parentObjects.getName());
for (Obj parentObject : parentObjects.values()) {
if (parentObject.getChildren(childObjects.getName()).size() == 0) {
parentObject.addMeasurement(new Measurement(parentMeasurementName, 0));
} else {
parentObject.addMeasurement(new Measurement(parentMeasurementName, 1));
}
}
for (Obj childObject : childObjects.values()) {
if (childObject.getParent(parentObjects.getName()) == null) {
childObject.addMeasurement(new Measurement(childMeasurementName, 0));
} else {
childObject.addMeasurement(new Measurement(childMeasurementName, 1));
}
}
}
use of io.github.mianalysis.mia.object.Measurement in project mia by mianalysis.
the class RelateManyToOne method linkByCentroidProximity.
public static void linkByCentroidProximity(Objs parentObjects, Objs childObjects, boolean linkInSameFrame, double linkingDistance, int nThreads) {
String moduleName = RelateObjects.class.getSimpleName();
String measurementNamePx = getFullName(Measurements.DIST_CENTROID_PX, parentObjects.getName());
String measurementNameCal = getFullName(Measurements.DIST_CENTROID_CAL, parentObjects.getName());
AtomicInteger count = new AtomicInteger(1);
int numberOfChildren = childObjects.size();
// Ensuring all parent objects have a calculated centroid
for (Obj parent : parentObjects.values()) parent.getMeanCentroid();
ThreadPoolExecutor pool = new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
for (Obj childObject : childObjects.values()) {
Runnable task = () -> {
double minDist = Double.MAX_VALUE;
Obj minLink = null;
double dpp = childObject.getDppXY();
for (Obj parentObject : parentObjects.values()) {
if (linkInSameFrame & parentObject.getT() != childObject.getT())
continue;
double dist = childObject.getCentroidSeparation(parentObject, true);
if (dist < minDist && dist <= linkingDistance) {
minDist = dist;
minLink = parentObject;
}
}
// Adding measurements to the input object
if (minLink != null) {
childObject.addParent(minLink);
minLink.addChild(childObject);
childObject.addMeasurement(new Measurement(measurementNamePx, minDist));
childObject.addMeasurement(new Measurement(measurementNameCal, minDist * dpp));
} else {
childObject.addMeasurement(new Measurement(measurementNamePx, Double.NaN));
childObject.addMeasurement(new Measurement(measurementNameCal, Double.NaN));
}
writeProgressStatus(count.getAndIncrement(), numberOfChildren, "objects", moduleName);
};
pool.submit(task);
}
pool.shutdown();
try {
// i.e. never terminate early
pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {
// Do nothing as the user has selected this
}
}
use of io.github.mianalysis.mia.object.Measurement in project mia by mianalysis.
the class RelateManyToOne method spatialOverlap.
public void spatialOverlap(Objs parentObjects, Objs childObjects, double minOverlap, boolean centroidOverlap, boolean linkInSameFrame) {
long nCombined = parentObjects.size() * childObjects.size();
long count = 0;
String overlapMeasurementName = getFullName(Measurements.OVERLAP_PC, parentObjects.getName());
// If there are no objects to link, just set all children to no link
if (nCombined == 0)
return;
// Runs through each child object against each parent object
for (Obj parentObject : parentObjects.values()) {
for (Obj childObject : childObjects.values()) {
// Testing if the two objects are in the same frame (if this matters)
if (linkInSameFrame && parentObject.getT() != childObject.getT())
continue;
// If requiring the child centroid is overlapped with the parent object
if (centroidOverlap) {
int xCent = (int) Math.round(childObject.getXMean(true));
int yCent = (int) Math.round(childObject.getYMean(true));
// Relates to image location
int zCent = (int) Math.round(childObject.getZMean(true, false));
Point<Integer> centroid = new Point<>(xCent, yCent, zCent);
// If the centroid doesn't overlap, skip this link
if (!parentObject.contains(centroid))
continue;
}
// Calculates the percentage overlap
double nTotal = (double) childObject.size();
double nOverlap = (double) parentObject.getOverlap(childObject);
double overlap = (nOverlap / nTotal) * 100;
// Testing the minimum overlap requirement
if (overlap == 0 || overlap <= minOverlap)
continue;
// If the tests are successful, addRef the link. If the child has already been
// linked, but with a smaller
// overlap, remove that link.
Obj oldParent = childObject.getParent(parentObject.getName());
if (oldParent != null) {
if (childObject.getMeasurement(overlapMeasurementName).getValue() < overlap) {
oldParent.removeChild(childObject);
} else {
// If the previous link had a better overlap, skip the assignment
continue;
}
}
// Creating the link
parentObject.addChild(childObject);
childObject.addParent(parentObject);
// Adding the overlap as a measurement
Measurement measurement = new Measurement(getFullName(Measurements.OVERLAP_PC, parentObject.getName()));
measurement.setValue(overlap);
childObject.addMeasurement(measurement);
}
writeStatus("Compared " + Math.floorDiv(100 * childObjects.size() * ++count, nCombined) + "% of pairs");
}
}
use of io.github.mianalysis.mia.object.Measurement in project mia by mianalysis.
the class TrackObjects method getMeasurementCost.
public static double getMeasurementCost(Obj prevObj, Obj currObj, String measurementName) {
Measurement currMeasurement = currObj.getMeasurement(measurementName);
Measurement prevMeasurement = prevObj.getMeasurement(measurementName);
if (currMeasurement == null || prevMeasurement == null)
return Double.NaN;
double currMeasurementValue = currMeasurement.getValue();
double prevMeasurementValue = prevMeasurement.getValue();
return Math.abs(prevMeasurementValue - currMeasurementValue);
}
Aggregations