Search in sources :

Example 1 with Wand

use of ij.gui.Wand in project TrakEM2 by trakem2.

the class Segmentation method magicWand.

public static Bureaucrat magicWand(final AreaWrapper aw, final Layer layer, final Rectangle srcRect, final int x_p_w, final int y_p_w, final List<Runnable> post_tasks, final boolean inverse, final boolean subtract) {
    // Capture pointers before they are set to null
    final AreaContainer ac = (AreaContainer) aw.getSource();
    final AffineTransform source_aff = aw.getSource().getAffineTransform();
    final Rectangle box = new Rectangle(x_p_w - Segmentation.fmp.width / 2, y_p_w - Segmentation.fmp.height / 2, Segmentation.fmp.width, Segmentation.fmp.height);
    Bureaucrat burro = Bureaucrat.create(new Worker.Task("Magic Wand") {

        public void exec() {
            // Capture image as large as the fmp width,height centered on x_p_w,y_p_w
            Utils.log2("fmp box is " + box);
            ImageProcessor ip = Patch.makeFlatImage(ImagePlus.GRAY8, layer, box, 1.0, (Collection) layer.getDisplayables(Patch.class, new Area(box), true), Color.black);
            // Apply wand
            Wand wand = new Wand(ip);
            String smode = WandToolOptions.getMode();
            int mode = Wand.LEGACY_MODE;
            if (null == smode) {
            } else if (smode.startsWith("4"))
                mode = Wand.FOUR_CONNECTED;
            else if (smode.startsWith("8"))
                mode = Wand.EIGHT_CONNECTED;
            // 8-bit image
            wand.autoOutline(ip.getWidth() / 2, ip.getHeight() / 2, WandToolOptions.getTolerance(), mode);
            if (wand.npoints > 0) {
                Area area = M.getArea(new PolygonRoi(wand.xpoints, wand.ypoints, wand.npoints, PolygonRoi.FREEROI));
                if (inverse) {
                    Area b = new Area(new Rectangle(0, 0, box.width, box.height));
                    b.subtract(area);
                    area = b;
                }
                // Compose an Area that is local to the AreaWrapper's area
                final AffineTransform aff = new AffineTransform(1, 0, 0, 1, box.x, box.y);
                try {
                    aff.preConcatenate(source_aff.createInverse());
                } catch (NoninvertibleTransformException nite) {
                    IJError.print(nite);
                    return;
                }
                area.transform(aff);
                if (subtract)
                    aw.getArea().subtract(area);
                else
                    aw.getArea().add(area);
                ac.calculateBoundingBox(layer);
                Display.repaint(layer);
            }
        }
    }, layer.getProject());
    if (null != post_tasks)
        for (Runnable task : post_tasks) burro.addPostTask(task);
    burro.goHaveBreakfast();
    return burro;
}
Also used : Rectangle(java.awt.Rectangle) Wand(ij.gui.Wand) Bureaucrat(ini.trakem2.utils.Bureaucrat) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) ImageProcessor(ij.process.ImageProcessor) PolygonRoi(ij.gui.PolygonRoi) Area(java.awt.geom.Area) AreaContainer(ini.trakem2.display.AreaContainer) AffineTransform(java.awt.geom.AffineTransform) Worker(ini.trakem2.utils.Worker) Collection(java.util.Collection) Patch(ini.trakem2.display.Patch)

Aggregations

PolygonRoi (ij.gui.PolygonRoi)1 Wand (ij.gui.Wand)1 ImageProcessor (ij.process.ImageProcessor)1 AreaContainer (ini.trakem2.display.AreaContainer)1 Patch (ini.trakem2.display.Patch)1 Bureaucrat (ini.trakem2.utils.Bureaucrat)1 Worker (ini.trakem2.utils.Worker)1 Rectangle (java.awt.Rectangle)1 AffineTransform (java.awt.geom.AffineTransform)1 Area (java.awt.geom.Area)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 Collection (java.util.Collection)1