Search in sources :

Example 1 with FastMarching

use of levelsets.algorithm.FastMarching 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)

Aggregations

ImagePlus (ij.ImagePlus)1 PointRoi (ij.gui.PointRoi)1 AreaContainer (ini.trakem2.display.AreaContainer)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 ArrayList (java.util.ArrayList)1 Coordinate (levelsets.algorithm.Coordinate)1 FastMarching (levelsets.algorithm.FastMarching)1 ImageContainer (levelsets.ij.ImageContainer)1 StateContainer (levelsets.ij.StateContainer)1