Search in sources :

Example 56 with FloatProcessor

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

the class ConfigurationTemplateTest method canLoadTemplateImageFromFile.

@SeededTest
void canLoadTemplateImageFromFile(RandomSeed seed) throws IOException {
    ConfigurationTemplate.clearTemplates();
    Assertions.assertEquals(0, ConfigurationTemplate.getTemplateNamesWithImage().length);
    // Create a dummy image
    final int size = 20;
    final float[] pixels = new float[size * size];
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    for (int i = pixels.length; i-- > 0; ) {
        pixels[i] = r.nextFloat();
    }
    final ImagePlus imp = new ImagePlus("test", new FloatProcessor(size, size, pixels));
    final File tmpFile = File.createTempFile("tmp", ".tif");
    tmpFile.deleteOnExit();
    IJ.save(imp, tmpFile.getPath());
    final String name = "canLoadTemplateImageFromFile";
    final File file = new File(FileUtils.replaceExtension(tmpFile.getPath(), ".xml"));
    ConfigurationTemplate.saveTemplate(name, TemplateSettings.getDefaultInstance(), file);
    Assertions.assertEquals(1, ConfigurationTemplate.getTemplateNamesWithImage().length);
    final ImagePlus imp2 = ConfigurationTemplate.getTemplateImage(name);
    Assertions.assertNotNull(imp2);
    final float[] data = (float[]) imp2.getProcessor().toFloat(0, null).getPixels();
    Assertions.assertArrayEquals(pixels, data);
}
Also used : FloatProcessor(ij.process.FloatProcessor) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) ImagePlus(ij.ImagePlus) File(java.io.File) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 57 with FloatProcessor

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

the class Image3D method insert.

/**
 * Insert a sub-region.
 *
 * @param x the x position
 * @param y the y position
 * @param z the z position
 * @param stack the image stack
 * @throws IllegalArgumentException if the region is not within the data
 */
public void insert(int x, int y, int z, ImageStack stack) {
    // Check the region range
    final int width = stack.getWidth();
    final int height = stack.getHeight();
    final int depth = stack.getSize();
    if (width < 1 || height < 1 || depth < 1) {
        return;
    }
    if (x < 0 || (long) x + width > nc || y < 0 || (long) y + height > nr || z < 0 || (long) z + depth > ns) {
        throw new IllegalArgumentException("Region not within the data");
    }
    final boolean isFloat = stack.getBitDepth() == 32;
    final FloatProcessor fp = (isFloat) ? new FloatProcessor(width, height) : null;
    for (int s = 0; s < depth; s++, z++) {
        int base = z * nrByNc + y * nc + x;
        final float[] region = (float[]) ((isFloat) ? stack.getPixels(1 + s) : stack.getProcessor(1 + s).toFloat(0, fp).getPixels());
        for (int r = 0, i = 0; r < height; r++) {
            copyFrom(region, i, width, base);
            base += nc;
            i += width;
        }
    }
}
Also used : FloatProcessor(ij.process.FloatProcessor)

Example 58 with FloatProcessor

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

the class JTransformsTest method canComputeUsingFft.

private static void canComputeUsingFft(boolean convolution) {
    final int size = 16;
    final int ex = 5;
    final int ey = 7;
    final int ox = 1;
    final int oy = 2;
    final FloatProcessor fp1 = createProcessor(size, ex, ey, 4, 4, null);
    final FloatProcessor fp2 = createProcessor(size, size / 2 + ox, size / 2 + oy, 4, 4, null);
    final float[] input1 = (float[]) fp1.getPixels();
    final float[] input2 = (float[]) fp2.getPixels();
    final FhtFilter ff = new FhtFilter(input2.clone(), size, size);
    ff.setOperation((convolution) ? Operation.CONVOLUTION : Operation.CORRELATION);
    final float[] e = input1.clone();
    ff.filter(e, size, size);
    // Do the same with JTransforms
    final float[] data1 = new float[input1.length * 2];
    final FloatFFT_2D fft = new FloatFFT_2D(size, size);
    System.arraycopy(input1, 0, data1, 0, input1.length);
    final float[] data2 = new float[data1.length];
    System.arraycopy(input2, 0, data2, 0, input2.length);
    fft.realForwardFull(data1);
    fft.realForwardFull(data2);
    // https://en.wikipedia.org/wiki/Complex_number#Multiplication_and_division
    for (int i = 0; i < data2.length; i += 2) {
        final float a = data1[i];
        final float b = data1[i + 1];
        final float c = data2[i];
        // Get the conjugate for correlation
        final float d = (convolution) ? data2[i + 1] : -data2[i + 1];
        data1[i] = a * c - b * d;
        data1[i + 1] = b * c + a * d;
    }
    fft.complexInverse(data1, true);
    final float[] o = new float[e.length];
    for (int i = 0, j = 0; i < o.length; i++, j += 2) {
        o[i] = data1[j];
    }
    Fht.swapQuadrants(new FloatProcessor(size, size, o));
    Assertions.assertArrayEquals(e, o, 1e-3f);
}
Also used : FloatFFT_2D(org.jtransforms.fft.FloatFFT_2D) FloatProcessor(ij.process.FloatProcessor) FhtFilter(uk.ac.sussex.gdsc.smlm.ij.filters.FhtFilter)

