Search in sources :

Example 1 with FloatProcessor

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

the class ImageConverter method getDoubleData.

/**
	 * Get the data from the image as a double array (include cropping to the ROI). Data is duplicated if the
	 * input already a double 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 double array data
	 */
public static double[] getDoubleData(final Object oPixels, final int width, final int height, final Rectangle bounds, double[] 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)) {
            double[] 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 {
            double[] pixels2 = allocate(buffer, pixels.length);
            for (int i = 0; i < pixels.length; i++) pixels2[i] = pixels[i];
            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)) {
            double[] 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 {
            double[] 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)) {
            double[] 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 {
            double[] 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 (double[]) fp.getPixels();
    }
    return null;
}
Also used : ImageProcessor(ij.process.ImageProcessor) ColorProcessor(ij.process.ColorProcessor) FloatProcessor(ij.process.FloatProcessor)

Example 2 with FloatProcessor

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

the class DriftCalculator method calculateDriftUsingFrames.

/**
	 * Calculate the drift by aligning N consecutive frames with the overall image. Update the current drift parameters.
	 * 
	 * @param blocks
	 * @param blockT
	 * @param bounds
	 * @param scale
	 * @param dx
	 * @param dy
	 * @param smoothing
	 * @param iterations
	 * @return
	 */
private double calculateDriftUsingFrames(ArrayList<ArrayList<Localisation>> blocks, int[] blockT, Rectangle bounds, float scale, double[] dx, double[] dy, double[] originalDriftTimePoints, double smoothing, int iterations) {
    // Construct images using the current drift
    tracker.status("Constructing images");
    // Built an image for each block of results.
    final ImageProcessor[] images = new ImageProcessor[blocks.size()];
    List<Future<?>> futures = new LinkedList<Future<?>>();
    progressCounter = 0;
    totalCounter = images.length * 2;
    for (int i = 0; i < images.length; i++) {
        futures.add(threadPool.submit(new ImageBuilder(blocks.get(i), images, i, bounds, scale, dx, dy)));
    }
    Utils.waitForCompletion(futures);
    for (int i = 0; i < blocks.size(); i++) {
        tracker.progress(i, blocks.size());
        IJImagePeakResults blockImage = newImage(bounds, scale);
        for (Localisation r : blocks.get(i)) {
            blockImage.add(r.t, (float) (r.x + dx[r.t]), (float) (r.y + dy[r.t]), r.s);
        }
        images[i] = getImage(blockImage);
    }
    // Build an image with all results.
    FloatProcessor allIp = new FloatProcessor(images[0].getWidth(), images[0].getHeight());
    for (ImageProcessor ip : images) allIp.copyBits(ip, 0, 0, Blitter.ADD);
    return calculateDrift(blockT, scale, dx, dy, originalDriftTimePoints, smoothing, iterations, images, allIp, true);
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloatProcessor(ij.process.FloatProcessor) Future(java.util.concurrent.Future) IJImagePeakResults(gdsc.smlm.ij.results.IJImagePeakResults) LinkedList(java.util.LinkedList) Point(java.awt.Point)

Example 3 with FloatProcessor

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

the class IJImagePeakResultsTest method canInterpolateUpInXY.

@Test
public void canInterpolateUpInXY() {
    IJImagePeakResults r = new IJImagePeakResults(title, bounds, 1);
    r.setDisplayFlags(IJImagePeakResults.DISPLAY_WEIGHTED);
    FloatProcessor fp = new FloatProcessor(bounds.width, bounds.height);
    begin(r);
    addValue(r, 1.75f, 1.75f, 1);
    fp.putPixelValue(1, 1, 0.75f * 0.75f);
    fp.putPixelValue(2, 1, 0.75f * 0.25f);
    fp.putPixelValue(1, 2, 0.75f * 0.25f);
    fp.putPixelValue(2, 2, 0.25f * 0.25f);
    r.end();
    float[] expecteds = getImage(fp);
    float[] actuals = getImage(r);
    Assert.assertArrayEquals(expecteds, actuals, 0);
}
Also used : FloatProcessor(ij.process.FloatProcessor) Test(org.junit.Test)

Example 4 with FloatProcessor

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

the class IJImagePeakResultsTest method canInterpolateInMiddleOfPixel.

@Test
public void canInterpolateInMiddleOfPixel() {
    IJImagePeakResults r = new IJImagePeakResults(title, bounds, 1);
    r.setDisplayFlags(IJImagePeakResults.DISPLAY_WEIGHTED);
    FloatProcessor fp = new FloatProcessor(bounds.width, bounds.height);
    begin(r);
    addValue(r, 1.5f, 1.5f, 1);
    fp.putPixelValue(1, 1, 1);
    r.end();
    float[] expecteds = getImage(fp);
    float[] actuals = getImage(r);
    Assert.assertArrayEquals(expecteds, actuals, 0);
}
Also used : FloatProcessor(ij.process.FloatProcessor) Test(org.junit.Test)

Example 5 with FloatProcessor

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

the class IJImagePeakResultsTest method canInterpolateDownInXY.

@Test
public void canInterpolateDownInXY() {
    IJImagePeakResults r = new IJImagePeakResults(title, bounds, 1);
    r.setDisplayFlags(IJImagePeakResults.DISPLAY_WEIGHTED);
    FloatProcessor fp = new FloatProcessor(bounds.width, bounds.height);
    begin(r);
    addValue(r, 1.25f, 1.25f, 1);
    fp.putPixelValue(0, 0, 0.25f * 0.25f);
    fp.putPixelValue(0, 1, 0.75f * 0.25f);
    fp.putPixelValue(1, 0, 0.75f * 0.25f);
    fp.putPixelValue(1, 1, 0.75f * 0.75f);
    r.end();
    float[] expecteds = getImage(fp);
    float[] actuals = getImage(r);
    Assert.assertArrayEquals(expecteds, actuals, 0);
}
Also used : FloatProcessor(ij.process.FloatProcessor) Test(org.junit.Test)

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