Search in sources :

Example 86 with FloatProcessor

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

the class ImageConverter method getData.

/**
	 * Get the data from the image as a float array (include cropping to the ROI). Data is duplicated if the
	 * input already a float array.
	 * <p>
	 * Allows reuse of an existing buffer if provided. This will not be truncated if it is larger than the
	 * ImageProcessor ROI bounds. If smaller then a new buffer will be created.
	 * 
	 * @param oPixels
	 * @param width
	 * @param height
	 * @param bounds
	 * @param buffer
	 * @return The float array data
	 */
public static float[] getData(final Object oPixels, final int width, final int height, final Rectangle bounds, float[] buffer) {
    if (oPixels == null)
        return null;
    if (oPixels instanceof float[]) {
        float[] pixels = (float[]) oPixels;
        if (bounds != null && (bounds.x != 0 || bounds.y != 0 || bounds.width != width || bounds.height != height)) {
            float[] pixels2 = allocate(buffer, bounds.width * bounds.height);
            for (int ys = bounds.y; ys < bounds.y + bounds.height; ys++) {
                int offset1 = (ys - bounds.y) * bounds.width;
                int offset2 = ys * width + bounds.x;
                for (int xs = 0; xs < bounds.width; xs++) pixels2[offset1++] = pixels[offset2++];
            }
            return pixels2;
        } else {
            float[] pixels2 = allocate(buffer, pixels.length);
            System.arraycopy(pixels, 0, pixels2, 0, pixels.length);
            return pixels2;
        }
    } else if (oPixels instanceof short[]) {
        short[] pixels = (short[]) oPixels;
        if (bounds != null && (bounds.x != 0 || bounds.y != 0 || bounds.width != width || bounds.height != height)) {
            float[] pixels2 = allocate(buffer, bounds.width * bounds.height);
            for (int ys = bounds.y; ys < bounds.y + bounds.height; ys++) {
                int offset1 = (ys - bounds.y) * bounds.width;
                int offset2 = ys * width + bounds.x;
                for (int xs = 0; xs < bounds.width; xs++) pixels2[offset1++] = pixels[offset2++] & 0xffff;
            }
            return pixels2;
        } else {
            float[] pixels2 = allocate(buffer, pixels.length);
            for (int i = 0; i < pixels.length; i++) pixels2[i] = pixels[i] & 0xffff;
            return pixels2;
        }
    } else if (oPixels instanceof byte[]) {
        byte[] pixels = (byte[]) oPixels;
        if (bounds != null && (bounds.x != 0 || bounds.y != 0 || bounds.width != width || bounds.height != height)) {
            float[] pixels2 = allocate(buffer, bounds.width * bounds.height);
            for (int ys = bounds.y; ys < bounds.y + bounds.height; ys++) {
                int offset1 = (ys - bounds.y) * bounds.width;
                int offset2 = ys * width + bounds.x;
                for (int xs = 0; xs < bounds.width; xs++) pixels2[offset1++] = pixels[offset2++] & 0xff;
            }
            return pixels2;
        } else {
            float[] pixels2 = allocate(buffer, pixels.length);
            for (int i = 0; i < pixels.length; i++) pixels2[i] = pixels[i] & 0xff;
            return pixels2;
        }
    } else if (oPixels instanceof int[]) {
        // The default processing
        ImageProcessor ip = new ColorProcessor(width, height, (int[]) oPixels);
        ip.setRoi(bounds);
        FloatProcessor fp = ip.crop().toFloat(0, null);
        return (float[]) fp.getPixels();
    }
    return null;
}
Also used : ImageProcessor(ij.process.ImageProcessor) ColorProcessor(ij.process.ColorProcessor) FloatProcessor(ij.process.FloatProcessor)

Example 87 with FloatProcessor

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

the class FRCTest method canComputeMirrored.