Example 59 with FloatProcessor

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

the class FhtFilterTest 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 60 with FloatProcessor

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

the class FrcTest method canComputeMirrored.

@SeededTest
void canComputeMirrored(RandomSeed seed) {
    // Sample lines through an image to create a structure.
    final int size = 1024;
    final double[][] data = new double[size * 2][];
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    final SharedStateContinuousSampler gs = SamplerUtils.createGaussianSampler(r, 0, 5);
    for (int x = 0, y = 0, y2 = size, i = 0; x < size; x++, y++, y2--) {
        data[i++] = new double[] { x + gs.sample(), y + gs.sample() };
        data[i++] = new double[] { x + gs.sample(), y2 + gs.sample() };
    }
    // Create 2 images
    final Rectangle bounds = new Rectangle(0, 0, size, size);
    ImageJImagePeakResults i1 = createImage(bounds);
    ImageJImagePeakResults i2 = createImage(bounds);
    final int[] indices = SimpleArrayUtils.natural(data.length);
    PermutationSampler.shuffle(r, indices);
    for (final int i : indices) {
        final ImageJImagePeakResults image = i1;
        i1 = i2;
        i2 = image;
        image.add((float) data[i][0], (float) data[i][1], 1);
    }
    i1.end();
    i2.end();
    final ImageProcessor ip1 = i1.getImagePlus().getProcessor();
    final ImageProcessor ip2 = i2.getImagePlus().getProcessor();
    // Test
    final Frc frc = new Frc();
    FloatProcessor[] fft1;
    FloatProcessor[] fft2;
    fft1 = frc.getComplexFft(ip1);
    fft2 = frc.getComplexFft(ip2);
    final float[] dataA1 = (float[]) fft1[0].getPixels();
    final float[] dataB1 = (float[]) fft1[1].getPixels();
    final float[] dataA2 = (float[]) fft2[0].getPixels();
    final float[] dataB2 = (float[]) fft2[1].getPixels();
    final float[] numeratorE = new float[dataA1.length];
    final float[] absFft1E = new float[dataA1.length];
    final float[] absFft2E = new float[dataA1.length];
    Frc.compute(numeratorE, absFft1E, absFft2E, dataA1, dataB1, dataA2, dataB2);
    Assertions.assertTrue(checkSymmetry(numeratorE, size), "numeratorE");
    Assertions.assertTrue(checkSymmetry(absFft1E, size), "absFft1E");
    Assertions.assertTrue(checkSymmetry(absFft2E, size), "absFft2E");
    final float[] numeratorA = new float[dataA1.length];
    final float[] absFft1A = new float[dataA1.length];
    final float[] absFft2A = new float[dataA1.length];
    Frc.computeMirrored(size, numeratorA, absFft1A, absFft2A, dataA1, dataB1, dataA2, dataB2);
    // for (int y=0, i=0; y<size; y++)
    // for (int x=0; x<size; x++, i++)
    // {
    // logger.fine(FunctionUtils.getSupplier("[%d,%d = %d] %f ?= %f", x, y, i, numeratorE[i],
    // numeratorA[i]);
    // }
    Assertions.assertArrayEquals(numeratorE, numeratorA, "numerator");
    Assertions.assertArrayEquals(absFft1E, absFft1A, "absFft1");
    Assertions.assertArrayEquals(absFft2E, absFft2A, "absFft2");
    Frc.computeMirroredFast(size, numeratorA, absFft1A, absFft2A, dataA1, dataB1, dataA2, dataB2);
    // Check this.
    for (int y = 1; y < size; y++) {
        for (int x = 1, i = y * size + 1; x < size; x++, i++) {
            Assertions.assertEquals(numeratorE[i], numeratorA[i], "numerator");
            Assertions.assertEquals(absFft1E[i], absFft1A[i], "absFft1");
            Assertions.assertEquals(absFft2E[i], absFft2A[i], "absFft2");
        }
    }
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloatProcessor(ij.process.FloatProcessor) SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) Rectangle(java.awt.Rectangle) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) 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