Search in sources :

Example 81 with ByteProcessor

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

Example 82 with ByteProcessor

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

the class ElasticMontage method extractAndSaveFeatures.

/**
 * Extract SIFT features and save them into the project folder.
 *
 * @param tiles
 * @param siftParam
 * @param clearCache
 * @throws Exception
 */
protected static final void extractAndSaveFeatures(final List<AbstractAffineTile2D<?>> tiles, final FloatArray2DSIFT.Param siftParam, final boolean clearCache) throws Exception {
    final ExecutorService exec = Executors.newFixedThreadPool(p.maxNumThreads);
    /* extract features for all slices and store them to disk */
    final AtomicInteger counter = new AtomicInteger(0);
    final ArrayList<Future<ArrayList<Feature>>> siftTasks = new ArrayList<Future<ArrayList<Feature>>>();
    for (int i = 0; i < tiles.size(); ++i) {
        final int tileIndex = i;
        siftTasks.add(exec.submit(new Callable<ArrayList<Feature>>() {

            @Override
            public ArrayList<Feature> call() {
                final AbstractAffineTile2D<?> tile = tiles.get(tileIndex);
                final String patchName = patchName(tile.getPatch());
                IJ.showProgress(counter.getAndIncrement(), tiles.size() - 1);
                ArrayList<Feature> fs = null;
                if (!clearCache)
                    fs = mpicbg.trakem2.align.Util.deserializeFeatures(tile.getPatch().getProject(), siftParam, null, tile.getPatch().getId());
                if (null == fs) {
                    final FloatArray2DSIFT sift = new FloatArray2DSIFT(siftParam);
                    final SIFT ijSIFT = new SIFT(sift);
                    fs = new ArrayList<Feature>();
                    final ByteProcessor ip = tile.createMaskedByteImage();
                    ijSIFT.extractFeatures(ip, fs);
                    Utils.log(fs.size() + " features extracted for " + patchName);
                    if (!mpicbg.trakem2.align.Util.serializeFeatures(tile.getPatch().getProject(), siftParam, null, tile.getPatch().getId(), fs))
                        Utils.log("FAILED to store serialized features for " + patchName);
                } else
                    Utils.log(fs.size() + " features loaded for " + patchName);
                return fs;
            }
        }));
    }
    /* join */
    for (final Future<ArrayList<Feature>> fu : siftTasks) fu.get();
    siftTasks.clear();
    exec.shutdown();
}
Also used : ByteProcessor(ij.process.ByteProcessor) SIFT(mpicbg.ij.SIFT) FloatArray2DSIFT(mpicbg.imagefeatures.FloatArray2DSIFT) ArrayList(java.util.ArrayList) Feature(mpicbg.imagefeatures.Feature) Point(mpicbg.models.Point) Callable(java.util.concurrent.Callable) FloatArray2DSIFT(mpicbg.imagefeatures.FloatArray2DSIFT) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 83 with ByteProcessor

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

the class AbstractAffineTile2D method createMaskedByteImage.

public final ByteProcessor createMaskedByteImage() {
    final ByteProcessor mask;
    final Patch.PatchImage pai = patch.createTransformedImage();
    if (pai.mask == null)
        mask = pai.outside;
    else
        mask = pai.mask;
    pai.target.setMinAndMax(patch.getMin(), patch.getMax());
    final ByteProcessor target = (ByteProcessor) pai.target.convertToByte(true);
    /* Other than any other ImageProcessor, ByteProcessors ignore scaling, so ... */
    if (ByteProcessor.class.isInstance(pai.target)) {
        final float s = 255.0f / (float) (patch.getMax() - patch.getMin());
        final int m = (int) patch.getMin();
        final byte[] targetBytes = (byte[]) target.getPixels();
        for (int i = 0; i < targetBytes.length; ++i) {
            targetBytes[i] = (byte) (Math.max(0, Math.min(255, ((targetBytes[i] & 0xff) - m) * s)));
        }
        target.setMinAndMax(0, 255);
    }
    if (mask != null) {
        final byte[] targetBytes = (byte[]) target.getPixels();
        final byte[] maskBytes = (byte[]) mask.getPixels();
        if (pai.outside != null) {
            final byte[] outsideBytes = (byte[]) pai.outside.getPixels();
            for (int i = 0; i < outsideBytes.length; ++i) {
                if ((outsideBytes[i] & 0xff) != 255)
                    maskBytes[i] = 0;
                final float a = (float) (maskBytes[i] & 0xff) / 255f;
                final int t = (targetBytes[i] & 0xff);
                targetBytes[i] = (byte) (t * a + 127 * (1 - a));
            }
        } else {
            for (int i = 0; i < targetBytes.length; ++i) {
                final float a = (float) (maskBytes[i] & 0xff) / 255f;
                final int t = (targetBytes[i] & 0xff);
                targetBytes[i] = (byte) (t * a + 127 * (1 - a));
            }
        }
    }
    return target;
}
Also used : ByteProcessor(ij.process.ByteProcessor) Patch(ini.trakem2.display.Patch) Point(mpicbg.models.Point)

