Search in sources :

Example 51 with FloatProcessor

use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.

the class CreateData method updateArea.

private void updateArea(int[] mask, int width, int height) {
    // Note: The apparent area will be bigger due to the PSF width blurring the edges.
    // Assume the entire max intensity mask is in focus and the PSF is Gaussian in the focal plane.
    // Blur the mask
    final float[] pixels = new float[mask.length];
    for (int i = 0; i < pixels.length; i++) {
        pixels[i] = mask[i];
    }
    final GaussianFilter blur = new GaussianFilter();
    final double scaleX = (double) settings.getSize() / width;
    final double scaleY = (double) settings.getSize() / height;
    // Allow extra?
    final double extra = 1;
    final double sd = getPsfSd() * extra;
    blur.convolve(pixels, width, height, sd / scaleX, sd / scaleY);
    // Count pixels in blurred mask. Ignore those that are very faint (at the edge of the region)
    int count = 0;
    // // By fraction of max value
    // float limit = 0.1f;
    // //float min = (float) (Maths.max(pixels) * limit);
    // float min = limit;
    // for (float f : pixels)
    // if (f > min)
    // c++;
    // // Rank in order and get fraction of sum
    // Arrays.sort(pixels);
    // double sum = 0;
    // double stop = Maths.sum(pixels) * 0.95;
    // float after = Maths.max(pixels) + 1;
    // for (float f : pixels)
    // {
    // sum += f;
    // if (sum > stop)
    // {
    // break;
    // //after = f;
    // //stop = Float.POSITIVE_INFINITY;
    // }
    // if (f > after)
    // break;
    // c++;
    // }
    // Threshold to a mask
    final FloatProcessor fp = new FloatProcessor(width, height, pixels);
    final ShortProcessor sp = (ShortProcessor) fp.convertToShort(true);
    final int t = AutoThreshold.getThreshold(AutoThreshold.Method.OTSU, sp.getHistogram());
    // Utils.display("Blurred", fp);
    for (int i = 0; i < mask.length; i++) {
        if (sp.get(i) >= t) {
            count++;
        }
    }
    // Convert
    final double scale = ((double) count) / mask.length;
    // System.out.printf("Scale = %f\n", scale);
    areaInUm = scale * settings.getSize() * settings.getPixelPitch() * settings.getSize() * settings.getPixelPitch() / 1e6;
}
Also used : GaussianFilter(uk.ac.sussex.gdsc.smlm.filters.GaussianFilter) FloatProcessor(ij.process.FloatProcessor) ReadHint(uk.ac.sussex.gdsc.smlm.results.ImageSource.ReadHint) ShortProcessor(ij.process.ShortProcessor)

Example 52 with FloatProcessor

use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.

the class PcPalmAnalysis method computeAutoCorrelationCurveFft.

/**
 * Compute the auto-correlation curve using FFT. Computes the correlation image and then samples
 * the image at radii up to the specified length to get the average correlation at a given radius.
 *
 * @param im the image
 * @param wp the weighted image
 * @param maxRadius the max radius
 * @param nmPerPixel the nm per pixel
 * @param density the density
 * @return { distances[], gr[], gr_se[] }
 */
@Nullable
private double[][] computeAutoCorrelationCurveFft(ImageProcessor im, ImageProcessor wp, int maxRadius, double nmPerPixel, double density) {
    log("Performing FFT correlation");
    final FloatProcessor corrIm = computeAutoCorrelationFft(im);
    final FloatProcessor corrW = computeAutoCorrelationFft(wp);
    if (corrIm == null || corrW == null) {
        error("Unable to perform Fourier transform");
        return null;
    }
    final int centre = corrIm.getHeight() / 2;
    final Rectangle crop = new Rectangle(centre - maxRadius, centre - maxRadius, maxRadius * 2, maxRadius * 2);
    if (settings.showCorrelationImages) {
        displayCorrelation(corrIm, "Image correlation FFT", crop);
        displayCorrelation(corrW, "Window correlation FFT", crop);
    }
    log("  Normalising correlation");
    final FloatProcessor correlation = normaliseCorrelation(corrIm, corrW, density);
    if (settings.showCorrelationImages) {
        displayCorrelation(correlation, "Normalised correlation FFT", crop);
    }
    return computeRadialAverage(maxRadius, nmPerPixel, correlation);
}
Also used : FloatProcessor(ij.process.FloatProcessor) Rectangle(java.awt.Rectangle) Nullable(uk.ac.sussex.gdsc.core.annotation.Nullable)

Example 53 with FloatProcessor

use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.

the class PcPalmAnalysis method createWeightImage.

/**
 * Create a weight image of the same size. All pixels corresponding to the original image area are
 * set to 1. A window function is optionally applied.
 *
 * @param im the image
 * @param applyWindow the apply window flag
 * @return The weight image
 */
