Search in sources :

Example 26 with ShortProcessor

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

the class DownsamplerTest method main.

/**
 * @param args
 */
public static void main(final String[] args) {
    new ImageJ();
    final Timer timer = new Timer();
    final ImagePlus imp = new ImagePlus("/home/saalfeld/tmp/fetter-example.tif");
    // final ImagePlus imp = new ImagePlus( "/home/albert/Desktop/t2/fetter-example.tif" );
    // final ImagePlus imp = new ImagePlus( "/home/saalfeld/Desktop/norway.jpg" );
    imp.show();
    final ImageProcessor ip = imp.getProcessor().duplicate();
    System.out.println("short");
    final ShortProcessor ipShort = (ShortProcessor) ip.convertToShort(false);
    for (int i = 0; i < n; ++i) {
        timer.start();
        testShort(ipShort);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("downsampleShort() + downsampleByteProcessor() (independent short with byte mapping + alpha)");
    for (int i = 0; i < n; ++i) {
        final Pair<ShortProcessor, byte[]> ba = new Pair<ShortProcessor, byte[]>(ipShort, (byte[]) ipShort.convertToByte(true).getPixels());
        final ByteProcessor alpha = new ByteProcessor(ipShort.getWidth(), ipShort.getHeight(), (byte[]) ipShort.convertToByte(true).getPixels(), null);
        timer.start();
        testShortAlphaIndependently(ba, alpha);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("downsampleShortProcessor() + convertToByte() + downsampleByteProcessor() (independent short + byte mapping + alpha)");
    for (int i = 0; i < n; ++i) {
        final ByteProcessor alpha = new ByteProcessor(ipShort.getWidth(), ipShort.getHeight(), (byte[]) ipShort.convertToByte(true).getPixels(), null);
        timer.start();
        testShortAlphaByteMappingIndependently(ipShort, alpha);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("float");
    final FloatProcessor ipFloat = (FloatProcessor) ip.convertToFloat();
    for (int i = 0; i < n; ++i) {
        timer.start();
        testFloat(ipFloat);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("independent float with byte mapping + alpha");
    for (int i = 0; i < n; ++i) {
        final Pair<FloatProcessor, byte[]> ba = new Pair<FloatProcessor, byte[]>(ipFloat, (byte[]) ipShort.convertToByte(true).getPixels());
        final ByteProcessor alpha = new ByteProcessor(ipShort.getWidth(), ipShort.getHeight(), (byte[]) ipShort.convertToByte(true).getPixels(), null);
        timer.start();
        testFloatAlphaIndependently(ba, alpha);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("byte");
    final ByteProcessor ipByte = (ByteProcessor) ip.convertToByte(true);
    for (int i = 0; i < n; ++i) {
        timer.start();
        testByte(ipByte);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("2 x byte");
    final ByteProcessor ipByte2 = (ByteProcessor) ipByte.duplicate();
    for (int i = 0; i < n; ++i) {
        timer.start();
        testByte(ipByte);
        testByte(ipByte2);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("color");
    final ColorProcessor ipColor = (ColorProcessor) ip.convertToRGB();
    for (int i = 0; i < n; ++i) {
        timer.start();
        testColor(ipColor);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("independent color with byte mapping + alpha");
    for (int i = 0; i < n; ++i) {
        final byte[][] rgb = new byte[4][ipColor.getWidth() * ipColor.getHeight()];
        ipColor.getRGB(rgb[0], rgb[1], rgb[2]);
        final Pair<ColorProcessor, byte[][]> ba = new Pair<ColorProcessor, byte[][]>(ipColor, rgb);
        final ByteProcessor alpha = new ByteProcessor(ipShort.getWidth(), ipShort.getHeight(), (byte[]) ipShort.convertToByte(true).getPixels(), null);
        timer.start();
        testColorAlphaIndependently(ba, alpha);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("alpha + outside");
    for (int i = 0; i < n; ++i) {
        ByteProcessor outside = new ByteProcessor(ipByte.getWidth(), ipByte.getHeight());
        outside.setRoi(new OvalRoi(100, 100, ipByte.getWidth() - 200, ipByte.getHeight() - 200));
        outside.setValue(255);
        outside.fill(outside.getMask());
        final Pair<ByteProcessor, ByteProcessor> ba = new Pair<ByteProcessor, ByteProcessor>(ipByte, outside);
        timer.start();
        testAlphaOutside(ba);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
    System.out.println("outside");
    for (int i = 0; i < n; ++i) {
        ByteProcessor outside = new ByteProcessor(ipByte.getWidth(), ipByte.getHeight());
        outside.setRoi(new OvalRoi(100, 100, ipByte.getWidth() - 200, ipByte.getHeight() - 200));
        outside.setValue(255);
        outside.fill(outside.getMask());
        timer.start();
        testOutside(outside);
        final long t = timer.stop();
        System.out.println(i + ": " + t + "ms");
    }
// System.out.println( "byte integral" );
// final ByteProcessor ipByteI = ( ByteProcessor )ipShort.convertToByte( true );
// 
// for ( int i = 0; i < 10; ++i )
// {
// timer.start();
// testByteIntegral( ipByteI );
// final long t = timer.stop();
// System.out.println( i + ": " + t  + "ms" );
// }
}
Also used : ByteProcessor(ij.process.ByteProcessor) FloatProcessor(ij.process.FloatProcessor) OvalRoi(ij.gui.OvalRoi) ImagePlus(ij.ImagePlus) ShortProcessor(ij.process.ShortProcessor) ImageJ(ij.ImageJ) ImageProcessor(ij.process.ImageProcessor) ColorProcessor(ij.process.ColorProcessor) Timer(mpicbg.util.Timer) Pair(mpicbg.trakem2.util.Downsampler.Pair)

Example 27 with ShortProcessor

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

the class CorrectBackground method process.

@Override
public ImageProcessor process(final ImageProcessor ip) {
    final ShortProcessor sp;
    try {
        sp = (ShortProcessor) ip;
    } catch (ClassCastException cce) {
        System.out.println("CorrectBackground supports 16-bit images only!");
        return ip;
    }
    final double min = ip.getMin(), max = ip.getMax();
    final long[] hist = IntegralHistogram2d.integralHistogram2d((short[]) sp.getPixels(), sp.getWidth(), sp.getHeight(), nBins, min, max);
    final short[] median = IntegralHistogram2d.median(sp.getWidth(), sp.getHeight(), hist, nBins, min, max, this.medianRadius);
    if (this.postGaussian) {
        ij.plugin.filter.GaussianBlur g = new ij.plugin.filter.GaussianBlur();
        g.blurGaussian(new ShortProcessor(sp.getWidth(), sp.getHeight(), median, null), this.sigma, this.sigma, 0.02);
    }
    // Approximate mean image value (within min-max range) from the histogram present in the last set of nBins in the integral histogram
    final double binInc = (max - min + 1) / nBins;
    double sum = 0;
    long count = 0;
    for (int i = hist.length - nBins, j = 0; j < nBins; ++i, ++j) {
        sum += hist[i] * (min + (j * binInc));
        count += hist[i];
    }
    // TODO error the sum of hist[-16] to hist[-1] doesn't add up to width*height !!!
    // can't use median.length or p.length, there is an erroneous mismatch
    final double mean = sum / count;
    final short[] p = (short[]) sp.getPixels();
    // The pixels of the provided image
    for (int i = 0; i < p.length; ++i) {
        final double m = median[i] & 0xffff;
        p[i] = (short) (((p[i] & 0xffff) / m) * mean);
    }
    return ip;
}
Also used : ShortProcessor(ij.process.ShortProcessor)

Example 28 with ShortProcessor

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

the class IntegralHistogram2d method main.

@SuppressWarnings("unused")
public static final void main(String[] args) {
    final ShortProcessor sp = (ShortProcessor) IJ.openImage("/home/albert/Desktop/t2/bridge-16bit.tif").getProcessor();
    final ShortProcessor filtered = median(sp, 64, 0, 65535, 100);
    new ImageJ();
    new ImagePlus("median", filtered).show();
}
Also used : ImageJ(ij.ImageJ) ImagePlus(ij.ImagePlus) ShortProcessor(ij.process.ShortProcessor)

Example 29 with ShortProcessor

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

the class ExportUnsignedShort method mapIntensities.

protected static final ShortProcessor mapIntensities(final PatchIntensityRange pir, final double min, final double max) {
    final double a = 65535.0 / (max - min);
    final ImageProcessor source = pir.patch.getImageProcessor();
    final short[] targetPixels = new short[source.getWidth() * source.getHeight()];
    for (int i = 0; i < targetPixels.length; ++i) {
        targetPixels[i] = (short) Math.max(0, Math.min(65535, Math.round(((source.getf(i) - pir.patch.getMin()) / pir.a - min) * a)));
    }
    final ShortProcessor target = new ShortProcessor(source.getWidth(), source.getHeight(), targetPixels, null);
    target.setMinAndMax(-min * a, (1.0 - min) * a);
    return target;
}
Also used : ImageProcessor(ij.process.ImageProcessor) ShortProcessor(ij.process.ShortProcessor)

Example 30 with ShortProcessor

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

the class ExportUnsignedShort method makeFlatImage.

public static final Pair<ShortProcessor, ByteProcessor> makeFlatImage(final List<Patch> patches, final Rectangle roi, final double backgroundValue, final double scale, final boolean makeAlphaMask) {
    final ArrayList<PatchIntensityRange> patchIntensityRanges = new ArrayList<PatchIntensityRange>();
    double min = Double.MAX_VALUE;
    double max = -Double.MAX_VALUE;
    for (final Displayable d : patches) {
        final Patch patch = (Patch) d;
        final PatchIntensityRange pir = new PatchIntensityRange(patch);
        if (pir.min < min)
            min = pir.min;
        if (pir.max > max)
            max = pir.max;
        patchIntensityRanges.add(pir);
    }
    final double minI = -min * 65535.0 / (max - min);
    final double maxI = (1.0 - min) * 65535.0 / (max - min);
    final ShortProcessor sp;
    if (Double.isNaN(scale)) {
        sp = new ShortProcessor(roi.width, roi.height);
    } else {
        sp = new ShortProcessor((int) (roi.width * scale + 0.5), (int) (roi.height * scale + 0.5));
    }
    sp.setMinAndMax(minI, maxI);
    if (0 != backgroundValue) {
        sp.setValue(backgroundValue);
        sp.setRoi(0, 0, roi.width, roi.height);
        sp.fill();
    }
    final ByteProcessor alphaTarget = makeAlphaMask ? new ByteProcessor(sp.getWidth(), sp.getHeight()) : null;
    for (final PatchIntensityRange pir : patchIntensityRanges) {
        map(new PatchTransform(pir), roi.x, roi.y, scale, mapIntensities(pir, min, max), sp, alphaTarget);
    }
    return new Pair<ShortProcessor, ByteProcessor>(sp, alphaTarget);
}
Also used : ByteProcessor(ij.process.ByteProcessor) Displayable(ini.trakem2.display.Displayable) ArrayList(java.util.ArrayList) Patch(ini.trakem2.display.Patch) ShortProcessor(ij.process.ShortProcessor) Pair(mpicbg.trakem2.util.Pair)

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