@Test
public void canComputeMirrored() {
    // Sample lines through an image to create a structure.
    int size = 1024;
    double[][] data = new double[size * 2][];
    RandomGenerator r = new Well19937c(30051977);
    for (int x = 0, y = 0, y2 = size, i = 0; x < size; x++, y++, y2--) {
        data[i++] = new double[] { x + r.nextGaussian() * 5, y + r.nextGaussian() * 5 };
        data[i++] = new double[] { x + r.nextGaussian() * 5, y2 + r.nextGaussian() * 5 };
    }
    // Create 2 images
    Rectangle bounds = new Rectangle(0, 0, size, size);
    IJImagePeakResults i1 = createImage(bounds);
    IJImagePeakResults i2 = createImage(bounds);
    int[] indices = Utils.newArray(data.length, 0, 1);
    MathArrays.shuffle(indices, r);
    for (int i : indices) {
        IJImagePeakResults image = i1;
        i1 = i2;
        i2 = image;
        image.add((float) data[i][0], (float) data[i][1], 1);
    }
    i1.end();
    i2.end();
    ImageProcessor ip1 = i1.getImagePlus().getProcessor();
    ImageProcessor ip2 = i2.getImagePlus().getProcessor();
    // Test
    FRC frc = new FRC();
    FloatProcessor[] fft1, fft2;
    fft1 = frc.getComplexFFT(ip1);
    fft2 = frc.getComplexFFT(ip2);
    float[] dataA1 = (float[]) fft1[0].getPixels();
    float[] dataB1 = (float[]) fft1[1].getPixels();
    float[] dataA2 = (float[]) fft2[0].getPixels();
    float[] dataB2 = (float[]) fft2[1].getPixels();
    float[] numeratorE = new float[dataA1.length];
    float[] absFFT1E = new float[dataA1.length];
    float[] absFFT2E = new float[dataA1.length];
    FRC.compute(numeratorE, absFFT1E, absFFT2E, dataA1, dataB1, dataA2, dataB2);
    Assert.assertTrue("numeratorE", FRC.checkSymmetry(numeratorE, size));
    Assert.assertTrue("absFFT1E", FRC.checkSymmetry(absFFT1E, size));
    Assert.assertTrue("absFFT2E", FRC.checkSymmetry(absFFT2E, size));
    float[] numeratorA = new float[dataA1.length];
    float[] absFFT1A = new float[dataA1.length];
    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++)
    //	{
    //		System.out.printf("[%d,%d = %d] %f ?= %f\n", x, y, i, numeratorE[i], numeratorA[i]);
    //	}
    Assert.assertArrayEquals("numerator", numeratorE, numeratorA, 0);
    Assert.assertArrayEquals("absFFT1", absFFT1E, absFFT1A, 0);
    Assert.assertArrayEquals("absFFT2", absFFT2E, absFFT2A, 0);
    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++) {
        Assert.assertEquals("numerator", numeratorE[i], numeratorA[i], 0);
        Assert.assertEquals("absFFT1", absFFT1E[i], absFFT1A[i], 0);
        Assert.assertEquals("absFFT2", absFFT2E[i], absFFT2A[i], 0);
    }
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloatProcessor(ij.process.FloatProcessor) Rectangle(java.awt.Rectangle) Well19937c(org.apache.commons.math3.random.Well19937c) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) Test(org.junit.Test)

Example 88 with FloatProcessor

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

the class FRCTest method computeMirroredIsFaster.

