Search in sources :

Example 1 with AreaContainer

use of ini.trakem2.display.AreaContainer 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)

Example 2 with AreaContainer

use of ini.trakem2.display.AreaContainer in project TrakEM2 by trakem2.

the class Segmentation method fastMarching.

public static Bureaucrat fastMarching(final AreaWrapper aw, final Layer layer, final Rectangle srcRect, final int x_p_w, final int y_p_w, final List<Runnable> post_tasks) {
    // 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("Fast marching") {

        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);
            ImagePlus imp = new ImagePlus("", Patch.makeFlatImage(ImagePlus.GRAY8, layer, box, 1.0, (Collection) layer.getDisplayables(Patch.class, new Area(box), true), Color.black));
            // Bandpass filter
            if (fmp.apply_bandpass_filter) {
                IJ.run(imp, "Bandpass Filter...", "filter_large=" + fmp.low_frequency_threshold + " filter_small=" + fmp.high_frequency_threshold + " suppress=None tolerance=5" + (fmp.autoscale_after_filtering ? " autoscale" : "") + (fmp.saturate_when_autoscaling ? " saturate" : ""));
            }
            // Setup seed point
            PointRoi roi = new PointRoi(box.width / 2, box.height / 2);
            imp.setRoi(roi);
            Utils.log2("imp: " + imp);
            Utils.log2("proi: " + imp.getRoi() + "    " + Utils.toString(new int[] { x_p_w - srcRect.x, y_p_w - srcRect.y }));
            // Setup state
            ImageContainer ic = new ImageContainer(imp);
            StateContainer state = new StateContainer();
            state.setROI(roi, ic.getWidth(), ic.getHeight(), ic.getImageCount(), imp.getCurrentSlice());
            state.setExpansionToInside(false);
            // Run FastMarching
            final FastMarching fm = new FastMarching(ic, null, state, true, fmp.fm_grey, fmp.fm_dist, fmp.apply_grey_value_erosion);
            final int max_iterations = fmp.max_iterations;
            final int iter_inc = fmp.iter_inc;
            for (int i = 0; i < max_iterations; i++) {
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
                if (!fm.step(iter_inc))
                    break;
            }
            // Extract ROI
            setTaskName("Adding area");
            final ArrayList<Coordinate> vc = fm.getStateContainer().getXYZ(false);
            if (0 == vc.size()) {
                Utils.log("No area growth.");
                return;
            }
            final Area area = new Area();
            Coordinate first = vc.remove(0);
            final Rectangle r = new Rectangle(first.x, first.y, 1, 1);
            int count = 0;
            // Scan and add line-wise
            for (final Coordinate c : vc) {
                count++;
                if (c.y == r.y && c.x == r.x + 1) {
                    // same line:
                    r.width += 1;
                    continue;
                } else {
                    // add previous one
                    area.add(new Area(r));
                }
                // start new line:
                r.x = c.x;
                r.y = c.y;
                r.width = 1;
                if (0 == count % 1024 && Thread.currentThread().isInterrupted()) {
                    return;
                }
            }
            // add last:
            area.add(new Area(r));
            /*
			// Trying from the image mask: JUST AS SLOW
			final byte[] b = (byte[]) fm.getStateContainer().getIPMask()[0].getPixels();
			final int w = imp.getWidth();
			for (int i=0; i<b.length; i++) {
				if (0 == b[i]) {
					r.x = i%w;
					r.y = i/w;
					area.add(new Area(r));
				}
			}
			*/
            /* // DOESN'T FILL?
			// Trying to just get the contour, and then filling holes
			for (final Coordinate c : fm.getStateContainer().getXYZ(true)) {
				r.x = c.x;
				r.y = c.y;
				area.add(new Area(r));
			}
			Polygon pol = new Polygon();
			final float[] coords = new float[6];
			for (PathIterator pit = area.getPathIterator(null); !pit.isDone(); ) {
				switch (pit.currentSegment(coords)) {
					case PathIterator.SEG_MOVETO:
					case PathIterator.SEG_LINETO:
						pol.addPoint((int)coords[0], (int)coords[1]);
						break;
					case PathIterator.SEG_CLOSE:
						area.add(new Area(pol));
						// prepare next:
						pol = new Polygon();
						break;
					default:
						Utils.log2("WARNING: unhandled seg type.");
						break;
				}
				pit.next();
				if (pit.isDone()) {
					break;
				}
			}
			*/
            // / FAILS because by now AreaWrapper's source is null
            // aw.add(area, new AffineTransform(1, 0, 0, 1, box.x, box.y));
            // Instead, 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;
            }
            aw.getArea().add(area.createTransformedArea(aff));
            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 : FastMarching(levelsets.algorithm.FastMarching) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) Bureaucrat(ini.trakem2.utils.Bureaucrat) ImagePlus(ij.ImagePlus) ImageContainer(levelsets.ij.ImageContainer) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) Area(java.awt.geom.Area) StateContainer(levelsets.ij.StateContainer) Coordinate(levelsets.algorithm.Coordinate) AreaContainer(ini.trakem2.display.AreaContainer) AffineTransform(java.awt.geom.AffineTransform) Worker(ini.trakem2.utils.Worker) PointRoi(ij.gui.PointRoi)

