Search in sources :

Example 1 with BitType

use of mpicbg.imglib.type.logic.BitType in project TrakEM2 by trakem2.

the class BinaryInterpolation2D method process.

/**
 * The first time, it will prepare the distance transform images, which are computed only once.
 */
public Image<BitType> process(final float weight) {
    synchronized (this) {
        if (null == idt1 || null == idt2) {
            ExecutorService exec = Executors.newFixedThreadPool(Math.min(2, Runtime.getRuntime().availableProcessors()));
            Future<IDT2D> fu1 = exec.submit(new NewITD2D(img1));
            Future<IDT2D> fu2 = exec.submit(new NewITD2D(img2));
            exec.shutdown();
            try {
                this.idt1 = fu1.get();
                this.idt2 = fu2.get();
            } catch (InterruptedException ie) {
                throw new RuntimeException(ie);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
    }
    // Cannot just img1.createNewImage() because the container may not be able to receive data,
    // such as the ShapeList container.
    final ImageFactory<BitType> f = new ImageFactory<BitType>(new BitType(), new ArrayContainerFactory());
    final Image<BitType> interpolated = f.createImage(new int[] { img1.getDimension(0), img1.getDimension(1) });
    if (img1.getContainer().compareStorageContainerCompatibility(img2.getContainer())) {
        final Cursor<IntType> c1 = idt1.result.createCursor();
        final Cursor<IntType> c2 = idt2.result.createCursor();
        final Cursor<BitType> ci = interpolated.createCursor();
        while (ci.hasNext()) {
            c1.fwd();
            c2.fwd();
            ci.fwd();
            if ((c1.getType().get() * weight) + (c2.getType().get() * (1 - weight)) > 0) {
                ci.getType().set(true);
            }
        }
        c1.close();
        c2.close();
        ci.close();
    } else {
        System.out.println("using option 2");
        final LocalizableByDimCursor<IntType> c1 = idt1.result.createLocalizableByDimCursor();
        final LocalizableByDimCursor<IntType> c2 = idt2.result.createLocalizableByDimCursor();
        final LocalizableByDimCursor<BitType> ci = interpolated.createLocalizableByDimCursor();
        while (ci.hasNext()) {
            ci.fwd();
            c1.setPosition(ci);
            c2.setPosition(ci);
            if (0 <= c1.getType().get() * weight + c2.getType().get() * (1 - weight)) {
                ci.getType().set(true);
            }
        }
        c1.close();
        c2.close();
        ci.close();
    }
    return interpolated;
}
Also used : ArrayContainerFactory(mpicbg.imglib.container.array.ArrayContainerFactory) IntType(mpicbg.imglib.type.numeric.integer.IntType) ImageFactory(mpicbg.imglib.image.ImageFactory) BitType(mpicbg.imglib.type.logic.BitType) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with BitType

use of mpicbg.imglib.type.logic.BitType in project TrakEM2 by trakem2.

the class AreaUtils method manyToManyInterpolation.

public static final Area[] manyToManyInterpolation(final Area a1, final Area a2, final int nInterpolates) throws InterruptedException, ExecutionException {
    final Rectangle b = a1.getBounds();
    b.add(a2.getBounds());
    final AffineTransform translate = new AffineTransform(1, 0, 0, 1, -b.x, -b.y);
    final ShapeList<BitType> shapeList1 = new ShapeListCached<BitType>(new int[] { b.width, b.height }, new BitType(false), 32);
    shapeList1.addShape(a1.createTransformedArea(translate), new BitType(true), new int[] { 0 });
    final Image<BitType> img1 = new Image<BitType>(shapeList1, shapeList1.getBackground(), "ShapeListContainer");
    final ShapeList<BitType> shapeList2 = new ShapeListCached<BitType>(new int[] { b.width, b.height }, new BitType(false), 32);
    shapeList2.addShape(a2.createTransformedArea(translate), new BitType(true), new int[] { 0 });
    final Image<BitType> img2 = new Image<BitType>(shapeList2, shapeList2.getBackground(), "ShapeListContainer");
    final float inc = 1.0f / (nInterpolates + 1);
    final BinaryInterpolation2D interpol = new BinaryInterpolation2D(img1, img2, inc);
    if (!interpol.checkInput()) {
        System.out.println("Error: " + interpol.getErrorMessage());
        return null;
    }
    final Area[] as = new Area[nInterpolates];
    final AffineTransform back = new AffineTransform(1, 0, 0, 1, b.x, b.y);
    // TODO parallelize, which needs the means to call process() in parallel too--currently it cannot,
    // the result would get overwritten.
    final ExecutorService exec = Executors.newFixedThreadPool(Math.min(nInterpolates, Runtime.getRuntime().availableProcessors()));
    final ArrayList<Future<Area>> fus = new ArrayList<Future<Area>>();
    try {
        for (int i = 1; i <= nInterpolates; i++) {
            final float weight = 1 - inc * i;
            fus.add(exec.submit(new Callable<Area>() {

                @Override
                public Area call() throws Exception {
                    final Image<BitType> imb = interpol.process(weight);
                    final ImagePlus imp = ImageJFunctions.copyToImagePlus(imb, ImagePlus.GRAY8);
                    // BitType gets copied to 0 and 255 in 8-bit ByteProcessor
                    final ThresholdToSelection ts = new ThresholdToSelection();
                    ts.setup("", imp);
                    final ImageProcessor ip = imp.getProcessor();
                    ip.setThreshold(1, 255, ImageProcessor.NO_LUT_UPDATE);
                    ts.run(ip);
                    final Roi roi = imp.getRoi();
                    return null == roi ? new Area() : M.getArea(roi).createTransformedArea(back);
                }
            }));
        }
        int i = 0;
        for (final Future<Area> fu : fus) {
            as[i++] = fu.get();
        }
    } catch (final Throwable t) {
        IJError.print(t);
    } finally {
        exec.shutdown();
    }
    return as;
}
Also used : Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) ShapeListCached(mpicbg.imglib.container.shapelist.ShapeListCached) Image(mpicbg.imglib.image.Image) ImagePlus(ij.ImagePlus) Roi(ij.gui.Roi) Callable(java.util.concurrent.Callable) BinaryInterpolation2D(ini.trakem2.imaging.BinaryInterpolation2D) ImageProcessor(ij.process.ImageProcessor) Area(java.awt.geom.Area) BitType(mpicbg.imglib.type.logic.BitType) ExecutorService(java.util.concurrent.ExecutorService) AffineTransform(java.awt.geom.AffineTransform) Future(java.util.concurrent.Future) ThresholdToSelection(ij.plugin.filter.ThresholdToSelection)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)2 BitType (mpicbg.imglib.type.logic.BitType)2 ImagePlus (ij.ImagePlus)1 Roi (ij.gui.Roi)1 ThresholdToSelection (ij.plugin.filter.ThresholdToSelection)1 ImageProcessor (ij.process.ImageProcessor)1 BinaryInterpolation2D (ini.trakem2.imaging.BinaryInterpolation2D)1 Rectangle (java.awt.Rectangle)1 AffineTransform (java.awt.geom.AffineTransform)1 Area (java.awt.geom.Area)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ArrayContainerFactory (mpicbg.imglib.container.array.ArrayContainerFactory)1 ShapeListCached (mpicbg.imglib.container.shapelist.ShapeListCached)1 Image (mpicbg.imglib.image.Image)1 ImageFactory (mpicbg.imglib.image.ImageFactory)1 IntType (mpicbg.imglib.type.numeric.integer.IntType)1