Search in sources :

Example 66 with ByteProcessor

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

the class Downsampler method downsampleAlphaAndOutside.

/**
 * Combine an alpha and outside mask into a downsampled alpha and outside
 * mask.  Those pixels not fully covered in the outside mask are set to 0,
 * all others to their interpolated value.
 *
 * @param aAlpha
 * @param aOutside
 * @return
 */
public static final Pair<ByteProcessor, ByteProcessor> downsampleAlphaAndOutside(final ByteProcessor aAlpha, final ByteProcessor aOutside) {
    final int wa = aAlpha.getWidth();
    final int ha = aAlpha.getHeight();
    final int wa2 = wa + wa;
    final int wb = wa / 2;
    final int hb = ha / 2;
    final int nb = hb * wb;
    final ByteProcessor bAlpha = new ByteProcessor(wb, hb);
    final ByteProcessor bOutside = new ByteProcessor(wb, hb);
    final byte[] aAlphaPixels = (byte[]) aAlpha.getPixels();
    final byte[] aOutsidePixels = (byte[]) aOutside.getPixels();
    final byte[] bAlphaPixels = (byte[]) bAlpha.getPixels();
    final byte[] bOutsidePixels = (byte[]) bOutside.getPixels();
    for (int ya = 0, yb = 0; yb < nb; ya += wa2, yb += wb) {
        final int ya1 = ya + wa;
        for (int xa = 0, xb = 0; xb < wb; xa += 2, ++xb) {
            final int xa1 = xa + 1;
            final int yaxa = ya + xa;
            final int yaxa1 = ya + xa1;
            final int ya1xa = ya1 + xa;
            final int ya1xa1 = ya1 + xa1;
            final int ybxb = yb + xb;
            final int sOutside = andByte(yaxa, yaxa1, ya1xa, ya1xa1, aOutsidePixels);
            combineAlphaAndOutside(sOutside, yaxa, yaxa1, ya1xa, ya1xa1, ybxb, aAlphaPixels, bAlphaPixels, bOutsidePixels);
        }
    }
    return new Pair<ByteProcessor, ByteProcessor>(bAlpha, bOutside);
}
Also used : ByteProcessor(ij.process.ByteProcessor)

Example 67 with ByteProcessor

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

the class Downsampler method downsampleByteProcessor.

public static final ByteProcessor downsampleByteProcessor(final ByteProcessor a) {
    final int wa = a.getWidth();
    final int ha = a.getHeight();
    final int wa2 = wa + wa;
    final int wb = wa / 2;
    final int hb = ha / 2;
    final int nb = hb * wb;
    final ByteProcessor b = new ByteProcessor(wb, hb);
    final byte[] aPixels = (byte[]) a.getPixels();
    final byte[] bPixels = (byte[]) b.getPixels();
    for (int ya = 0, yb = 0; yb < nb; ya += wa2, yb += wb) {
        final int ya1 = ya + wa;
        for (int xa = 0, xb = 0; xb < wb; xa += 2, ++xb) {
            final int xa1 = xa + 1;
            final int s = averageByte(ya + xa, ya + xa1, ya1 + xa, ya1 + xa1, aPixels);
            bPixels[yb + xb] = (byte) s;
        }
    }
    return b;
}
Also used : ByteProcessor(ij.process.ByteProcessor)

Example 68 with ByteProcessor

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

the class DownsamplerTest method testByteIntegral.

private static final void testByteIntegral(ByteProcessor ipByte) {
    while (ipByte.getWidth() > 32) {
        int w = ipByte.getWidth(), h = ipByte.getHeight();
        long[] l = FastIntegralImage.longIntegralImage((byte[]) ipByte.getPixels(), w, h);
        ipByte = new ByteProcessor(w / 2, h / 2, FastIntegralImage.scaleAreaAverage(l, w + 1, h + 1, w / 2, h / 2), null);
    }
}
Also used : ByteProcessor(ij.process.ByteProcessor)

Example 69 with ByteProcessor

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

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

the class DownsamplerTest method testShortAlphaByteMappingIndependently.

/**
 * Test downsampling the pyramid of a short image + byte alpha channel
 * including the byte mapping of the short all in separate loops.
 *
 * @param ba
 */
private static final void testShortAlphaByteMappingIndependently(ShortProcessor sp, ByteProcessor alpha) {
    ByteProcessor bp;
    while (sp.getWidth() > 32) {
        sp = Downsampler.downsampleShortProcessor(sp);
        bp = (ByteProcessor) sp.convertToByte(true);
        alpha = Downsampler.downsampleByteProcessor(alpha);
    // new ImagePlus( "pixels " + ba.a.getWidth(), ba.a ).show();
    // new ImagePlus( "pixels to byte " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 0 ], null ) ).show();
    // new ImagePlus( "alpha " + ba.a.getWidth(), new ByteProcessor( ba.a.getWidth(), ba.a.getHeight(), ba.b[ 1 ], null ) ).show();
    }
}
Also used : ByteProcessor(ij.process.ByteProcessor)

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