Search in sources :

Example 6 with ThresholdToSelection

use of ij.plugin.filter.ThresholdToSelection in project imagej1 by imagej.

the class BinaryInterpolator method run.

public void run(ImagePlus image, Roi[] rois) {
    w = image.getWidth();
    h = image.getHeight();
    ImageStack stack = new ImageStack(w, h);
    int firstIndex = -1, lastIndex = -1;
    for (int i = 1; i < rois.length; i++) {
        if (rois[i] != null) {
            firstIndex = (firstIndex == -1) ? i : firstIndex;
            lastIndex = i;
        }
    }
    if (firstIndex == -1) {
        IJ.error("There must be at least one selection in order to interpolate.");
        return;
    }
    for (int i = firstIndex; i <= lastIndex; i++) {
        ByteProcessor bp = new ByteProcessor(w, h);
        if (rois[i] != null) {
            bp.copyBits(rois[i].getMask(), rois[i].getBounds().x, rois[i].getBounds().y, ij.process.Blitter.ADD);
        }
        stack.addSlice("", bp);
    }
    run(stack);
    ImagePlus roiImage = new ImagePlus("bla", stack);
    ThresholdToSelection ts = new ThresholdToSelection();
    ts.setup("", roiImage);
    for (int i = firstIndex; i <= lastIndex; i++) {
        ImageProcessor bp = stack.getProcessor(1);
        stack.deleteSlice(1);
        int threshold = 255;
        bp.setThreshold(threshold, threshold, ImageProcessor.NO_LUT_UPDATE);
        ts.run(bp);
        rois[i] = roiImage.getRoi();
    }
}
Also used : ImageStack(ij.ImageStack) ThresholdToSelection(ij.plugin.filter.ThresholdToSelection) ImagePlus(ij.ImagePlus)

Example 7 with ThresholdToSelection

use of ij.plugin.filter.ThresholdToSelection 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 8 with ThresholdToSelection

use of ij.plugin.filter.ThresholdToSelection in project mcib3d-core by mcib3d.

the class Object3DVoxels method createRoi.

/**
 * @param z Description of the Parameter
 * @return Description of the Return Value
 * @deprecated use Object3D-IJUtils
 * Constructor for the createRoi object
 */
@Override
public Roi createRoi(int z) {
    // IJ.write("create roi " + z);
    int sx = this.getXmax() - this.getXmin() + 1;
    int sy = this.getYmax() - this.getYmin() + 1;
    ByteProcessor mask = new ByteProcessor(sx, sy);
    // object black on white
    // mask.invert();
    draw(mask, z, 255);
    ImagePlus maskPlus = new ImagePlus("mask " + z, mask);
    // maskPlus.show();
    // IJ.run("Create Selection");
    ThresholdToSelection tts = new ThresholdToSelection();
    tts.setup("", maskPlus);
    tts.run(mask);
    maskPlus.updateAndDraw();
    // IJ.write("sel=" + maskPlus.getRoi());
    // maskPlus.hide();
    Roi roi = maskPlus.getRoi();
    Rectangle rect = roi.getBounds();
    rect.x += this.getXmin();
    rect.y += this.getYmin();
    return roi;
}
Also used : ByteProcessor(ij.process.ByteProcessor) ThresholdToSelection(ij.plugin.filter.ThresholdToSelection) ImagePlus(ij.ImagePlus) Roi(ij.gui.Roi)

Example 9 with ThresholdToSelection

use of ij.plugin.filter.ThresholdToSelection in project mcib3d-core by mcib3d.

the class Object3D_IJUtils method createRoi.

public static Roi createRoi(Object3D object3D, int z) {
    // IJ.write("create roi " + z);
    int sx = object3D.getXmax() - object3D.getXmin() + 1;
    int sy = object3D.getYmax() - object3D.getYmin() + 1;
    ByteProcessor mask = new ByteProcessor(sx, sy);
    // object black on white
    // mask.invert();
    draw(object3D, mask, z, 255);
    ImagePlus maskPlus = new ImagePlus("mask " + z, mask);
    // maskPlus.show();
    // IJ.run("Create Selection");
    ThresholdToSelection tts = new ThresholdToSelection();
    tts.setup("", maskPlus);
    tts.run(mask);
    maskPlus.updateAndDraw();
    // IJ.write("sel=" + maskPlus.getRoi());
    // maskPlus.hide();
    Roi roi = maskPlus.getRoi();
    Rectangle rect = roi.getBounds();
    rect.x += object3D.getXmin();
    rect.y += object3D.getYmin();
    return roi;
}
Also used : ByteProcessor(ij.process.ByteProcessor) ThresholdToSelection(ij.plugin.filter.ThresholdToSelection) ImagePlus(ij.ImagePlus) Roi(ij.gui.Roi)

Example 10 with ThresholdToSelection

use of ij.plugin.filter.ThresholdToSelection in project TrakEM2 by trakem2.

the class Patch method getArea.

/**
 * Returns an Area in world coords representing the inside of this Patch. The fully alpha pixels are considered outside.
 */
