Search in sources :

Example 31 with ByteProcessor

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

the class PcPalmMolecules method drawImage.

/**
 * Draw an image of the molecules.
 *
 * @param molecules the molecules
 * @param minx the minx
 * @param miny the miny
 * @param maxx the maxx
 * @param maxy the maxy
 * @param nmPerPixel the nm per pixel
 * @param checkBounds Set to true to check the molecules is within the bounds
 * @param binary the binary
 * @return the image
 */
static ImageProcessor drawImage(List<Molecule> molecules, double minx, double miny, double maxx, double maxy, double nmPerPixel, boolean checkBounds, boolean binary) {
    final double scalex = maxx - minx;
    final double scaley = maxy - miny;
    final int width = (int) Math.round(scalex / nmPerPixel) + 1;
    final int height = (int) Math.round(scaley / nmPerPixel) + 1;
    // ***
    if (binary) {
        final byte[] data = new byte[width * height];
        for (final Molecule m : molecules) {
            if (checkBounds) {
                if (m.x < minx || m.x >= maxx || m.y < miny || m.y >= maxy) {
                    continue;
                }
            }
            // Shift to the origin. This makes the image more memory efficient.
            final int x = (int) Math.round((m.x - minx) / nmPerPixel);
            final int y = (int) Math.round((m.y - miny) / nmPerPixel);
            final int index = y * width + x;
            // Construct a binary image
            data[index] = (byte) 1;
        }
        final ByteProcessor ip = new ByteProcessor(width, height, data, null);
        ip.setMinAndMax(0, 1);
        return ip;
    }
    final short[] data = new short[width * height];
    for (final Molecule m : molecules) {
        if (checkBounds && (m.x < minx || m.x >= maxx || m.y < miny || m.y >= maxy)) {
            continue;
        }
        // Shift to the origin. This makes the image more memory efficient.
        final int x = (int) Math.round((m.x - minx) / nmPerPixel);
        final int y = (int) Math.round((m.y - miny) / nmPerPixel);
        final int index = y * width + x;
        // Construct a count image
        data[index]++;
    }
    final ShortProcessor ip = new ShortProcessor(width, height, data, null);
    ip.setMinAndMax(0, MathUtils.max(data));
    return ip;
}
Also used : ByteProcessor(ij.process.ByteProcessor) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) ClusterPoint(uk.ac.sussex.gdsc.core.clustering.ClusterPoint) ShortProcessor(ij.process.ShortProcessor)

Example 32 with ByteProcessor

use of ij.process.ByteProcessor 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 33 with ByteProcessor

use of ij.process.ByteProcessor 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 34 with ByteProcessor

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

the class YeastMask method createMask.