private static ImageProcessor createWeightImage(ImageProcessor im, boolean applyWindow) {
    final float[] data = new float[im.getWidth() * im.getHeight()];
    Arrays.fill(data, 1);
    ImageProcessor wp = new FloatProcessor(im.getWidth(), im.getHeight(), data, null);
    if (applyWindow) {
        wp = applyWindow(wp, imageWindow);
    }
    return wp;
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloatProcessor(ij.process.FloatProcessor)

Example 54 with FloatProcessor

use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.

the class JTransformsTest method createProcessor.

private static FloatProcessor createProcessor(int size, int x, int y, int width, int height, UniformRandomProvider rng) {
    final ByteProcessor bp = new ByteProcessor(size, size);
    bp.setColor(255);
    bp.fillOval(x, y, width, height);
    final EDM e = new EDM();
    final FloatProcessor fp = e.makeFloatEDM(bp, 0, true);
    if (rng != null) {
        final float[] d = (float[]) fp.getPixels();
        for (int i = 0; i < d.length; i++) {
            d[i] += rng.nextFloat() * 0.01;
        }
    }
    return fp;
}
Also used : ByteProcessor(ij.process.ByteProcessor) FloatProcessor(ij.process.FloatProcessor) EDM(ij.plugin.filter.EDM)

Example 55 with FloatProcessor

use of ij.process.FloatProcessor in project GDSC-SMLM by aherbert.

the class JTransformsTest method jtransforms2DDhtIsFasterThanFht2.

@SpeedTag
@SeededTest
void jtransforms2DDhtIsFasterThanFht2(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    // Test the forward DHT of data. and reverse transform or the pre-computed correlation.
    final int size = 256;
    final int w = size / 4;
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    // Blob in the centre
    FloatProcessor fp = createProcessor(size, size / 2 - w / 2, size / 2 - w / 2, w, w, null);
    final Fht fht2 = new Fht((float[]) fp.getPixels(), size, false);
    fht2.transform();
    fht2.initialiseFastMultiply();
    // Random blobs, original and correlated
    final int N = 40;
    final float[][] data = new float[N * 2][];
    final int lower = w;
    final int upper = size - w;
    final int range = upper - lower;
    for (int i = 0, j = 0; i < N; i++) {
        final int x = lower + r.nextInt(range);
        final int y = lower + r.nextInt(range);
        fp = createProcessor(size, x, y, w, w, r);
        final float[] pixels = (float[]) fp.getPixels();
        data[j++] = pixels.clone();
        final Fht fht1 = new Fht(pixels, size, false);
        fht1.copyTables(fht2);
        fht2.transform();
        final float[] pixels2 = new float[pixels.length];
        fht2.conjugateMultiply(fht2, pixels2);
        data[j++] = pixels2;
    }
    // CommonUtils.setThreadsBeginN_1D_FFT_2Threads(Long.MAX_VALUE);
    // CommonUtils.setThreadsBeginN_1D_FFT_4Threads(Long.MAX_VALUE);
    CommonUtils.setThreadsBeginN_2D(Long.MAX_VALUE);
    final TimingService ts = new TimingService();
    ts.execute(new ImageJFhtSpeedTask(size, data));
    ts.execute(new ImageJFht2SpeedTask(size, data));
    ts.execute(new JTransformsDhtSpeedTask(size, data));
    ts.repeat();
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    // Assertions.assertTrue(ts.get(-1).getMean() < ts.get(-2).getMean());
    final TimingResult slow = ts.get(-2);
    final TimingResult fast = ts.get(-1);
    logger.log(TestLogUtils.getTimingRecord(slow, fast));
}
Also used : Fht(uk.ac.sussex.gdsc.core.ij.process.Fht) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) FloatProcessor(ij.process.FloatProcessor) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

FloatProcessor (ij.process.FloatProcessor)174 ImageProcessor (ij.process.ImageProcessor)30 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)26 Rectangle (java.awt.Rectangle)23 Test (org.junit.Test)22 ByteProcessor (ij.process.ByteProcessor)21 ShortProcessor (ij.process.ShortProcessor)20 Point (java.awt.Point)20 Test (org.junit.jupiter.api.Test)20 ImagePlus (ij.ImagePlus)19 ImageStack (ij.ImageStack)19 Future (java.util.concurrent.Future)12 ColorProcessor (ij.process.ColorProcessor)11 ArrayList (java.util.ArrayList)9 LinkedList (java.util.LinkedList)8 Fht (uk.ac.sussex.gdsc.core.ij.process.Fht)7 Point (mpicbg.models.Point)6 FHT2 (ij.process.FHT2)5 File (java.io.File)5 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)5