Search in sources :

Example 51 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, RandomGenerator[] random) {
    MoleculeModel m = new MoleculeModel(0, new double[3]);
    float[] xValues = new float[totalSteps];
    float[] x = new float[totalSteps];
    float[] y = new float[totalSteps];
    final double[] axis = (settings.getDiffusionType() == DiffusionType.LINEAR_WALK) ? nextVector() : null;
    for (int j = 0; j < totalSteps; j++) {
        if (settings.getDiffusionType() == DiffusionType.GRID_WALK)
            m.walk(diffusionSigma, random);
        else if (settings.getDiffusionType() == DiffusionType.LINEAR_WALK)
            m.slide(diffusionSigma, axis, random[0]);
        else
            m.move(diffusionSigma, random);
        x[j] = (float) (m.getX());
        y[j] = (float) (m.getY());
        xValues[j] = (float) ((j + 1) / settings.stepsPerSecond);
    }
    // Plot x and y coords on a timeline
    String title = TITLE + " example coordinates";
    Plot2 plot = new Plot2(title, "Time (seconds)", "Distance (um)");
    float[] xUm = convertToUm(x);
    float[] yUm = convertToUm(y);
    float[] limits = Maths.limits(xUm);
    limits = Maths.limits(limits, yUm);
    plot.setLimits(0, totalSteps / settings.stepsPerSecond, limits[0], limits[1]);
    plot.setColor(Color.red);
    plot.addPoints(xValues, xUm, Plot2.LINE);
    plot.setColor(Color.blue);
    plot.addPoints(xValues, yUm, Plot2.LINE);
    Utils.display(title, plot);
    // Scale up and draw 2D position
    for (int j = 0; j < totalSteps; j++) {
        x[j] *= magnification;
        y[j] *= magnification;
    }
    float[] limitsx = getLimits(x);
    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 * magnification);
        limitsx[0] = -width / 2;
    }
    if (height == 0) {
        height = (int) (32 * magnification);
        limitsy[0] = -height / 2;
    }
    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((int) round(x[totalSteps - 1]), (int) round(y[totalSteps - 1]), 255);
    ImagePlus imp = Utils.display(TITLE + " example", ip);
    // Apply the fire lookup table
    WindowManager.setTempCurrentImage(imp);
    LutLoader lut = new LutLoader();
    lut.run("fire");
    WindowManager.setTempCurrentImage(null);
}
Also used : ByteProcessor(ij.process.ByteProcessor) ImageProcessor(ij.process.ImageProcessor) MoleculeModel(gdsc.smlm.model.MoleculeModel) LutLoader(ij.plugin.LutLoader) Plot2(ij.gui.Plot2) ImagePlus(ij.ImagePlus)

Example 52 with ByteProcessor

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

the class PSFCreator method plotSignalAtSpecifiedSD.

/**
	 * Show a plot of the amount of signal within N x SD for each z position. This indicates
	 * how much the PSF has spread from the original Gaussian shape.
	 * 
	 * @param psf
	 *            The PSF
	 * @param fittedSd
	 *            The width of the PSF (in pixels)
	 * @param factor
	 *            The factor to use
	 * @param slice
	 *            The slice used to create the label
	 */