@Override
public Area getArea() {
    CoordinateTransform ct = null;
    if (hasAlphaMask()) {
        // Read the mask as a ROI for the 0 pixels only and apply the AffineTransform to it:
        ImageProcessor alpha_mask = getAlphaMask();
        if (null == alpha_mask) {
            Utils.log2("Could not retrieve alpha mask for " + this);
        } else {
            if (hasCoordinateTransform()) {
                // must transform it
                ct = getCoordinateTransform();
                final TransformMesh mesh = new TransformMesh(ct, meshResolution, o_width, o_height);
                final TransformMeshMapping mapping = new TransformMeshMapping(mesh);
                // Without interpolation
                alpha_mask = mapping.createMappedImage(alpha_mask);
            // Keep in mind the affine of the Patch already contains the translation specified by the mesh bounds.
            }
            // Threshold all non-zero areas of the mask:
            alpha_mask.setThreshold(1, 255, ImageProcessor.NO_LUT_UPDATE);
            final ImagePlus imp = new ImagePlus("", alpha_mask);
            // TODO replace by our much faster method that scans by line, in AmiraImporter
            final ThresholdToSelection tts = new ThresholdToSelection();
            tts.setup("", imp);
            tts.run(alpha_mask);
            final Roi roi = imp.getRoi();
            if (null == roi) {
                // All pixels in the alpha mask have a value of zero
                return new Area();
            }
            return M.getArea(roi).createTransformedArea(this.at);
        }
    }
    // No alpha mask, or error in retrieving it:
    final int[] x = new int[o_width + o_width + o_height + o_height];
    final int[] y = new int[x.length];
    int next = 0;
    // Top edge:
    for (int i = 0; i <= o_width; i++, next++) {
        // len: o_width + 1
        x[next] = i;
        y[next] = 0;
    }
    // Right edge:
    for (int i = 1; i <= o_height; i++, next++) {
        // len: o_height
        x[next] = o_width;
        y[next] = i;
    }
    // bottom edge:
    for (int i = o_width - 1; i > -1; i--, next++) {
        // len: o_width
        x[next] = i;
        y[next] = o_height;
    }
    // left edge:
    for (int i = o_height - 1; i > 0; i--, next++) {
        // len: o_height -1
        x[next] = 0;
        y[next] = i;
    }
    if (hasCoordinateTransform() && null == ct)
        ct = getCoordinateTransform();
    if (null != ct) {
        final CoordinateTransformList<CoordinateTransform> t = new CoordinateTransformList<CoordinateTransform>();
        t.add(ct);
        final TransformMesh mesh = new TransformMesh(ct, meshResolution, o_width, o_height);
        final Rectangle box = mesh.getBoundingBox();
        final AffineTransform aff = new AffineTransform(this.at);
        // Must correct for the inverse of the mesh translation, because the affine also includes the translation.
        aff.translate(-box.x, -box.y);
        final AffineModel2D affm = new AffineModel2D();
        affm.set(aff);
        t.add(affm);
        /*
			 * WORKS FINE, but for points that fall outside the mesh, they don't get transformed!
			// Do it like Patch does it to generate the mipmap, with a mesh (and all the imprecisions of a mesh):
			final CoordinateTransformList t = new CoordinateTransformList();
			final TransformMesh mesh = new TransformMesh(this.ct, meshResolution, o_width, o_height);
			final AffineTransform aff = new AffineTransform(this.at);
			t.add(mesh);
			final AffineModel2D affm = new AffineModel2D();
			affm.set(aff);
			t.add(affm);
			*/
        final double[] f = new double[] { x[0], y[0] };
        t.applyInPlace(f);
        final Path2D.Float path = new Path2D.Float(Path2D.Float.WIND_EVEN_ODD, x.length + 1);
        path.moveTo(f[0], f[1]);
        for (int i = 1; i < x.length; i++) {
            f[0] = x[i];
            f[1] = y[i];
            t.applyInPlace(f);
            path.lineTo(f[0], f[1]);
        }
        // line to last call to moveTo
        path.closePath();
        return new Area(path);
    } else {
        return new Area(new Polygon(x, y, x.length)).createTransformedArea(this.at);
    }
}
Also used : TransformMeshMapping(mpicbg.trakem2.transform.TransformMeshMapping) CoordinateTransformList(mpicbg.trakem2.transform.CoordinateTransformList) Path2D(java.awt.geom.Path2D) Rectangle(java.awt.Rectangle) ImagePlus(ij.ImagePlus) ShapeRoi(ij.gui.ShapeRoi) Roi(ij.gui.Roi) ImageProcessor(ij.process.ImageProcessor) Area(java.awt.geom.Area) AffineModel2D(mpicbg.trakem2.transform.AffineModel2D) AffineTransform(java.awt.geom.AffineTransform) ThresholdToSelection(ij.plugin.filter.ThresholdToSelection) Polygon(java.awt.Polygon) CoordinateTransform(mpicbg.trakem2.transform.CoordinateTransform) TransformMesh(mpicbg.trakem2.transform.TransformMesh) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh)

Aggregations

ThresholdToSelection (ij.plugin.filter.ThresholdToSelection)10 ImagePlus (ij.ImagePlus)8 Roi (ij.gui.Roi)6 ByteProcessor (ij.process.ByteProcessor)4 Rectangle (java.awt.Rectangle)4 ImageStack (ij.ImageStack)2 EDM (ij.plugin.filter.EDM)2 ImageProcessor (ij.process.ImageProcessor)2 AffineTransform (java.awt.geom.AffineTransform)2 Area (java.awt.geom.Area)2 ArrayList (java.util.ArrayList)2 ShapeRoi (ij.gui.ShapeRoi)1 RoiManager (ij.plugin.frame.RoiManager)1 BinaryInterpolation2D (ini.trakem2.imaging.BinaryInterpolation2D)1 Polygon (java.awt.Polygon)1 Path2D (java.awt.geom.Path2D)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