Search in sources :

Example 1 with ObjectAnalyzer

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"));
}
Also used : ByteProcessor(ij.process.ByteProcessor) Rectangle(java.awt.Rectangle) XyrResultProcedure(uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure) ByteProcessor(ij.process.ByteProcessor) ObjectAnalyzer(uk.ac.sussex.gdsc.smlm.ij.utils.ObjectAnalyzer) ImageProcessor(ij.process.ImageProcessor) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) InputSource(uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource) WindowManager(ij.WindowManager) DistanceUnit(uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit) AtomicReference(java.util.concurrent.atomic.AtomicReference) TextUtils(uk.ac.sussex.gdsc.core.utils.TextUtils) ImagePlus(ij.ImagePlus) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) ShortProcessor(ij.process.ShortProcessor) ImageJUtils(uk.ac.sussex.gdsc.core.ij.ImageJUtils) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults) IJ(ij.IJ) PlugIn(ij.plugin.PlugIn) Rectangle(java.awt.Rectangle) ImagePlus(ij.ImagePlus) ShortProcessor(ij.process.ShortProcessor) ImageProcessor(ij.process.ImageProcessor) ObjectAnalyzer(uk.ac.sussex.gdsc.smlm.ij.utils.ObjectAnalyzer) Counter(uk.ac.sussex.gdsc.smlm.results.count.Counter) MemoryPeakResults(uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)

Aggregations

IJ (ij.IJ)1 ImagePlus (ij.ImagePlus)1 WindowManager (ij.WindowManager)1 PlugIn (ij.plugin.PlugIn)1 ByteProcessor (ij.process.ByteProcessor)1 ImageProcessor (ij.process.ImageProcessor)1 ShortProcessor (ij.process.ShortProcessor)1 Rectangle (java.awt.Rectangle)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ImageJUtils (uk.ac.sussex.gdsc.core.ij.ImageJUtils)1 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)1 TextUtils (uk.ac.sussex.gdsc.core.utils.TextUtils)1 DistanceUnit (uk.ac.sussex.gdsc.smlm.data.config.UnitProtos.DistanceUnit)1 InputSource (uk.ac.sussex.gdsc.smlm.ij.plugins.ResultsManager.InputSource)1 ObjectAnalyzer (uk.ac.sussex.gdsc.smlm.ij.utils.ObjectAnalyzer)1 MemoryPeakResults (uk.ac.sussex.gdsc.smlm.results.MemoryPeakResults)1 Counter (uk.ac.sussex.gdsc.smlm.results.count.Counter)1 XyrResultProcedure (uk.ac.sussex.gdsc.smlm.results.procedures.XyrResultProcedure)1