Search in sources :

Example 1 with ShortProcessor

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

the class CreateData method updateArea.

private void updateArea(int[] mask, int w, int h) {
    // Note: The apparent area will be bigger due to the PSF width blurring the edges.
    // Assume the entire max intensity mask is in focus and the PSF is Gaussian in the focal plane.
    // Blur the mask
    float[] pixels = new float[mask.length];
    for (int i = 0; i < pixels.length; i++) pixels[i] = mask[i];
    GaussianFilter blur = new GaussianFilter();
    final double scaleX = (double) settings.size / w;
    final double scaleY = (double) settings.size / h;
    // Allow extra?
    double extra = 1;
    double sd = getPsfSD() * extra;
    blur.convolve(pixels, w, h, sd / scaleX, sd / scaleY);
    // Count pixels in blurred mask. Ignore those that are very faint (at the edge of the region)
    int c = 0;
    //		// By fraction of max value
    //		float limit = 0.1f;
    //		//float min = (float) (Maths.max(pixels) * limit);
    //		float min = limit;
    //		for (float f : pixels)
    //			if (f > min)
    //				c++;
    //		// Rank in order and get fraction of sum
    //		Arrays.sort(pixels);
    //		double sum = 0;
    //		double stop = Maths.sum(pixels) * 0.95;
    //		float after = Maths.max(pixels) + 1;
    //		for (float f : pixels)
    //		{
    //			sum += f;
    //			if (sum > stop)
    //			{
    //				break;
    //				//after = f;
    //				//stop = Float.POSITIVE_INFINITY;
    //			}
    //			if (f > after)
    //				break;
    //			c++;
    //		}
    // Threshold to a mask
    FloatProcessor fp = new FloatProcessor(w, h, pixels);
    ShortProcessor sp = (ShortProcessor) fp.convertToShort(true);
    final int t = AutoThreshold.getThreshold(AutoThreshold.Method.OTSU, sp.getHistogram());
    //Utils.display("Blurred", fp);
    for (int i = 0; i < mask.length; i++) if (sp.get(i) >= t)
        c++;
    // Convert 
    final double scale = ((double) c) / mask.length;
    //System.out.printf("Scale = %f\n", scale);
    areaInUm = scale * settings.size * settings.pixelPitch * settings.size * settings.pixelPitch / 1e6;
}
Also used : GaussianFilter(gdsc.smlm.filters.GaussianFilter) FloatProcessor(ij.process.FloatProcessor) ShortProcessor(ij.process.ShortProcessor)

Example 2 with ShortProcessor

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

the class PCPALMMolecules method drawImage.

static ImageProcessor drawImage(ArrayList<Molecule> molecules, double minx, double miny, double maxx, double maxy, double nmPerPixel, boolean checkBounds, boolean binary) {
    double scalex = maxx - minx;
    double scaley = maxy - miny;
    int width = (int) Math.round(scalex / nmPerPixel) + 1;
    int height = (int) Math.round(scaley / nmPerPixel) + 1;
    // ***
    if (binary) {
        byte[] data = new byte[width * height];
        for (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.
            int x = (int) Math.round((m.x - minx) / nmPerPixel);
            int y = (int) Math.round((m.y - miny) / nmPerPixel);
            int index = y * width + x;
            // Construct a binary image
            data[index] = (byte) 1;
        }
        ByteProcessor ip = new ByteProcessor(width, height, data, null);
        ip.setMinAndMax(0, 1);
        return ip;
    } else {
        short[] data = new short[width * height];
        for (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.
            int x = (int) Math.round((m.x - minx) / nmPerPixel);
            int y = (int) Math.round((m.y - miny) / nmPerPixel);
            int index = y * width + x;
            // Construct a count image
            data[index]++;
        }
        ShortProcessor ip = new ShortProcessor(width, height, data, null);
        ip.setMinAndMax(0, Maths.max(data));
        return ip;
    }
}
Also used : ByteProcessor(ij.process.ByteProcessor) WeightedObservedPoint(org.apache.commons.math3.fitting.WeightedObservedPoint) ClusterPoint(gdsc.core.clustering.ClusterPoint) ShortProcessor(ij.process.ShortProcessor)

Example 3 with ShortProcessor

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

the class RegionLabeling method makeLabelImageGray.

private ShortProcessor makeLabelImageGray() {
    ShortProcessor sp = new ShortProcessor(width, height);
    for (int v = 0; v < height; v++) {
        for (int u = 0; u < width; u++) {
            int lb = getLabel(u, v);
            sp.set(u, v, (lb >= 0) ? lb : 0);
        }
    }
    sp.resetMinAndMax();
    return sp;
}
Also used : Point(java.awt.Point) ShortProcessor(ij.process.ShortProcessor)

Example 4 with ShortProcessor

use of ij.process.ShortProcessor in project bioformats by openmicroscopy.

the class ImageProcessorReader method openProcessors.

/**
 * Returns an array of ImageProcessors that represent the given slice.
 * There is one ImageProcessor per RGB channel;
 * i.e., length of returned array == getRGBChannelCount().
 *
 * @param no Position of image plane.
 */