private void plotSignalAtSpecifiedSD(ImageStack psf, double fittedSd, double factor, int slice) {
    if (signalZ == null) {
        // Get the bounds
        int radius = (int) Math.round(fittedSd * factor);
        int min = FastMath.max(0, psf.getWidth() / 2 - radius);
        int max = FastMath.min(psf.getWidth() - 1, psf.getWidth() / 2 + radius);
        // Create a circle mask of the PSF projection
        ByteProcessor circle = new ByteProcessor(max - min + 1, max - min + 1);
        circle.setColor(255);
        circle.fillOval(0, 0, circle.getWidth(), circle.getHeight());
        final byte[] mask = (byte[]) circle.getPixels();
        // Sum the pixels within the mask for each slice
        signalZ = new double[psf.getSize()];
        signal = new double[psf.getSize()];
        for (int i = 0; i < psf.getSize(); i++) {
            double sum = 0;
            float[] data = (float[]) psf.getProcessor(i + 1).getPixels();
            for (int y = min, ii = 0; y <= max; y++) {
                int index = y * psf.getWidth() + min;
                for (int x = min; x <= max; x++, ii++, index++) {
                    if (mask[ii] != 0 && data[index] > 0)
                        sum += data[index];
                }
            }
            double total = 0;
            for (float f : data) if (f > 0)
                total += f;
            signalZ[i] = i + 1;
            signal[i] = 100 * sum / total;
        }
        signalTitle = String.format("%% PSF signal at %s x SD", Utils.rounded(factor, 3));
        signalLimits = Maths.limits(signal);
    }
    // Plot the sum
    boolean alignWindows = (WindowManager.getFrame(signalTitle) == null);
    final double total = signal[slice - 1];
    Plot2 plot = new Plot2(signalTitle, "z", "Signal", signalZ, signal);
    plot.addLabel(0, 0, String.format("Total = %s. z = %s nm", Utils.rounded(total), Utils.rounded((slice - zCentre) * nmPerSlice)));
    plot.setColor(Color.green);
    plot.drawLine(slice, signalLimits[0], slice, signalLimits[1]);
    plot.setColor(Color.blue);
    PlotWindow plotWindow = Utils.display(signalTitle, plot);
    if (alignWindows && plotWindow != null) {
        if (alignWindows && plotWindow != null) {
            PlotWindow otherWindow = getPlot(TITLE_AMPLITUDE);
            if (otherWindow != null) {
                // Put the two plots tiled together so both are visible
                Point l = plotWindow.getLocation();
                l.x = otherWindow.getLocation().x + otherWindow.getWidth();
                l.y = otherWindow.getLocation().y;
                plotWindow.setLocation(l);
            }
        }
    }
}
Also used : ByteProcessor(ij.process.ByteProcessor) PlotWindow(ij.gui.PlotWindow) Plot2(ij.gui.Plot2) Point(java.awt.Point) BasePoint(gdsc.core.match.BasePoint) Point(java.awt.Point) BasePoint(gdsc.core.match.BasePoint)

Example 53 with ByteProcessor

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

the class SplitResults method splitResults.

private void splitResults(MemoryPeakResults results, ImageProcessor ip) {
    IJ.showStatus("Splitting " + Utils.pleural(results.size(), "result"));
    // Create an object mask
    ObjectAnalyzer objectAnalyzer = new ObjectAnalyzer(ip, false);
    final int maxx = ip.getWidth();
    final int maxy = ip.getHeight();
    final float scaleX = (float) results.getBounds().width / maxx;
    final float scaleY = (float) results.getBounds().height / maxy;
    // Create a results set for each object
    final int maxObject = objectAnalyzer.getMaxObject();
    MemoryPeakResults[] resultsSet = new MemoryPeakResults[maxObject + 1];
    for (int object = 0; object <= maxObject; object++) {
        MemoryPeakResults newResults = new MemoryPeakResults();
        newResults.copySettings(results);
        newResults.setName(results.getName() + " " + object);
        resultsSet[object] = newResults;
    }
    final int[] mask = objectAnalyzer.getObjectMask();
    if (showObjectMask) {
        ImageProcessor objectIp = (maxObject <= 255) ? new ByteProcessor(maxx, maxy) : new ShortProcessor(maxx, maxy);
        for (int i = 0; i < mask.length; i++) objectIp.set(i, mask[i]);
        ImagePlus imp = Utils.display(objectMask + " Objects", objectIp);
        imp.setDisplayRange(0, maxObject);
        imp.updateAndDraw();
    }
    // Process the results mapping them to their objects
    int i = 0;
    final int size = results.size();
    final int step = Utils.getProgressInterval(size);
    for (PeakResult result : results.getResults()) {
        if (++i % step == 0)
            IJ.showProgress(i, size);
        // Map to the mask objects
        final int object;
        int x = (int) (result.getXPosition() / scaleX);
        int y = (int) (result.getYPosition() / scaleY);
        if (x < 0 || x >= maxx || y < 0 || y >= maxy) {
            object = 0;
        } else {
            final int index = y * maxx + x;
            if (index < 0 || index >= mask.length)
                object = 0;
            else
                object = mask[index];
        }
        resultsSet[object].add(result);
    }
    IJ.showProgress(1);
    // Add the new results sets to memory
    i = 0;
    for (int object = (nonMaskDataset) ? 0 : 1; object <= maxObject; object++) {
        if (!resultsSet[object].isEmpty()) {
            MemoryPeakResults.addResults(resultsSet[object]);
            i++;
        }
    }
    IJ.showStatus("Split " + Utils.pleural(results.size(), "result") + " into " + Utils.pleural(i, "set"));
}
Also used : ByteProcessor(ij.process.ByteProcessor) ImageProcessor(ij.process.ImageProcessor) ObjectAnalyzer(gdsc.smlm.ij.utils.ObjectAnalyzer) MemoryPeakResults(gdsc.smlm.results.MemoryPeakResults) ImagePlus(ij.ImagePlus) PeakResult(gdsc.smlm.results.PeakResult) ShortProcessor(ij.process.ShortProcessor)