Example 3 with AreaContainer

use of ini.trakem2.display.AreaContainer in project TrakEM2 by trakem2.

the class ProjectThing method measure.

/**
 * Call on children things, and on itself if it contains a basic data type directly.
 *  All children of the same type report to the same table.
 *  Result tables are returned without ever displaying them.
 */
public HashMap<Class<?>, ResultsTable> measure(HashMap<Class<?>, ResultsTable> ht) {
    if (null == ht)
        ht = new HashMap<Class<?>, ResultsTable>();
    if (null != object && object instanceof Displayable) {
        Displayable d = (Displayable) object;
        if (d.isVisible()) {
            ResultsTable rt = d.measure(ht.get(d.getClass()));
            if (null != rt)
                ht.put(d.getClass(), rt);
            // Areas:
            if (object instanceof AreaContainer) {
                ResultsTable rta = ((AreaContainer) object).measureAreas(ht.get(AreaContainer.class));
                if (null != rta)
                    ht.put(AreaContainer.class, rta);
            }
        } else {
            Utils.log("Measure: skipping hidden object " + d.getProject().getMeaningfulTitle(d));
        }
    }
    if (null == al_children)
        return ht;
    // profile list: always special ...
    if (template.getType().equals("profile_list") && null != al_children) {
        synchronized (al_children) {
            if (al_children.size() > 1) {
                Profile[] p = new Profile[al_children.size()];
                for (int i = 0; i < al_children.size(); i++) p[i] = (Profile) al_children.get(i).object;
                ResultsTable rt = Profile.measure(p, ht.get(Profile.class), this.id);
                if (null != rt)
                    ht.put(Profile_List.class, rt);
            // return ht; // don't return: do each profile separately as well
            }
        }
    }
    synchronized (al_children) {
        for (ProjectThing child : al_children) child.measure(ht);
    }
    return ht;
}
Also used : ZDisplayable(ini.trakem2.display.ZDisplayable) Displayable(ini.trakem2.display.Displayable) HashMap(java.util.HashMap) AreaContainer(ini.trakem2.display.AreaContainer) ResultsTable(ij.measure.ResultsTable) Profile(ini.trakem2.display.Profile)

Aggregations

AreaContainer (ini.trakem2.display.AreaContainer)3 Bureaucrat (ini.trakem2.utils.Bureaucrat)2 Worker (ini.trakem2.utils.Worker)2 Rectangle (java.awt.Rectangle)2 AffineTransform (java.awt.geom.AffineTransform)2 Area (java.awt.geom.Area)2 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)2 ImagePlus (ij.ImagePlus)1 PointRoi (ij.gui.PointRoi)1 PolygonRoi (ij.gui.PolygonRoi)1 Wand (ij.gui.Wand)1 ResultsTable (ij.measure.ResultsTable)1 ImageProcessor (ij.process.ImageProcessor)1 Displayable (ini.trakem2.display.Displayable)1 Patch (ini.trakem2.display.Patch)1 Profile (ini.trakem2.display.Profile)1 ZDisplayable (ini.trakem2.display.ZDisplayable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1