use of uk.ac.sussex.gdsc.smlm.ij.utils.ObjectAnalyzer in project GDSC-SMLM by aherbert.
the class SplitResults method splitResults.
private void splitResults(MemoryPeakResults results, ImageProcessor ip) {
IJ.showStatus("Splitting " + TextUtils.pleural(results.size(), "result"));
// Create an object mask
final ObjectAnalyzer objectAnalyzer = new ObjectAnalyzer(ip, false);
final int maxx = ip.getWidth();
final int maxy = ip.getHeight();
final Rectangle bounds = results.getBounds();
final double ox = bounds.getX();
final double oy = bounds.getY();
final double scaleX = bounds.getWidth() / maxx;
final double scaleY = bounds.getHeight() / maxy;
// Create a results set for each object
final int maxObject = objectAnalyzer.getMaxObject();
final MemoryPeakResults[] resultsSet = new MemoryPeakResults[maxObject + 1];
for (int object = 0; object <= maxObject; object++) {
final MemoryPeakResults newResults = new MemoryPeakResults();
newResults.copySettings(results);
newResults.setName(results.getName() + " " + object);
resultsSet[object] = newResults;
}
final int[] mask = objectAnalyzer.getObjectMask();
if (settings.showObjectMask) {
final ImageProcessor objectIp = (maxObject <= 255) ? new ByteProcessor(maxx, maxy) : new ShortProcessor(maxx, maxy);
for (int i = 0; i < mask.length; i++) {
objectIp.set(i, mask[i]);
}
final ImagePlus imp = ImageJUtils.display(settings.objectMask + " Objects", objectIp);
imp.setDisplayRange(0, maxObject);
imp.updateAndDraw();
}
// Process the results mapping them to their objects
final Counter i = new Counter();
final int size = results.size();
final int step = ImageJUtils.getProgressInterval(size);
results.forEach(DistanceUnit.PIXEL, (XyrResultProcedure) (xx, yy, result) -> {
if (i.incrementAndGet() % step == 0) {
IJ.showProgress(i.getCount(), size);
}
// Map to the mask objects
final int object;
final int x = (int) ((xx - ox) / scaleX);
final int y = (int) ((yy - oy) / scaleY);
if (x < 0 || x >= maxx || y < 0 || y >= maxy) {
object = 0;
} else {
final int index = y * maxx + x;
// is within the bounds of the image processor?
if (index < 0 || index >= mask.length) {
object = 0;
} else {
object = mask[index];
}
}
resultsSet[object].add(result);
});
IJ.showProgress(1);
// Add the new results sets to memory
i.reset();
for (int object = (settings.nonMaskDataset) ? 0 : 1; object <= maxObject; object++) {
if (resultsSet[object].isNotEmpty()) {
MemoryPeakResults.addResults(resultsSet[object]);
i.increment();
}
}
IJ.showStatus("Split " + TextUtils.pleural(results.size(), "result") + " into " + TextUtils.pleural(i.getCount(), "set"));
}
Aggregations