Example 54 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(radius * 1000 / nmPerPixel);
    final int hd = (int) Math.ceil(radius * 1000 / 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(length * 1000 / nmPerPixel);
    if (h > 0) {
        ImageStack newStack = new ImageStack(width, stack.getHeight() + h, stack.getSize());
        for (int slice = 1; slice <= stack.getSize(); slice++) {
            byte[] pixels = (byte[]) stack.getPixels(slice);
            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
    ImageStack newStack = new ImageStack(width, stack.getHeight() + hw, stack.getSize());
    for (int slice = 1; slice <= stack.getSize(); slice++) {
        byte[] pixels = (byte[]) stack.getPixels(slice);
        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 (excludeNucleus) {
        ImageStack stack2 = createNucleusSphere(width, depth);
        int xloc = (stack.getWidth() - stack2.getWidth()) / 2;
        int yloc = (stack.getHeight() - stack2.getHeight()) / 2;
        int offset = (stack.getSize() - stack2.getSize()) / 2;
        for (int slice = 1; slice <= stack2.getSize(); slice++) {
            ImageProcessor ip = stack.getProcessor(slice + offset);
            ImageProcessor ip2 = stack2.getProcessor(slice);
            ip.copyBits(ip2, xloc, yloc, Blitter.SUBTRACT);
        }
    }
    if (squareOutput && stack.getWidth() != stack.getHeight()) {
        ImageStack stack2 = new ImageStack(stack.getHeight(), stack.getHeight());
        int end = stack.getHeight() - stack.getWidth();
        for (int slice = 1; slice <= stack.getSize(); slice++) {
            ImageProcessor ip = stack.getProcessor(slice);
            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 (border > 0) {
        ImageStack stack2 = new ImageStack(stack.getWidth() + 2 * border, stack.getHeight() + 2 * border);
        for (int slice = 1; slice <= stack.getSize(); slice++) {
            ImageProcessor ip = stack.getProcessor(slice);
            ImageProcessor ip2 = new ByteProcessor(stack2.getWidth(), stack2.getHeight());
            stack2.addSlice(ip2);
            ip2.insert(ip, border, border);
        }
        stack = stack2;
    }
    ImagePlus imp;
    if (is2D) {
        // TODO - Remove this laziness since we should really just do a 2D image
        int centre = stack.getSize() / 2;
        imp = Utils.display(TITLE, stack.getProcessor(centre));
    } else {
        imp = Utils.display(TITLE, stack);
    }
    // Calibrate
    Calibration cal = new Calibration();
    cal.setUnit("um");
    cal.pixelWidth = cal.pixelHeight = nmPerPixel / 1000;
    cal.pixelDepth = 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 55 with ByteProcessor

use of ij.process.ByteProcessor in project imagingbook-common by imagingbook.

the class HoughTransformLinesPosRadius method process.

private void process(ImageProcessor ip) {
    ByteProcessor check = new ByteProcessor(M, N);
    if (params.showProgress)
        IJ.showStatus("filling accumulator ...");
    for (int v = 0; v < N; v++) {
        if (params.showProgress)
            IJ.showProgress(v, N);
        for (int u = 0; u < M; u++) {
            if ((0xFFFFFF & ip.get(u, v)) != 0) {
                // this is a foreground (edge) pixel - use ImageAccessor??
                doOnePoint(u, v);
                check.putPixel(u, v, 128);
            }
        }
    }
    if (params.showProgress)
        IJ.showProgress(1, 1);
    if (params.showCheckImage)
        (new ImagePlus("Check", check)).show();
}
Also used : ByteProcessor(ij.process.ByteProcessor) ImagePlus(ij.ImagePlus)

Aggregations

ByteProcessor (ij.process.ByteProcessor)103 ImagePlus (ij.ImagePlus)36 ImageProcessor (ij.process.ImageProcessor)31 FloatProcessor (ij.process.FloatProcessor)23 ShortProcessor (ij.process.ShortProcessor)21 ColorProcessor (ij.process.ColorProcessor)20 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