Example 84 with ByteProcessor

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

the class Render method render.

/**
 * Renders a patch, mapping its intensities [min, max] &rarr; [0, 1]
 *
 * @param patch the patch to be rendered
 * @param targetImage target pixels, specifies the target box
 * @param targetWeight target weight pixels, depending on alpha
 * @param x target box offset in world coordinates
 * @param y target box offset in world coordinates
 * @param scale target scale
 */
public static final void render(final Patch patch, final int coefficientsWidth, final int coefficientsHeight, final FloatProcessor targetImage, final FloatProcessor targetWeight, final ColorProcessor targetCoefficients, final double x, final double y, final double scale) {
    /* assemble coordinate transformations and add bounding box offset */
    final CoordinateTransformList<CoordinateTransform> ctl = new CoordinateTransformList<CoordinateTransform>();
    ctl.add(patch.getFullCoordinateTransform());
    final AffineModel2D affineScale = new AffineModel2D();
    affineScale.set(scale, 0, 0, scale, -x * scale, -y * scale);
    ctl.add(affineScale);
    /* estimate average scale and generate downsampled source */
    final int width = patch.getOWidth(), height = patch.getOHeight();
    final double s = sampleAverageScale(ctl, width, height, width / patch.getMeshResolution());
    final int mipmapLevel = bestMipmapLevel(s);
    final ImageProcessor ipMipmap = Downsampler.downsampleImageProcessor(patch.getImageProcessor(), mipmapLevel);
    /* create a target */
    final ImageProcessor tp = ipMipmap.createProcessor(targetImage.getWidth(), targetImage.getHeight());
    /* prepare and downsample alpha mask if there is one */
    final ByteProcessor bpMaskMipmap;
    final ByteProcessor bpMaskTarget;
    final ByteProcessor bpMask = patch.getAlphaMask();
    if (bpMask == null) {
        bpMaskMipmap = null;
        bpMaskTarget = null;
    } else {
        bpMaskMipmap = bpMask == null ? null : Downsampler.downsampleByteProcessor(bpMask, mipmapLevel);
        bpMaskTarget = new ByteProcessor(tp.getWidth(), tp.getHeight());
    }
    /* create coefficients map */
    final ColorProcessor cp = new ColorProcessor(ipMipmap.getWidth(), ipMipmap.getHeight());
    final int w = cp.getWidth();
    final int h = cp.getHeight();
    for (int yi = 0; yi < h; ++yi) {
        final int yc = yi * coefficientsHeight / h;
        final int ic = yc * coefficientsWidth;
        final int iyi = yi * w;
        for (int xi = 0; xi < w; ++xi) cp.set(iyi + xi, ic + (xi * coefficientsWidth / w) + 1);
    }
    /* attach mipmap transformation */
    final CoordinateTransformList<CoordinateTransform> ctlMipmap = new CoordinateTransformList<CoordinateTransform>();
    ctlMipmap.add(createScaleLevelTransform(mipmapLevel));
    ctlMipmap.add(ctl);
    /* create mesh */
    final CoordinateTransformMesh mesh = new CoordinateTransformMesh(ctlMipmap, patch.getMeshResolution(), ipMipmap.getWidth(), ipMipmap.getHeight());
    /* render */
    final ImageProcessorWithMasks source = new ImageProcessorWithMasks(ipMipmap, bpMaskMipmap, null);
    final ImageProcessorWithMasks target = new ImageProcessorWithMasks(tp, bpMaskTarget, null);
    final TransformMeshMappingWithMasks<TransformMesh> mapping = new TransformMeshMappingWithMasks<TransformMesh>(mesh);
    mapping.mapInterpolated(source, target, 1);
    final TransformMeshMapping<TransformMesh> coefficientsMapMapping = new TransformMeshMapping<TransformMesh>(mesh);
    coefficientsMapMapping.map(cp, targetCoefficients, 1);
    /* set alpha channel */
    final byte[] alphaPixels;
    if (bpMaskTarget != null)
        alphaPixels = (byte[]) bpMaskTarget.getPixels();
    else
        alphaPixels = (byte[]) target.outside.getPixels();
    /* convert */
    final double min = patch.getMin();
    final double max = patch.getMax();
    final double a = 1.0 / (max - min);
    final double b = 1.0 / 255.0;
    for (int i = 0; i < alphaPixels.length; ++i) targetImage.setf(i, (float) ((tp.getf(i) - min) * a));
    for (int i = 0; i < alphaPixels.length; ++i) targetWeight.setf(i, (float) ((alphaPixels[i] & 0xff) * b));
}
Also used : ByteProcessor(ij.process.ByteProcessor) TransformMeshMapping(mpicbg.ij.TransformMeshMapping) ImageProcessorWithMasks(mpicbg.trakem2.transform.TransformMeshMappingWithMasks.ImageProcessorWithMasks) CoordinateTransformList(mpicbg.models.CoordinateTransformList) Point(mpicbg.models.Point) ImageProcessor(ij.process.ImageProcessor) ColorProcessor(ij.process.ColorProcessor) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh) AffineModel2D(mpicbg.models.AffineModel2D) TransformMeshMappingWithMasks(mpicbg.trakem2.transform.TransformMeshMappingWithMasks) CoordinateTransform(mpicbg.models.CoordinateTransform) TransformMesh(mpicbg.models.TransformMesh) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh)