@Test
public void computeMirroredIsFaster() {
    // Sample lines through an image to create a structure.
    final int size = 2048;
    double[][] data = new double[size * 2][];
    RandomGenerator r = new Well19937c(30051977);
    for (int x = 0, y = 0, y2 = size, i = 0; x < size; x++, y++, y2--) {
        data[i++] = new double[] { x + r.nextGaussian() * 5, y + r.nextGaussian() * 5 };
        data[i++] = new double[] { x + r.nextGaussian() * 5, y2 + r.nextGaussian() * 5 };
    }
    // Create 2 images
    Rectangle bounds = new Rectangle(0, 0, size, size);
    IJImagePeakResults i1 = createImage(bounds);
    IJImagePeakResults i2 = createImage(bounds);
    int[] indices = Utils.newArray(data.length, 0, 1);
    MathArrays.shuffle(indices, r);
    for (int i : indices) {
        IJImagePeakResults image = i1;
        i1 = i2;
        i2 = image;
        image.add((float) data[i][0], (float) data[i][1], 1);
    }
    i1.end();
    i2.end();
    ImageProcessor ip1 = i1.getImagePlus().getProcessor();
    ImageProcessor ip2 = i2.getImagePlus().getProcessor();
    // Test
    FRC frc = new FRC();
    FloatProcessor[] fft1, 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[] numerator = new float[dataA1.length];
    final float[] absFFT1 = new float[dataA1.length];
    final float[] absFFT2 = new float[dataA1.length];
    TimingService ts = new TimingService(10);
    ts.execute(new MyTimingTask("compute") {

        public Object run(Object data) {
            FRC.compute(numerator, absFFT1, absFFT2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.execute(new MyTimingTask("computeMirrored") {

        public Object run(Object data) {
            FRC.computeMirrored(size, numerator, absFFT1, absFFT2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.execute(new MyTimingTask("computeMirroredFast") {

        public Object run(Object data) {
            FRC.computeMirroredFast(size, numerator, absFFT1, absFFT2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.repeat(ts.getSize());
    ts.report();
}
Also used : FloatProcessor(ij.process.FloatProcessor) Rectangle(java.awt.Rectangle) Well19937c(org.apache.commons.math3.random.Well19937c) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) ImageProcessor(ij.process.ImageProcessor) TimingService(gdsc.core.test.TimingService) Test(org.junit.Test)

Example 89 with FloatProcessor

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

the class ConfigurationTemplateTest method canLoadTemplateImageFromFile.

@Test
public void canLoadTemplateImageFromFile() throws IOException {
    ConfigurationTemplate.clearTemplates();
    Assert.assertEquals(0, ConfigurationTemplate.getTemplateNamesWithImage().length);
    // Create a dummy image
    int size = 20;
    float[] pixels = new float[size * size];
    RandomGenerator r = new Well19937c();
    for (int i = pixels.length; i-- > 0; ) pixels[i] = r.nextFloat();
    ImagePlus imp = new ImagePlus("test", new FloatProcessor(size, size, pixels));
    File tmpFile = File.createTempFile("tmp", ".tif");
    tmpFile.deleteOnExit();
    IJ.save(imp, tmpFile.getPath());
    String name = "canLoadTemplateImageFromFile";
    File file = new File(Utils.replaceExtension(tmpFile.getPath(), ".xml"));
    ConfigurationTemplate.saveTemplate(name, new GlobalSettings(), file);
    Assert.assertEquals(1, ConfigurationTemplate.getTemplateNamesWithImage().length);
    ImagePlus imp2 = ConfigurationTemplate.getTemplateImage(name);
    Assert.assertNotNull(imp2);
    float[] data = (float[]) imp2.getProcessor().toFloat(0, null).getPixels();
    Assert.assertArrayEquals(pixels, data, 0);
}
Also used : FloatProcessor(ij.process.FloatProcessor) GlobalSettings(gdsc.smlm.ij.settings.GlobalSettings) Well19937c(org.apache.commons.math3.random.Well19937c) ImagePlus(ij.ImagePlus) File(java.io.File) RandomGenerator(org.apache.commons.math3.random.RandomGenerator) Test(org.junit.Test)

Example 90 with FloatProcessor

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

the class FRC method getComplexFFT.

/**
	 * Convert an image into a Fourier image with real and imaginary parts
	 * 
	 * @param ip
	 *            The image
	 * @return the real and imaginary parts
	 */
public FloatProcessor[] getComplexFFT(ImageProcessor ip) {
    FloatProcessor taperedDataImage = getSquareTaperedImage(ip);
    FHT2 fht = new FHT2(taperedDataImage);
    fht.setShowProgress(false);
    fht.transform();
    ImageStack stack1 = fht.getComplexTransform();
    return getProcessors(stack1);
}
Also used : FHT2(ij.process.FHT2) FloatProcessor(ij.process.FloatProcessor) ImageStack(ij.ImageStack)

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