Search in sources :

Example 1 with Layer

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

the class DBLoader method importStackAsPatches.

/**
 * Returns the last Patch.
 */
protected Patch importStackAsPatches(final Project project, final Layer first_layer, final double x, final double y, final ImagePlus imp_stack, final boolean as_copy, final String filepath) {
    double pos_x = Double.MAX_VALUE != x ? x : first_layer.getLayerWidth() / 2 - imp_stack.getWidth() / 2;
    double pos_y = Double.MAX_VALUE != y ? y : first_layer.getLayerHeight() / 2 - imp_stack.getHeight() / 2;
    final double thickness = first_layer.getThickness();
    final String title = Utils.removeExtension(imp_stack.getTitle()).replace(' ', '_');
    Utils.showProgress(0);
    Patch previous_patch = null;
    final int n = imp_stack.getStackSize();
    for (int i = 1; i <= n; i++) {
        Layer layer = first_layer;
        double z = first_layer.getZ() + (i - 1) * thickness;
        // will create new layer if not found
        if (i > 1)
            layer = first_layer.getParent().getLayer(z, thickness, true);
        if (null == layer) {
            Utils.log("Display.importStack: could not create new layers.");
            return null;
        }
        ImageProcessor ip = imp_stack.getStack().getProcessor(i);
        if (as_copy)
            ip = ip.duplicate();
        ImagePlus imp_patch_i = new ImagePlus(title + "__slice=" + i, ip);
        String label = imp_stack.getStack().getSliceLabel(i);
        if (null == label)
            label = "";
        Patch patch = new Patch(project, label + " " + title + " " + i, pos_x, pos_y, imp_patch_i);
        layer.add(patch);
        if (null != previous_patch)
            patch.link(previous_patch);
        previous_patch = patch;
        Utils.showProgress(i * (1.0 / n));
    }
    Utils.showProgress(1.0);
    // return the last Patch
    return previous_patch;
}
Also used : ImageProcessor(ij.process.ImageProcessor) Patch(ini.trakem2.display.Patch) Layer(ini.trakem2.display.Layer) ImagePlus(ij.ImagePlus) Point(java.awt.Point) PGpoint(org.postgresql.geometric.PGpoint)

Example 2 with Layer

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

the class DBLoader method unpackLayers.

/**
 * Recursive. Place all Layer (but not LayerSet) objects with a key as Long(id).
 */
private void unpackLayers(LayerThing root, HashMap hs) {
    Object ob = root.getObject();
    if (ob instanceof Layer)
        hs.put(new Long(((DBObject) ob).getId()), ob);
    if (null == root.getChildren())
        return;
    Iterator it = root.getChildren().iterator();
    while (it.hasNext()) {
        LayerThing child = (LayerThing) it.next();
        unpackLayers(child, hs);
    }
}
Also used : LayerThing(ini.trakem2.tree.LayerThing) Iterator(java.util.Iterator) Layer(ini.trakem2.display.Layer)

Example 3 with Layer

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

the class DBLoader method getLayerThing.

private LayerThing getLayerThing(ResultSet r, Project project, HashMap hs_pt, TemplateThing layer_set_tt, TemplateThing layer_tt) throws Exception {
    long id = r.getLong("id");
    String type = r.getString("type");
    // if not a "Layer", then it's a "Layer Set"
    TemplateThing template = type.equals("layer_set") ? layer_set_tt : layer_tt;
    // HERE the order of the arguments layer_set_tt and layer_tt was inverted, and it worked??? There was a compensating bug, incredibly enough, in the type.equals(.. above.
    return new LayerThing(template, project, id, r.getString("title"), getLayerThingObject(project, r.getLong("object_id"), template, hs_pt), getChildrenLayerThing(project, id, hs_pt, layer_set_tt, layer_tt));
}
Also used : LayerThing(ini.trakem2.tree.LayerThing) TemplateThing(ini.trakem2.tree.TemplateThing)

Example 4 with Layer

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

the class AmiraImporter method importAmiraLabels.

/**
 * Returns the array of AreaList or null if the file dialog is canceled. The xo,yo is the pivot of reference.
 */
public static Collection<AreaList> importAmiraLabels(Layer first_layer, double xo, double yo, final String default_dir) {
    // open file
    OpenDialog od = new OpenDialog("Choose Amira Labels File", default_dir, "");
    String filename = od.getFileName();
    if (null == filename || 0 == filename.length())
        return null;
    String dir = od.getDirectory();
    if (IJ.isWindows())
        dir = dir.replace('\\', '/');
    if (!dir.endsWith("/"))
        dir += "/";
    String path = dir + filename;
    AmiraMeshDecoder dec = new AmiraMeshDecoder();
    if (!dec.open(path)) {
        YesNoDialog yn = new YesNoDialog("Error", "File was not an Amira labels file.\nChoose another one?");
        if (yn.yesPressed())
            return importAmiraLabels(first_layer, xo, yo, default_dir);
        return null;
    }
    ImagePlus imp = null;
    if (dec.isTable()) {
        Utils.showMessage("Select the other file (the labels)!");
        return null;
    } else {
        FileInfo fi = new FileInfo();
        fi.fileName = filename;
        fi.directory = dir;
        imp = new ImagePlus("Amira", dec.getStack());
        dec.parameters.setParameters(imp);
    }
    return extractAmiraLabels(imp, dec.parameters, first_layer, xo, yo);
}
Also used : AmiraMeshDecoder(amira.AmiraMeshDecoder) FileInfo(ij.io.FileInfo) YesNoDialog(ini.trakem2.display.YesNoDialog) ImagePlus(ij.ImagePlus) OpenDialog(ij.io.OpenDialog)

Example 5 with Layer

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

Layer (ini.trakem2.display.Layer)61 ArrayList (java.util.ArrayList)52 Patch (ini.trakem2.display.Patch)43 Rectangle (java.awt.Rectangle)34 HashSet (java.util.HashSet)27 ImagePlus (ij.ImagePlus)26 Displayable (ini.trakem2.display.Displayable)25 AffineTransform (java.awt.geom.AffineTransform)23 GenericDialog (ij.gui.GenericDialog)22 Worker (ini.trakem2.utils.Worker)22 Point (mpicbg.models.Point)22 HashMap (java.util.HashMap)21 Point (java.awt.Point)19 Area (java.awt.geom.Area)19 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)18 File (java.io.File)16 Future (java.util.concurrent.Future)16 LayerSet (ini.trakem2.display.LayerSet)15 ExecutorService (java.util.concurrent.ExecutorService)14 Collection (java.util.Collection)13