Example 85 with ByteProcessor

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

the class ImageArrayConverter method ImageToFloatArray2DDeprecated.

public static float[][] ImageToFloatArray2DDeprecated(ImageProcessor ip) {
    float[][] image;
    Object pixelArray = ip.getPixels();
    int count = 0;
    if (ip instanceof ByteProcessor) {
        image = new float[ip.getWidth()][ip.getHeight()];
        byte[] pixels = (byte[]) pixelArray;
        for (int y = 0; y < ip.getHeight(); y++) for (int x = 0; x < ip.getWidth(); x++) image[x][y] = pixels[count++] & 0xff;
    } else if (ip instanceof ShortProcessor) {
        image = new float[ip.getWidth()][ip.getHeight()];
        short[] pixels = (short[]) pixelArray;
        for (int y = 0; y < ip.getHeight(); y++) for (int x = 0; x < ip.getWidth(); x++) image[x][y] = pixels[count++] & 0xffff;
    } else if (ip instanceof FloatProcessor) {
        image = new float[ip.getWidth()][ip.getHeight()];
        float[] pixels = (float[]) pixelArray;
        for (int y = 0; y < ip.getHeight(); y++) for (int x = 0; x < ip.getWidth(); x++) image[x][y] = pixels[count++];
    } else // RGB
    {
        image = new float[ip.getWidth()][ip.getHeight()];
        int[] pixels = (int[]) pixelArray;
    // still unknown how to do...
    /*
            for (int y = 0; y < ip.getHeight(); y++)
                for (int x = 0; x < ip.getWidth(); x++)
                        image[x][y] = pixels[count++];// & 0xffffff;
            */
    }
    return image;
}
Also used : ByteProcessor(ij.process.ByteProcessor) FloatProcessor(ij.process.FloatProcessor) Point(java.awt.Point) ShortProcessor(ij.process.ShortProcessor)

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