private void createMask() {
    // Create the dimensions
    final int hw = (int) Math.ceil(settings.radius * 1000 / settings.nmPerPixel);
    final int hd = (int) Math.ceil(settings.radius * 1000 / settings.nmPerSlice);
    final int width = 2 * hw + 1;
    final int depth = 2 * hd + 1;
    ImageStack stack = createHemiSphere(width, depth);
    // Extend the centre circle of the sphere into a tube of the required length
    final int h = (int) Math.ceil(settings.length * 1000 / settings.nmPerPixel);
    if (h > 0) {
        final ImageStack newStack = new ImageStack(width, stack.getHeight() + h, stack.getSize());
        for (int slice = 1; slice <= stack.getSize(); slice++) {
            final byte[] pixels = (byte[]) stack.getPixels(slice);
            final byte[] newPixels = new byte[width * newStack.getHeight()];
            newStack.setPixels(newPixels, slice);
            System.arraycopy(pixels, 0, newPixels, 0, pixels.length);
            // Get the final strip to be extended
            final int offset = pixels.length - width;
            int target = pixels.length;
            for (int i = 0; i < h; i++) {
                System.arraycopy(pixels, offset, newPixels, target, width);
                target += width;
            }
        }
        stack = newStack;
    }
    // Copy the hemi-sphere onto the end
    final ImageStack newStack = new ImageStack(width, stack.getHeight() + hw, stack.getSize());
    for (int slice = 1; slice <= stack.getSize(); slice++) {
        final byte[] pixels = (byte[]) stack.getPixels(slice);
        final byte[] newPixels = new byte[width * newStack.getHeight()];
        newStack.setPixels(newPixels, slice);
        System.arraycopy(pixels, 0, newPixels, 0, pixels.length);
        // Copy the hemi-sphere
        int source = 0;
        int target = newPixels.length - width;
        for (int i = 0; i < hw; i++) {
            System.arraycopy(pixels, source, newPixels, target, width);
            target -= width;
            source += width;
        }
    }
    stack = newStack;
    if (settings.excludeNucleus) {
        final ImageStack stack2 = createNucleusSphere(width, depth);
        final int xloc = (stack.getWidth() - stack2.getWidth()) / 2;
        final int yloc = (stack.getHeight() - stack2.getHeight()) / 2;
        final int offset = (stack.getSize() - stack2.getSize()) / 2;
        for (int slice = 1; slice <= stack2.getSize(); slice++) {
            final ImageProcessor ip = stack.getProcessor(slice + offset);
            final ImageProcessor ip2 = stack2.getProcessor(slice);
            ip.copyBits(ip2, xloc, yloc, Blitter.SUBTRACT);
        }
    }
    if (settings.squareOutput && stack.getWidth() != stack.getHeight()) {
        final ImageStack stack2 = new ImageStack(stack.getHeight(), stack.getHeight());
        final int end = stack.getHeight() - stack.getWidth();
        for (int slice = 1; slice <= stack.getSize(); slice++) {
            final ImageProcessor ip = stack.getProcessor(slice);
            final ImageProcessor ip2 = new ByteProcessor(stack2.getWidth(), stack2.getHeight());
            stack2.addSlice(ip2);
            for (int xloc = 0; xloc <= end; xloc += stack.getWidth()) {
                ip2.insert(ip, xloc, 0);
            }
        }
        stack = stack2;
    }
    if (settings.border > 0) {
        final ImageStack stack2 = new ImageStack(stack.getWidth() + 2 * settings.border, stack.getHeight() + 2 * settings.border);
        for (int slice = 1; slice <= stack.getSize(); slice++) {
            final ImageProcessor ip = stack.getProcessor(slice);
            final ImageProcessor ip2 = new ByteProcessor(stack2.getWidth(), stack2.getHeight());
            stack2.addSlice(ip2);
            ip2.insert(ip, settings.border, settings.border);
        }
        stack = stack2;
    }
    ImagePlus imp;
    if (settings.is2D) {
        // TODO - Remove this laziness since we should really just do a 2D image
        final int centre = stack.getSize() / 2;
        imp = ImageJUtils.display(TITLE, stack.getProcessor(centre));
    } else {
        imp = ImageJUtils.display(TITLE, stack);
    }
    // Calibrate
    final Calibration cal = new Calibration();
    cal.setUnit("um");
    cal.pixelWidth = cal.pixelHeight = settings.nmPerPixel / 1000;
    cal.pixelDepth = settings.nmPerSlice / 1000;
    imp.setCalibration(cal);
}
Also used : ByteProcessor(ij.process.ByteProcessor) ImageProcessor(ij.process.ImageProcessor) ImageStack(ij.ImageStack) Calibration(ij.measure.Calibration) ImagePlus(ij.ImagePlus)

Example 35 with ByteProcessor

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

the class DiffusionRateTest method showExample.

