Search in sources :

Example 1 with BinaryInterpolation2D

use of ini.trakem2.imaging.BinaryInterpolation2D 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)

Example 2 with BinaryInterpolation2D

use of ini.trakem2.imaging.BinaryInterpolation2D in project TrakEM2 by trakem2.

the class AreaList method interpolate.

/**
 * Interpolate areas between the given first and last layers,
 * both of which must contain an area.
 *
 * @return false if the interpolation could not be done.
 * @throws Exception
 */
public boolean interpolate(final Layer first, final Layer last, final boolean always_use_distance_map) throws Exception {
    int i1 = layer_set.indexOf(first);
    int i2 = layer_set.indexOf(last);
    if (i1 > i2) {
        final int tmp = i1;
        i1 = i2;
        i2 = tmp;
    }
    final List<Layer> range = layer_set.getLayers().subList(i1, i2 + 1);
    Area start = null;
    int istart = 0;
    int inext = -1;
    final HashSet<Layer> touched = new HashSet<Layer>();
    for (final Layer la : range) {
        inext++;
        final Area next = getArea(la);
        if (null == next || next.isEmpty())
            continue;
        if (null == start || 0 == inext - istart - 1) {
            // skip for first area or for no space in between
            start = next;
            istart = inext;
            continue;
        }
        // Interpolate from start to next
        Area[] as = always_use_distance_map ? null : AreaUtils.singularInterpolation(start, next, inext - istart - 1);
        if (null == as) {
            // NOT SINGULAR: must use BinaryInterpolation2D
            as = AreaUtils.manyToManyInterpolation(start, next, inext - istart - 1);
        }
        if (null != as) {
            for (int k = 0; k < as.length; k++) {
                final Layer la2 = range.get(k + istart + 1);
                addArea(la2.getId(), as[k]);
                touched.add(la2);
            }
        }
        // Prepare next interval
        start = next;
        istart = inext;
    }
    for (final Layer la : touched) calculateBoundingBox(la);
    return true;
}
Also used : Area(java.awt.geom.Area) Point(java.awt.Point) USHORTPaint(ini.trakem2.display.paint.USHORTPaint) HashSet(java.util.HashSet)

Aggregations

Area (java.awt.geom.Area)2 ImagePlus (ij.ImagePlus)1 Roi (ij.gui.Roi)1 ThresholdToSelection (ij.plugin.filter.ThresholdToSelection)1 ImageProcessor (ij.process.ImageProcessor)1 USHORTPaint (ini.trakem2.display.paint.USHORTPaint)1 BinaryInterpolation2D (ini.trakem2.imaging.BinaryInterpolation2D)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1 AffineTransform (java.awt.geom.AffineTransform)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ShapeListCached (mpicbg.imglib.container.shapelist.ShapeListCached)1 Image (mpicbg.imglib.image.Image)1 BitType (mpicbg.imglib.type.logic.BitType)1