public ImageProcessor[] openProcessors(int no, int x, int y, int w, int h) throws FormatException, IOException {
    // read byte array
    byte[] b = openBytes(no, x, y, w, h);
    int c = getRGBChannelCount();
    int type = getPixelType();
    int bpp = FormatTools.getBytesPerPixel(type);
    boolean interleave = isInterleaved();
    if (b.length != w * h * c * bpp && b.length != w * h * bpp) {
        throw new FormatException("Invalid byte array length: " + b.length + " (expected w=" + w + ", h=" + h + ", c=" + c + ", bpp=" + bpp + ")");
    }
    // create a color model for this plane (null means default)
    final LUT cm = createColorModel();
    // convert byte array to appropriate primitive array type
    boolean isFloat = FormatTools.isFloatingPoint(type);
    boolean isLittle = isLittleEndian();
    boolean isSigned = FormatTools.isSigned(type);
    // construct image processors
    ImageProcessor[] ip = new ImageProcessor[c];
    for (int i = 0; i < c; i++) {
        byte[] channel = ImageTools.splitChannels(b, i, c, bpp, false, interleave);
        Object pixels = DataTools.makeDataArray(channel, bpp, isFloat, isLittle);
        if (pixels instanceof byte[]) {
            byte[] q = (byte[]) pixels;
            if (q.length != w * h) {
                byte[] tmp = q;
                q = new byte[w * h];
                System.arraycopy(tmp, 0, q, 0, Math.min(q.length, tmp.length));
            }
            if (isSigned)
                q = DataTools.makeSigned(q);
            ip[i] = new ByteProcessor(w, h, q, null);
            if (cm != null)
                ip[i].setColorModel(cm);
        } else if (pixels instanceof short[]) {
            short[] q = (short[]) pixels;
            if (q.length != w * h) {
                short[] tmp = q;
                q = new short[w * h];
                System.arraycopy(tmp, 0, q, 0, Math.min(q.length, tmp.length));
            }
            if (isSigned)
                q = DataTools.makeSigned(q);
            ip[i] = new ShortProcessor(w, h, q, cm);
        } else if (pixels instanceof int[]) {
            int[] q = (int[]) pixels;
            if (q.length != w * h) {
                int[] tmp = q;
                q = new int[w * h];
                System.arraycopy(tmp, 0, q, 0, Math.min(q.length, tmp.length));
            }
            ip[i] = new FloatProcessor(w, h, q);
        } else if (pixels instanceof float[]) {
            float[] q = (float[]) pixels;
            if (q.length != w * h) {
                float[] tmp = q;
                q = new float[w * h];
                System.arraycopy(tmp, 0, q, 0, Math.min(q.length, tmp.length));
            }
            ip[i] = new FloatProcessor(w, h, q, null);
        } else if (pixels instanceof double[]) {
            double[] q = (double[]) pixels;
            if (q.length != w * h) {
                double[] tmp = q;
                q = new double[w * h];
                System.arraycopy(tmp, 0, q, 0, Math.min(q.length, tmp.length));
            }
            ip[i] = new FloatProcessor(w, h, q);
        }
    }
    return ip;
}
Also used : ByteProcessor(ij.process.ByteProcessor) FloatProcessor(ij.process.FloatProcessor) LUT(ij.process.LUT) FormatException(loci.formats.FormatException) ShortProcessor(ij.process.ShortProcessor) ImageProcessor(ij.process.ImageProcessor)

Example 5 with ShortProcessor

use of ij.process.ShortProcessor in project TrakEM2 by trakem2.

the class DownsamplerMipMaps method create.

