Search in sources :

Example 1 with NoiseEstimator

use of uk.ac.sussex.gdsc.core.utils.NoiseEstimator in project GDSC-SMLM by aherbert.

the class Noise method run.

@Override
public void run(ImageProcessor ip) {
    // Perform all methods and add to the results
    final double[] result = new double[NoiseEstimator.Method.values().length + 1];
    int index = 0;
    result[index++] = (pfr == null) ? 1 : pfr.getSliceNumber();
    final Rectangle bounds = ip.getRoi();
    final float[] buffer = ImageJImageConverter.getData(ip.getPixels(), ip.getWidth(), ip.getHeight(), bounds, null);
    cameraModel.removeBiasAndGain(bounds, buffer);
    final NoiseEstimator ne = NoiseEstimator.wrap(buffer, bounds.width, bounds.height);
    ne.setPreserveResiduals(true);
    for (final NoiseEstimator.Method m : NoiseEstimator.Method.values()) {
        result[index++] = ne.getNoise(m);
    }
    results.add(result);
}
Also used : Rectangle(java.awt.Rectangle) NoiseEstimator(uk.ac.sussex.gdsc.core.utils.NoiseEstimator)

Example 2 with NoiseEstimator

use of uk.ac.sussex.gdsc.core.utils.NoiseEstimator in project GDSC-SMLM by aherbert.

the class Noise method drawPlot.

/**
 * Build a plot of the noise estimate from the current frame. Limit the preview to 100 frames.
 */
private void drawPlot() {
    final NoiseEstimatorMethod[] values = SettingsManager.getNoiseEstimatorMethodValues();
    final NoiseEstimator.Method method1 = FitProtosHelper.convertNoiseEstimatorMethod(values[settings.algorithm]);
    final NoiseEstimator.Method method2 = FitProtosHelper.convertNoiseEstimatorMethod(values[settings.algorithm2]);
    IJ.showStatus("Estimating noise ...");
    final boolean twoMethods = method1 != method2;
    final boolean preserveResiduals = method1.name().contains("Residuals") && method2.name().contains("Residuals") && twoMethods;
    final int current = imp.getCurrentSlice();
    final int stackSize = imp.getStackSize();
    final int preview = 100;
    int start = current;
    int end = current + preview;
    if (end > stackSize) {
        final int shift = end - stackSize;
        start -= shift;
        end = stackSize;
        start = Math.max(1, start);
    }
    final int size = end - start + 1;
    final double[] xValues = new double[size];
    final double[] yValues1 = new double[size];
    final double[] yValues2 = (twoMethods) ? new double[size] : null;
    final ImageStack stack = imp.getImageStack();
    final Rectangle bounds = imp.getProcessor().getRoi();
    float[] buffer = null;
    for (int slice = start, i = 0; slice <= end; slice++, i++) {
        IJ.showProgress(i, size);
        final ImageProcessor ip = stack.getProcessor(slice);
        buffer = ImageJImageConverter.getData(ip.getPixels(), ip.getWidth(), ip.getHeight(), bounds, buffer);
        cameraModel.removeBiasAndGain(bounds, buffer);
        final NoiseEstimator ne = NoiseEstimator.wrap(buffer, bounds.width, bounds.height);
        ne.setPreserveResiduals(preserveResiduals);
        ne.setRange(settings.lowestPixelsRange);
        xValues[i] = slice;
        yValues1[i] = ne.getNoise(method1);
        if (yValues2 != null) {
            yValues2[i] = ne.getNoise(method2);
        }
    }
    IJ.showProgress(1);
    IJ.showStatus("Plotting noise ...");
    // Get limits
    final double[] a = Tools.getMinMax(xValues);
    final double[] b1 = Tools.getMinMax(yValues1);
    if (twoMethods) {
        final double[] b2 = Tools.getMinMax(yValues2);
        b1[0] = Math.min(b1[0], b2[0]);
        b1[1] = Math.max(b1[1], b2[1]);
    }
    final String title = imp.getTitle() + " Noise";
    final Plot plot = new Plot(title, "Slice", yAxisTitle);
    double range = b1[1] - b1[0];
    if (range == 0) {
        range = 1;
    }
    plot.setLimits(a[0], a[1], b1[0] - 0.05 * range, b1[1] + 0.05 * range);
    plot.setColor(Color.blue);
    plot.addPoints(xValues, yValues1, Plot.LINE);
    String label = String.format("%s (Blue) = %s", trim(method1.getName()), MathUtils.rounded(Statistics.create(yValues1).getMean()));
    if (twoMethods) {
        plot.setColor(Color.red);
        plot.addPoints(xValues, yValues2, Plot.LINE);
        label += String.format(", %s (Red) = %s", trim(method2.getName()), MathUtils.rounded(Statistics.create(yValues2).getMean()));
    }
    plot.setColor(Color.black);
    plot.addLabel(0, 0, label);
    ImageJUtils.display(title, plot);
    IJ.showStatus("");
}
Also used : ImageStack(ij.ImageStack) Plot(ij.gui.Plot) Rectangle(java.awt.Rectangle) NoiseEstimatorMethod(uk.ac.sussex.gdsc.smlm.data.config.FitProtos.NoiseEstimatorMethod) ImageProcessor(ij.process.ImageProcessor) NoiseEstimator(uk.ac.sussex.gdsc.core.utils.NoiseEstimator)

Aggregations

Rectangle (java.awt.Rectangle)2 NoiseEstimator (uk.ac.sussex.gdsc.core.utils.NoiseEstimator)2 ImageStack (ij.ImageStack)1 Plot (ij.gui.Plot)1 ImageProcessor (ij.process.ImageProcessor)1 NoiseEstimatorMethod (uk.ac.sussex.gdsc.smlm.data.config.FitProtos.NoiseEstimatorMethod)1