Search in sources :

Example 16 with AreaList

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

the class AmiraImporter method extractAreaLists.

/**
 * Returns a map of label vs AreaList.
 */
public static Map<Float, AreaList> extractAreaLists(final ImagePlus imp, final Layer first_layer, final double base_x, final double base_y, final float alpha, final boolean add_background) {
    try {
        final HashMap<Integer, HashMap<Float, Area>> map = new HashMap<Integer, HashMap<Float, Area>>();
        // works even for images that are not stacks: it creates one
        final ImageStack stack = imp.getStack();
        final AtomicInteger ai = new AtomicInteger(1);
        final AtomicInteger completed_slices = new AtomicInteger(0);
        final int n_slices = imp.getNSlices();
        final Thread parent = Thread.currentThread();
        final Thread[] threads = MultiThreading.newThreads();
        for (int ithread = 0; ithread < threads.length; ithread++) {
            threads[ithread] = new Thread() {

                public void run() {
                    final Rectangle box = new Rectangle(0, 0, 1, 1);
                    for (int i = ai.getAndIncrement(); i <= n_slices; i = ai.getAndIncrement()) {
                        final ImageProcessor ip;
                        synchronized (map) {
                            ip = stack.getProcessor(i);
                        }
                        if (parent.isInterrupted())
                            return;
                        final HashMap<Float, Area> layer_map = new HashMap<Float, Area>();
                        synchronized (map) {
                            map.put(i, layer_map);
                        }
                        AreaUtils.extractAreas(ip, layer_map, add_background, box, parent, true);
                        Utils.showProgress(completed_slices.incrementAndGet() / (float) n_slices);
                    }
                }
            };
        }
        MultiThreading.startAndJoin(threads);
        Utils.showProgress(1);
        if (parent.isInterrupted())
            return null;
        final HashMap<Float, AreaList> alis = new HashMap<Float, AreaList>();
        Utils.log2("Recreating arealists...");
        Utils.log2("map.size() = " + map.size());
        final double thickness = first_layer.getThickness();
        final double first_z = first_layer.getZ();
        // Recreate AreaLists
        for (final Map.Entry<Integer, HashMap<Float, Area>> e : map.entrySet()) {
            final int slice_index = e.getKey();
            final HashMap<Float, Area> layer_map = e.getValue();
            for (final Map.Entry<Float, Area> fa : layer_map.entrySet()) {
                Float label = fa.getKey();
                AreaList ali = alis.get(label);
                if (null == ali) {
                    ali = new AreaList(first_layer.getProject(), "Label " + label.intValue(), base_x, base_y);
                    alis.put(label, ali);
                }
                double z = first_z + (slice_index - 1) * thickness;
                Layer layer = first_layer.getParent().getLayer(z, thickness, true);
                ali.setArea(layer.getId(), fa.getValue());
            }
        }
        Utils.log2("Done recreating.");
        first_layer.getParent().addAll(alis.values());
        Utils.log2("Done adding all to LayerSet");
        float hue = 0;
        for (final Map.Entry<Float, AreaList> e : alis.entrySet()) {
            final AreaList ali = e.getValue();
            ali.setProperty("label", Integer.toString(e.getKey().intValue()));
            ali.calculateBoundingBox(null);
            ali.setColor(Color.getHSBColor(hue, 1, 1));
            ali.setAlpha(alpha);
            // golden angle
            hue += 0.38197f;
            if (hue > 1)
                hue = hue - 1;
        }
        Utils.log2("Done setting properties");
        return alis;
    } catch (Exception e) {
        IJError.print(e);
    }
    return null;
}
Also used : ImageStack(ij.ImageStack) HashMap(java.util.HashMap) Rectangle(java.awt.Rectangle) AreaList(ini.trakem2.display.AreaList) Layer(ini.trakem2.display.Layer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ImageProcessor(ij.process.ImageProcessor) Area(java.awt.geom.Area) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with AreaList

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

the class AmiraImporter method extractAmiraLabels.

/**
 * Returns an ArrayList containing all AreaList objects. The xo,yo is the pivot of reference.
 */
public static Collection<AreaList> extractAmiraLabels(final ImagePlus labels, final AmiraParameters ap, final Layer first_layer, final double xo, final double yo) {
    final String[] materials = ap.getMaterialList();
    // extract labels as ArrayList of Area
    final Map<Float, AreaList> alis = extractAreaLists(labels, first_layer, xo, yo, 0.4f, false);
    for (int i = 0; i < materials.length; i++) {
        final int id = ap.getMaterialID(materials[i]);
        final double[] color = ap.getMaterialColor(id);
        final String name = ap.getMaterialName(id);
        if (name.equals("Exterior")) {
            Utils.log("Ignoring Amira's \"Exterior\" label");
            continue;
        }
        final AreaList ali = alis.get(new Float(id));
        if (null == ali) {
            Utils.log("ERROR: no AreaList for label id " + id);
            continue;
        }
        ali.setColor(new Color((float) color[0], (float) color[1], (float) color[2]));
        ali.setTitle(name);
    }
    return alis.values();
}
Also used : Color(java.awt.Color) AreaList(ini.trakem2.display.AreaList)

Aggregations

AreaList (ini.trakem2.display.AreaList)7 Area (java.awt.geom.Area)7 ImagePlus (ij.ImagePlus)6 Color (java.awt.Color)6 Point (java.awt.Point)6 ArrayList (java.util.ArrayList)6 GenericDialog (ij.gui.GenericDialog)5 Worker (ini.trakem2.utils.Worker)5 Rectangle (java.awt.Rectangle)5 ImageStack (ij.ImageStack)4 ShapeRoi (ij.gui.ShapeRoi)4 AffineTransform (java.awt.geom.AffineTransform)4 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)4 HashMap (java.util.HashMap)4 PolygonRoi (ij.gui.PolygonRoi)3 Roi (ij.gui.Roi)3 Layer (ini.trakem2.display.Layer)3 USHORTPaint (ini.trakem2.display.paint.USHORTPaint)3 File (java.io.File)3 HashSet (java.util.HashSet)3