public static final ImageBytes[] create(final Patch patch, final int type, final int n_levels, final ImageProcessor ip, final ByteProcessor alpha, final ByteProcessor outside) {
    // Create pyramid
    final ImageBytes[] p = new ImageBytes[n_levels];
    if (null == alpha && null == outside) {
        int i = 1;
        switch(type) {
            case ImagePlus.GRAY8:
                ByteProcessor bp = (ByteProcessor) ip;
                p[0] = asBytes(bp);
                while (i < p.length) {
                    bp = Downsampler.downsampleByteProcessor(bp);
                    p[i++] = asBytes(bp);
                }
                break;
            case ImagePlus.GRAY16:
                ShortProcessor sp = (ShortProcessor) ip;
                p[0] = asBytes(sp);
                Pair<ShortProcessor, byte[]> rs;
                while (i < p.length) {
                    rs = Downsampler.downsampleShort(sp);
                    sp = rs.a;
                    p[i++] = new ImageBytes(new byte[][] { rs.b }, sp.getWidth(), sp.getHeight());
                }
                break;
            case ImagePlus.GRAY32:
                FloatProcessor fp = (FloatProcessor) ip;
                p[0] = asBytes(fp);
                Pair<FloatProcessor, byte[]> rf;
                while (i < p.length) {
                    rf = Downsampler.downsampleFloat(fp);
                    fp = rf.a;
                    p[i++] = new ImageBytes(new byte[][] { rf.b }, fp.getWidth(), fp.getHeight());
                }
                break;
            case ImagePlus.COLOR_RGB:
                ColorProcessor cp = (ColorProcessor) ip;
                // TODO the int[] could be reused
                p[0] = asBytes(cp);
                Pair<ColorProcessor, byte[][]> rc;
                while (i < p.length) {
                    rc = Downsampler.downsampleColor(cp);
                    cp = rc.a;
                    p[i++] = new ImageBytes(rc.b, cp.getWidth(), cp.getHeight());
                }
                break;
        }
    } else {
        // Alpha channel
        final ByteProcessor[] masks = new ByteProcessor[p.length];
        if (null != alpha && null != outside) {
            // Use both alpha and outside:
            final byte[] b1 = (byte[]) alpha.getPixels(), b2 = (byte[]) outside.getPixels();
            for (int i = 0; i < b1.length; ++i) {
                // 'outside' is a binary mask, qualitative. -1 means 255
                b1[i] = b2[i] != -1 ? 0 : b1[i];
            }
            masks[0] = alpha;
            // 
            int i = 1;
            Pair<ByteProcessor, ByteProcessor> pair;
            ByteProcessor a = alpha, o = outside;
            while (i < p.length) {
                pair = Downsampler.downsampleAlphaAndOutside(a, o);
                a = pair.a;
                o = pair.b;
                // o is already combined into it
                masks[i] = a;
                ++i;
            }
        } else {
            // Only one of the two is not null:
            if (null == alpha) {
                masks[0] = outside;
                int i = 1;
                while (i < p.length) {
                    masks[i] = Downsampler.downsampleOutside(masks[i - 1]);
                    ++i;
                }
            } else {
                masks[0] = alpha;
                int i = 1;
                while (i < p.length) {
                    masks[i] = Downsampler.downsampleByteProcessor(masks[i - 1]);
                    ++i;
                }
            }
        }
        // Image channels
        int i = 1;
        switch(type) {
            case ImagePlus.GRAY8:
                ByteProcessor bp = (ByteProcessor) ip;
                p[0] = asBytes(bp, masks[0]);
                while (i < p.length) {
                    bp = Downsampler.downsampleByteProcessor(bp);
                    p[i] = asBytes(bp, masks[i]);
                    ++i;
                }
                break;
            case ImagePlus.GRAY16:
                ShortProcessor sp = (ShortProcessor) ip;
                p[0] = asBytes(sp, masks[0]);
                while (i < p.length) {
                    final Pair<ShortProcessor, byte[]> rs = Downsampler.downsampleShort(sp);
                    sp = rs.a;
                    p[i] = new ImageBytes(new byte[][] { rs.b, (byte[]) masks[i].getPixels() }, sp.getWidth(), sp.getHeight());
                    ++i;
                }
                break;
            case ImagePlus.GRAY32:
                FloatProcessor fp = (FloatProcessor) ip;
                p[0] = asBytes(fp, masks[0]);
                while (i < p.length) {
                    final Pair<FloatProcessor, byte[]> rs = Downsampler.downsampleFloat(fp);
                    fp = rs.a;
                    p[i] = new ImageBytes(new byte[][] { rs.b, (byte[]) masks[i].getPixels() }, fp.getWidth(), fp.getHeight());
                    ++i;
                }
                break;
            case ImagePlus.COLOR_RGB:
                ColorProcessor cp = (ColorProcessor) ip;
                // TODO the int[] could be reused
                p[0] = asBytes(cp, masks[0]);
                while (i < p.length) {
                    final Pair<ColorProcessor, byte[][]> rs = Downsampler.downsampleColor(cp);
                    cp = rs.a;
                    final byte[][] rgb = rs.b;
                    p[i] = new ImageBytes(new byte[][] { rgb[0], rgb[1], rgb[2], (byte[]) masks[i].getPixels() }, cp.getWidth(), cp.getHeight());
                    ++i;
                }
                break;
        }
    }
    return p;
}
Also used : ByteProcessor(ij.process.ByteProcessor) FloatProcessor(ij.process.FloatProcessor) ShortProcessor(ij.process.ShortProcessor) ColorProcessor(ij.process.ColorProcessor)

Aggregations

ShortProcessor (ij.process.ShortProcessor)34 FloatProcessor (ij.process.FloatProcessor)20 ByteProcessor (ij.process.ByteProcessor)19 ImageProcessor (ij.process.ImageProcessor)10 ImagePlus (ij.ImagePlus)8 Point (java.awt.Point)8 Rectangle (java.awt.Rectangle)6 ArrayList (java.util.ArrayList)6 ImageStack (ij.ImageStack)4 ColorProcessor (ij.process.ColorProcessor)4 LUT (ij.process.LUT)3 Displayable (ini.trakem2.display.Displayable)3 Patch (ini.trakem2.display.Patch)3 FormatException (loci.formats.FormatException)3 ImageJ (ij.ImageJ)2 GenericDialog (ij.gui.GenericDialog)2 Calibration (ij.measure.Calibration)2 AffineTransform (java.awt.geom.AffineTransform)2 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2