private void showExample(int totalSteps, double diffusionSigma, UniformRandomProvider rng) {
    final MoleculeModel m = new MoleculeModel(0, new double[3]);
    final float[] xValues = new float[totalSteps];
    final float[] x = new float[totalSteps];
    final float[] y = new float[totalSteps];
    final DiffusionType diffusionType = CreateDataSettingsHelper.getDiffusionType(settings.getDiffusionType());
    double[] axis;
    if (diffusionType == DiffusionType.LINEAR_WALK) {
        axis = nextVector(SamplerUtils.createNormalizedGaussianSampler(rng));
    } else {
        axis = null;
    }
    for (int j = 0; j < totalSteps; j++) {
        if (diffusionType == DiffusionType.GRID_WALK) {
            m.walk(diffusionSigma, rng);
        } else if (diffusionType == DiffusionType.LINEAR_WALK) {
            m.slide(diffusionSigma, axis, rng);
        } else {
            m.move(diffusionSigma, rng);
        }
        x[j] = (float) (m.getX());
        y[j] = (float) (m.getY());
        xValues[j] = (float) ((j + 1) / settings.getStepsPerSecond());
    }
    // Plot x and y coords on a timeline
    final String title = TITLE + " example coordinates";
    final Plot plot = new Plot(title, "Time (seconds)", "Distance (um)");
    final float[] xUm = convertToUm(x);
    final float[] yUm = convertToUm(y);
    float[] limits = MathUtils.limits(xUm);
    limits = MathUtils.limits(limits, yUm);
    plot.setLimits(0, totalSteps / settings.getStepsPerSecond(), limits[0], limits[1]);
    plot.setColor(Color.red);
    plot.addPoints(xValues, xUm, Plot.LINE);
    plot.setColor(Color.blue);
    plot.addPoints(xValues, yUm, Plot.LINE);
    ImageJUtils.display(title, plot);
    // Scale up and draw 2D position
    for (int j = 0; j < totalSteps; j++) {
        x[j] *= pluginSettings.magnification;
        y[j] *= pluginSettings.magnification;
    }
    final float[] limitsx = getLimits(x);
    final float[] limitsy = getLimits(y);
    int width = (int) (limitsx[1] - limitsx[0]);
    int height = (int) (limitsy[1] - limitsy[0]);
    // Ensure we draw something, even it is a simple dot at the centre for no diffusion
    if (width == 0) {
        width = (int) (32 * pluginSettings.magnification);
        limitsx[0] = -width / 2.0f;
    }
    if (height == 0) {
        height = (int) (32 * pluginSettings.magnification);
        limitsy[0] = -height / 2.0f;
    }
    final ImageProcessor ip = new ByteProcessor(width, height);
    // Adjust x and y using the minimum to centre
    x[0] -= limitsx[0];
    y[0] -= limitsy[0];
    for (int j = 1; j < totalSteps; j++) {
        // Adjust x and y using the minimum to centre
        x[j] -= limitsx[0];
        y[j] -= limitsy[0];
        // Draw a line
        ip.setColor(32 + (223 * j) / (totalSteps - 1));
        ip.drawLine(round(x[j - 1]), round(y[j - 1]), round(x[j]), round(y[j]));
    }
    // Draw the final position
    ip.putPixel(round(x[totalSteps - 1]), round(y[totalSteps - 1]), 255);
    final ImagePlus imp = ImageJUtils.display(TITLE + " example", ip);
    // Apply the fire lookup table
    WindowManager.setTempCurrentImage(imp);
    final LutLoader lut = new LutLoader();
    lut.run("fire");
    WindowManager.setTempCurrentImage(null);
}
Also used : ByteProcessor(ij.process.ByteProcessor) ImageProcessor(ij.process.ImageProcessor) MoleculeModel(uk.ac.sussex.gdsc.smlm.model.MoleculeModel) DiffusionType(uk.ac.sussex.gdsc.smlm.model.DiffusionType) Plot(ij.gui.Plot) HistogramPlot(uk.ac.sussex.gdsc.core.ij.HistogramPlot) LutLoader(ij.plugin.LutLoader) ImagePlus(ij.ImagePlus)

Aggregations

ByteProcessor (ij.process.ByteProcessor)86 ImagePlus (ij.ImagePlus)30 ImageProcessor (ij.process.ImageProcessor)23 FloatProcessor (ij.process.FloatProcessor)21 ShortProcessor (ij.process.ShortProcessor)19 ColorProcessor (ij.process.ColorProcessor)14 ArrayList (java.util.ArrayList)13 Point (java.awt.Point)12 Rectangle (java.awt.Rectangle)11 Roi (ij.gui.Roi)10 AffineTransform (java.awt.geom.AffineTransform)10 ImageStack (ij.ImageStack)9 Patch (ini.trakem2.display.Patch)9 Calibration (ij.measure.Calibration)8 Pair (mpicbg.trakem2.util.Pair)7 Color (java.awt.Color)6 LUT (ij.process.LUT)5 BufferedImage (java.awt.image.BufferedImage)5 IOException (java.io.IOException)5 CoordinateTransformMesh (mpicbg.models.CoordinateTransformMesh)5