Search in sources :

Example 26 with LayerSet

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

the class Compare method variabilityAnalysis.

/**
 * @param reference_project If null, then the first one found in the Project.getProjects() lists is used.
 * @param regex A String (can be null) to filter objects by, to limit what gets processed.
 *              If regex is not null, then only ProjectThing nodes with the matching regex are analyzed (shallow: none of their children are questioned, but pipes will be built from them all).
 * @param generate_plots Whether to generate the variability plots at all.
 * @param show_plots If generate_plots, whether to show the plots in a stack image window or to save them.
 * @param show_3D Whether to show any 3D data.
 * @param show_condensed_3D If show_3D, whether to show the condensed vector strings, i.e. the "average" pipes.
 * @param show_sources_3D If show_3D, whether to show the source pipes from which the condensed vector string was generated.
 * @param sources_color_table Which colors to give to the pipes of which Project.
 * @param show_envelope_3D If show_3D, whether to generate the variability envelope.
 * @param envelope_alpha If show_envelope_3D, the envelope takes an alpha value between 0 (total transparency) and 1 (total opacity)
 * @param delta_envelope The delta to resample the envelope to. When smaller than or equal to 1, no envelope resampling occurs.
 * @param show_axes_3D If show_3D, whether to display the reference axes as well.
 * @param heat_map If show_3D, whether to color the variability with a Fire LUT.
 *                 If not show_condensed_3D, then the variability is shown in color-coded 3D spheres placed at the entry point to the neuropile.
 * @param map_condensed If not null, all VectorString3D are put into this map.
 * @param projects The projects to use.
 */
public static Bureaucrat variabilityAnalysis(final Project reference_project, final String regex, final String[] ignore, final boolean show_cata_dialog, final boolean generate_plots, final boolean show_plots, final String plot_dir_, final boolean show_3D, final boolean show_condensed_3D, final boolean show_sources_3D, final Map<Project, Color> sources_color_table, final boolean show_envelope_3D, final float envelope_alpha, final double delta_envelope, final int envelope_type, final boolean show_axes_3D, final boolean heat_map, final Map<String, VectorString3D> map_condensed, final Project[] projects) {
    // gather all open projects
    final Project[] p = null == projects ? Project.getProjects().toArray(new Project[0]) : projects;
    // make the reference_project be the first in the array
    if (null != reference_project && reference_project != p[0]) {
        for (int i = 0; i < p.length; i++) {
            if (reference_project == p[i]) {
                p[i] = p[0];
                p[0] = reference_project;
                break;
            }
        }
    }
    final Worker worker = new Worker("Comparing all to all") {

        @Override
        public void run() {
            startedWorking();
            try {
                Utils.log2("Asking for CATAParameters...");
                final CATAParameters cp = new CATAParameters();
                cp.regex = regex;
                cp.delta_envelope = delta_envelope;
                cp.envelope_type = envelope_type;
                if (show_cata_dialog && !cp.setup(false, regex, true, true)) {
                    finishedWorking();
                    return;
                }
                // so source points are stored in VectorString3D for each resampled and interpolated point
                cp.with_source = true;
                // Store a series of results, depending on options
                final HashMap<String, Display3D> results = new HashMap<String, Display3D>();
                String plot_dir = plot_dir_;
                if (generate_plots && !show_plots) {
                    // Save plots
                    if (null == plot_dir) {
                        final DirectoryChooser dc = new DirectoryChooser("Choose plots directory");
                        plot_dir = dc.getDirectory();
                        if (null == plot_dir) {
                            finishedWorking();
                            return;
                        }
                    }
                    if (IJ.isWindows())
                        plot_dir = plot_dir.replace('\\', '/');
                    if (!plot_dir.endsWith("/"))
                        plot_dir += "/";
                }
                Utils.log2("Gathering chains...");
                // Gather chains that do not match the ignore regexes
                // will transform them as well to the reference found in the first project in the p array
                Object[] ob = gatherChains(p, cp, ignore);
                ArrayList<Chain> chains = (ArrayList<Chain>) ob[0];
                // to keep track of each project's chains
                final ArrayList[] p_chains = (ArrayList[]) ob[1];
                ob = null;
                if (null == chains) {
                    finishedWorking();
                    return;
                }
                Utils.log2("Collecting bundles...");
                final HashMap<Project, HashMap<String, VectorString3D>> axes = new HashMap<Project, HashMap<String, VectorString3D>>();
                // Sort out into groups by unique names of lineage bundles
                final HashMap<String, ArrayList<Chain>> bundles = new HashMap<String, ArrayList<Chain>>();
                for (final Chain chain : chains) {
                    String title = chain.getCellTitle();
                    final String t = title.toLowerCase();
                    // unnamed
                    if (0 == t.indexOf('[') || 0 == t.indexOf('#'))
                        continue;
                    Utils.log("Accepting " + title);
                    title = title.substring(0, title.indexOf(' '));
                    // lineage bundle instance chains
                    ArrayList<Chain> bc = bundles.get(title);
                    if (null == bc) {
                        bc = new ArrayList<Chain>();
                        bundles.put(title, bc);
                    }
                    bc.add(chain);
                }
                Utils.log2("Found " + bundles.size() + " bundles.");
                chains = null;
                if (null != cp.regex && show_axes_3D && axes.size() < 3) {
                    // Must find the Mushroom Body lobes separately
                    final String cp_regex = cp.regex;
                    cp.regex = "mb";
                    final Object[] o = gatherChains(p, cp, ignore);
                    final ArrayList<Chain> lobes = (ArrayList<Chain>) o[0];
                    Utils.logAll("Found " + lobes.size() + " chains for lobes");
                    for (final Chain chain : lobes) {
                        final String t = chain.getCellTitle().toLowerCase();
                        if (-1 != t.indexOf("peduncle") || -1 != t.indexOf("medial lobe") || -1 != t.indexOf("dorsal lobe")) {
                            Utils.logAll("adding " + t);
                            final Project pr = chain.pipes.get(0).getProject();
                            HashMap<String, VectorString3D> m = axes.get(pr);
                            if (null == m) {
                                m = new HashMap<String, VectorString3D>();
                                axes.put(pr, m);
                            }
                            m.put(t, chain.vs);
                            continue;
                        }
                    }
                    cp.regex = cp_regex;
                } else {
                    Utils.logAll("Not: cp.regex = " + cp.regex + "  show_axes_3D = " + show_axes_3D + "  axes.size() = " + axes.size());
                }
                final HashMap<String, VectorString3D> condensed = new HashMap<String, VectorString3D>();
                Utils.log2("Condensing each bundle...");
                // Condense each into a single VectorString3D
                for (final Map.Entry<String, ArrayList<Chain>> entry : bundles.entrySet()) {
                    final ArrayList<Chain> bc = entry.getValue();
                    if (bc.size() < 2) {
                        Utils.log2("Skipping single: " + entry.getKey());
                        continue;
                    }
                    final VectorString3D[] vs = new VectorString3D[bc.size()];
                    for (int i = 0; i < vs.length; i++) vs[i] = bc.get(i).vs;
                    final VectorString3D c = condense(cp, vs, this);
                    c.setCalibration(p[0].getRootLayerSet().getCalibrationCopy());
                    condensed.put(entry.getKey(), c);
                    if (this.hasQuitted())
                        return;
                }
                // Store:
                if (null != map_condensed) {
                    map_condensed.putAll(condensed);
                }
                if (generate_plots) {
                    Utils.log2("Plotting stdDev for each condensed bundle...");
                    // Y axis: the stdDev at each point, computed from the group of points that contribute to each
                    for (final Map.Entry<String, VectorString3D> e : condensed.entrySet()) {
                        final String name = e.getKey();
                        final VectorString3D c = e.getValue();
                        final Plot plot = makePlot(cp, name, c);
                        // FAILS//plot.addLabel(10, cp.plot_height-5, name); // must be added after setting size
                        if (show_plots)
                            plot.show();
                        else if (null != plot_dir)
                            new FileSaver(plot.getImagePlus()).saveAsPng(plot_dir + name.replace('/', '-') + ".png");
                    }
                }
                if (show_3D) {
                    final HashMap<String, Color> heat_table = new HashMap<String, Color>();
                    if (heat_map || show_envelope_3D) {
                        // Create a Fire LUT
                        final ImagePlus lutimp = new ImagePlus("lut", new ByteProcessor(4, 4));
                        IJ.run(lutimp, "Fire", "");
                        final IndexColorModel icm = (IndexColorModel) lutimp.getProcessor().getColorModel();
                        final byte[] reds = new byte[256];
                        final byte[] greens = new byte[256];
                        final byte[] blues = new byte[256];
                        icm.getReds(reds);
                        icm.getGreens(greens);
                        icm.getBlues(blues);
                        final List<String> names = new ArrayList<String>(bundles.keySet());
                        Collections.sort(names);
                        // find max stdDev
                        double max = 0;
                        final HashMap<String, Double> heats = new HashMap<String, Double>();
                        for (final String name : names) {
                            final VectorString3D vs_merged = condensed.get(name);
                            if (null == vs_merged) {
                                Utils.logAll("WARNING could not find a condensed pipe for " + name);
                                continue;
                            }
                            final double[] stdDev = vs_merged.getStdDevAtEachPoint();
                            // double avg = 0;
                            // for (int i=0; i<stdDev.length; i++) avg += stdDev[i];
                            // avg = avg/stdDev.length;
                            Arrays.sort(stdDev);
                            // median is more representative than average
                            final double median = stdDev[stdDev.length / 2];
                            if (max < median)
                                max = median;
                            heats.put(name, median);
                        }
                        for (final Map.Entry<String, Double> e : heats.entrySet()) {
                            final String name = e.getKey();
                            final double median = e.getValue();
                            // scale between 0 and max to get a Fire LUT color:
                            int index = (int) ((median / max) * 255);
                            if (index > 255)
                                index = 255;
                            final Color color = new Color(0xff & reds[index], 0xff & greens[index], 0xff & blues[index]);
                            Utils.log2(new StringBuilder(name).append('\t').append(median).append('\t').append(reds[index]).append('\t').append(greens[index]).append('\t').append(blues[index]).toString());
                            heat_table.put(name, color);
                        }
                    }
                    final LayerSet common_ls = new LayerSet(p[0], -1, "Common", 10, 10, 0, 0, 0, 512, 512, false, 2, new AffineTransform());
                    final Display3D d3d = Display3D.get(common_ls);
                    float env_alpha = envelope_alpha;
                    if (env_alpha < 0) {
                        Utils.log2("WARNING envelope_alpha is invalid: " + envelope_alpha + "\n    Using 0.4f instead");
                        env_alpha = 0.4f;
                    } else if (env_alpha > 1)
                        env_alpha = 1.0f;
                    for (final String name : bundles.keySet()) {
                        final ArrayList<Chain> bc = bundles.get(name);
                        final VectorString3D vs_merged = condensed.get(name);
                        if (null == vs_merged) {
                            Utils.logAll("WARNING: could not find a condensed vs for " + name);
                            continue;
                        }
                        if (show_sources_3D) {
                            if (null != sources_color_table) {
                                final HashSet<String> titles = new HashSet<String>();
                                for (final Chain chain : bc) {
                                    final Color c = sources_color_table.get(chain.getRoot().getProject());
                                    final String title = chain.getCellTitle();
                                    String t = title;
                                    int i = 2;
                                    while (titles.contains(t)) {
                                        t = title + "-" + i;
                                        i += 1;
                                    }
                                    titles.add(t);
                                    Display3D.addMesh(common_ls, chain.vs, t, null != c ? c : Color.gray);
                                }
                            } else {
                                for (final Chain chain : bc) Display3D.addMesh(common_ls, chain.vs, chain.getCellTitle(), Color.gray);
                            }
                        }
                        if (show_condensed_3D) {
                            Display3D.addMesh(common_ls, vs_merged, name + "-condensed", heat_map ? heat_table.get(name) : Color.red);
                        }
                        if (show_envelope_3D) {
                            double[] widths = makeEnvelope(cp, vs_merged);
                            if (cp.delta_envelope > 1) {
                                vs_merged.addDependent(widths);
                                vs_merged.resample(cp.delta_envelope);
                                widths = vs_merged.getDependent(0);
                            }
                            Display3D.addMesh(common_ls, vs_merged, name + "-envelope", heat_map ? heat_table.get(name) : Color.red, widths, env_alpha);
                        } else if (heat_map) {
                            // Show spheres in place of envelopes, at the starting tip (neuropile entry point)
                            final double x = vs_merged.getPoints(0)[0];
                            final double y = vs_merged.getPoints(1)[0];
                            final double z = vs_merged.getPoints(2)[0];
                            final double r = 10;
                            final Color color = heat_table.get(name);
                            if (null == color) {
                                Utils.logAll("WARNING: heat table does not have a color for " + name);
                                continue;
                            }
                            final Content sphere = d3d.getUniverse().addMesh(ij3d.Mesh_Maker.createSphere(x, y, z, r), new Color3f(heat_table.get(name)), name + "-sphere", 1);
                        }
                    }
                    if (show_axes_3D) {
                        for (int i = 0; i < p.length; i++) {
                            final Map<String, VectorString3D> m = axes.get(p[i]);
                            if (null == m) {
                                Utils.log2("No axes found for project " + p[i]);
                                continue;
                            }
                            for (final Map.Entry<String, VectorString3D> e : m.entrySet()) {
                                Display3D.addMesh(common_ls, e.getValue(), e.getKey() + "-" + i, Color.gray);
                            }
                        }
                    }
                    results.put("d3d", Display3D.get(common_ls));
                }
                this.result = results;
                Utils.log2("Done.");
            } catch (final Exception e) {
                IJError.print(e);
            } finally {
                finishedWorking();
            }
        }
    };
    return Bureaucrat.createAndStart(worker, p[0]);
}
Also used : CATAParameters(ini.trakem2.analysis.Compare.CATAParameters) ByteProcessor(ij.process.ByteProcessor) HashMap(java.util.HashMap) Color3f(org.scijava.vecmath.Color3f) ArrayList(java.util.ArrayList) FileSaver(ij.io.FileSaver) Worker(ini.trakem2.utils.Worker) IndexColorModel(java.awt.image.IndexColorModel) HashSet(java.util.HashSet) LayerSet(ini.trakem2.display.LayerSet) Display3D(ini.trakem2.display.Display3D) Plot(ij.gui.Plot) Color(java.awt.Color) ImagePlus(ij.ImagePlus) Project(ini.trakem2.Project) VectorString3D(ini.trakem2.vector.VectorString3D) Content(ij3d.Content) AffineTransform(java.awt.geom.AffineTransform) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) DirectoryChooser(ij.io.DirectoryChooser)

Example 27 with LayerSet

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

the class Compare method appendAndFork.

/**
 * Recursive.
 */
private static void appendAndFork(final ProjectThing parent, Chain chain, HashSet<ProjectThing> hs_c_done, final ArrayList<Chain> chains, final LayerSet ls, final Pattern exclude) throws Exception {
    if (null != exclude && exclude.matcher(parent.getTitle()).matches()) {
        Utils.logAll("Excluding node " + parent + " with title " + parent.getTitle() + ", and all its children nodes.");
        return;
    }
    final ArrayList<ProjectThing> children = parent.getChildren();
    if (null == children)
        return;
    if (null == hs_c_done)
        hs_c_done = new HashSet<ProjectThing>();
    for (final ProjectThing child : children) {
        if (hs_c_done.contains(child))
            continue;
        if (null != exclude && exclude.matcher(child.getTitle()).matches()) {
            Utils.log2("Excluding child " + child + " with title " + child.getTitle());
            continue;
        }
        hs_c_done.add(child);
        if (child.getObject() instanceof Line3D) {
            final Line3D pipe = (Line3D) child.getObject();
            // not from the same LayerSet, maybe from a nested one.
            if (!pipe.getLayerSet().equals(ls) || pipe.length() < 2)
                continue;
            if (null == chain) {
                chain = new Chain(pipe);
                chains.add(chain);
            } else {
                chain.append(pipe);
            }
            // find other children in the parent who contain children with child pipes
            boolean first = true;
            final Chain base = chain.duplicate();
            for (final ProjectThing c : children) {
                // already visited
                if (hs_c_done.contains(c))
                    continue;
                // c is at the same tree level as child (which contains a pipe directly)
                final ArrayList<Line3D> child_pipes = c.findChildrenOfType(Line3D.class);
                if (child_pipes.size() > 0) {
                    Chain ca;
                    if (first) {
                        // just append
                        ca = chain;
                        first = false;
                    } else {
                        // otherwise make a copy to branch out
                        // can't duplicate from chain itself, because it would have the previous child added to it.
                        ca = base.duplicate();
                        chains.add(ca);
                    }
                    appendAndFork(c, ca, hs_c_done, chains, ls, exclude);
                }
            }
            // pipe wrapping ProjectThing objects cannot have any children
            continue;
        }
        // if it does not have direct pipe children, cut chain - but keep inspecting
        if (0 == child.findChildrenOfType(Line3D.class).size()) {
            chain = null;
        }
        // inspect others down the unvisited tree nodes
        appendAndFork(child, chain, hs_c_done, chains, ls, exclude);
    }
}
Also used : ProjectThing(ini.trakem2.tree.ProjectThing) Line3D(ini.trakem2.display.Line3D) HashSet(java.util.HashSet)

Example 28 with LayerSet

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

the class AreaList method getStack.

/**
 * Returns a stack of images representing the pixel data of this LayerSet inside this AreaList.
 */
public ImagePlus getStack(final int type, final double scale) {
    final ImageProcessor ref_ip = Utils.createProcessor(type, 2, 2);
    if (null == ref_ip) {
        Utils.log("AreaList.getStack: Unknown type " + type);
        return null;
    }
    final Rectangle b = getBoundingBox();
    final int w = (int) (0.5 + b.width * scale);
    final int h = (int) (0.5 + b.height * scale);
    final ImageStack stack = new ImageStack(w, h);
    for (final Layer la : getLayerRange()) {
        final Area area = getArea(la);
        final double z = layer.getZ();
        project.getLoader().releaseToFit(w * h * 10);
        final ImageProcessor ip = ref_ip.createProcessor(w, h);
        if (null == area) {
            stack.addSlice(Double.toString(z), ip);
            continue;
        }
        // Create a ROI from the area at Layer la:
        final AffineTransform aff = getAffineTransformCopy();
        aff.translate(-b.x, -b.y);
        aff.scale(scale, scale);
        final ShapeRoi roi = new ShapeRoi(area.createTransformedArea(aff));
        // Create a cropped snapshot of the images at Layer la under the area:
        final ImageProcessor flat = Patch.makeFlatImage(type, la, b, scale, la.getAll(Patch.class), Color.black);
        flat.setRoi(roi);
        final Rectangle rb = roi.getBounds();
        ip.insert(flat.crop(), rb.x, rb.y);
        // Clear the outside
        final ImagePlus bimp = new ImagePlus("", ip);
        bimp.setRoi(roi);
        ip.setValue(0);
        ip.setBackgroundValue(0);
        IJ.run(bimp, "Clear Outside", "");
        stack.addSlice(Double.toString(z), ip);
    }
    final ImagePlus imp = new ImagePlus("AreaList stack for " + this, stack);
    imp.setCalibration(layer_set.getCalibrationCopy());
    return imp;
}
Also used : ImageProcessor(ij.process.ImageProcessor) Area(java.awt.geom.Area) ShapeRoi(ij.gui.ShapeRoi) ImageStack(ij.ImageStack) Rectangle(java.awt.Rectangle) AffineTransform(java.awt.geom.AffineTransform) ImagePlus(ij.ImagePlus) Point(java.awt.Point) USHORTPaint(ini.trakem2.display.paint.USHORTPaint)

Example 29 with LayerSet

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

the class Project method createNewProject.

private static Project createNewProject(Loader loader, boolean ask_for_template, TemplateThing template_root, boolean clone_ids) {
    Project project = new Project(loader);
    // Utils.log2("ask_for_template: " + ask_for_template);
    if (ask_for_template)
        template_root = project.loader.askForXMLTemplate(project);
    if (null == template_root) {
        template_root = new TemplateThing("anything");
    } else if (clone_ids) {
        // the given template_root belongs to another project from which we are cloning
        template_root = template_root.clone(project, true);
    }
    // else, use the given template_root as is.
    // create tree
    project.template_tree = new TemplateTree(project, template_root);
    project.root_tt = template_root;
    // collect unique TemplateThing instances
    synchronized (project.ht_unique_tt) {
        project.ht_unique_tt.clear();
        project.ht_unique_tt.putAll(template_root.getUniqueTypes(new HashMap<String, TemplateThing>()));
    }
    // add all TemplateThing objects to the database, recursively
    if (!clone_ids)
        template_root.addToDatabase(project);
    // else already done when cloning the root_tt
    // create a non-database bound template for the project Thing
    TemplateThing project_template = new TemplateThing("project");
    project.ht_unique_tt.put("project", project_template);
    project_template.addChild(template_root);
    // create the project Thing, to be root of the whole project thing tree
    try {
        project.root_pt = new ProjectThing(project_template, project, project);
    } catch (Exception e) {
        IJError.print(e);
    }
    // create the user objects tree
    project.project_tree = new ProjectTree(project, project.root_pt);
    // create the layer's tree
    project.createLayerTemplates();
    // initialized with default values, and null parent to signal 'root'
    project.layer_set = new LayerSet(project, "Top Level", 0, 0, null, 2048, 2048);
    try {
        project.root_lt = new LayerThing(Project.layer_set_template, project, project.layer_set);
        project.layer_tree = new LayerTree(project, project.root_lt);
    } catch (Exception e) {
        project.remove();
        IJError.print(e);
    }
    // create the project control window, containing the trees in a double JSplitPane
    // beware that this call is asynchronous, dispatched by the SwingUtilities.invokeLater to avoid havok with Swing components.
    ControlWindow.add(project, project.template_tree, project.project_tree, project.layer_tree);
    // register
    al_open_projects.add(project);
    return project;
}
Also used : ProjectTree(ini.trakem2.tree.ProjectTree) LayerTree(ini.trakem2.tree.LayerTree) HashMap(java.util.HashMap) LayerSet(ini.trakem2.display.LayerSet) LayerThing(ini.trakem2.tree.LayerThing) TemplateThing(ini.trakem2.tree.TemplateThing) TemplateTree(ini.trakem2.tree.TemplateTree) ProjectThing(ini.trakem2.tree.ProjectThing)

Example 30 with LayerSet

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

the class AreaList method exportAsLabels.

/**
 * Export all given AreaLists as one per pixel value, what is called a "labels" file; a file dialog is offered to save the image as a tiff stack.
 */
public static void exportAsLabels(final List<Displayable> listToPaint, final ij.gui.Roi roi, final float scale, int first_layer, int last_layer, final boolean visible_only, final boolean to_file, final boolean as_amira_labels) {
    // survive everything:
    if (null == listToPaint || 0 == listToPaint.size()) {
        Utils.log("Null or empty list.");
        return;
    }
    if (scale < 0 || scale > 1) {
        Utils.log("Improper scale value. Must be 0 < scale <= 1");
        return;
    }
    // Select the subset to paint
    final ArrayList<AreaList> list = new ArrayList<AreaList>();
    for (final Displayable d : listToPaint) {
        if (visible_only && !d.isVisible())
            continue;
        if (d instanceof AreaList)
            list.add((AreaList) d);
    }
    Utils.log2("exportAsLabels: list.size() is " + list.size());
    // Current AmiraMeshEncoder supports ByteProcessor only: 256 labels max, including background at zero.
    if (as_amira_labels && list.size() > 255) {
        Utils.log("Saving ONLY first 255 AreaLists!\nDiscarded:");
        final StringBuilder sb = new StringBuilder();
        for (final Displayable d : list.subList(255, list.size())) {
            sb.append("    ").append(d.getProject().getShortMeaningfulTitle(d)).append('\n');
        }
        Utils.log(sb.toString());
        final ArrayList<AreaList> li = new ArrayList<AreaList>(list);
        list.clear();
        list.addAll(li.subList(0, 255));
    }
    String path = null;
    if (to_file) {
        final String ext = as_amira_labels ? ".am" : ".tif";
        final File f = Utils.chooseFile("labels", ext);
        if (null == f)
            return;
        path = f.getAbsolutePath().replace('\\', '/');
    }
    final LayerSet layer_set = list.get(0).getLayerSet();
    if (first_layer > last_layer) {
        final int tmp = first_layer;
        first_layer = last_layer;
        last_layer = tmp;
        if (first_layer < 0)
            first_layer = 0;
        if (last_layer >= layer_set.size())
            last_layer = layer_set.size() - 1;
    }
    // Create image according to roi and scale
    final int width, height;
    final Rectangle broi;
    if (null == roi) {
        broi = null;
        width = (int) (layer_set.getLayerWidth() * scale);
        height = (int) (layer_set.getLayerHeight() * scale);
    } else {
        broi = roi.getBounds();
        width = (int) (broi.width * scale);
        height = (int) (broi.height * scale);
    }
    // Compute highest label value, which affects of course the stack image type
    final TreeSet<Integer> label_values = new TreeSet<Integer>();
    for (final Displayable d : list) {
        final String label = d.getProperty("label");
        if (null != label)
            label_values.add(Integer.parseInt(label));
    }
    int lowest = 0, highest = 0;
    if (label_values.size() > 0) {
        lowest = label_values.first();
        highest = label_values.last();
    }
    final int n_non_labeled = list.size() - label_values.size();
    final int max_label_value = highest + n_non_labeled;
    int type_ = ImagePlus.GRAY8;
    if (max_label_value > 255) {
        type_ = ImagePlus.GRAY16;
        if (max_label_value > 65535) {
            type_ = ImagePlus.GRAY32;
        }
    }
    final int type = type_;
    final ImageStack stack = new ImageStack(width, height);
    final Calibration cal = layer_set.getCalibration();
    String amira_params = null;
    if (as_amira_labels) {
        final StringBuilder sb = new StringBuilder("CoordType \"uniform\"\nMaterials {\nExterior {\n Id 0,\nColor 0 0 0\n}\n");
        final float[] c = new float[3];
        int value = 0;
        for (final Displayable d : list) {
            // 0 is background
            value++;
            d.getColor().getRGBColorComponents(c);
            String s = d.getProject().getShortMeaningfulTitle(d);
            s = s.replace('-', '_').replaceAll(" #", " id");
            sb.append(Utils.makeValidIdentifier(s)).append(" {\n").append("Id ").append(value).append(",\n").append("Color ").append(c[0]).append(' ').append(c[1]).append(' ').append(c[2]).append("\n}\n");
        }
        sb.append("}\n");
        amira_params = sb.toString();
    }
    final float len = last_layer - first_layer + 1;
    // Assign labels
    final HashMap<AreaList, Integer> labels = new HashMap<AreaList, Integer>();
    for (final AreaList d : list) {
        final String slabel = d.getProperty("label");
        int label;
        if (null != slabel) {
            label = Integer.parseInt(slabel);
        } else {
            // 0 is background
            label = (++highest);
        }
        labels.put(d, label);
    }
    final ExecutorService exec = Utils.newFixedThreadPool("labels");
    final Map<Integer, ImageProcessor> slices = Collections.synchronizedMap(new TreeMap<Integer, ImageProcessor>());
    final List<Future<?>> fus = new ArrayList<Future<?>>();
    final List<Layer> layers = layer_set.getLayers().subList(first_layer, last_layer + 1);
    for (int k = 0; k < layers.size(); k++) {
        final Layer la = layers.get(k);
        final int slice = k;
        fus.add(exec.submit(new Runnable() {

            @Override
            public void run() {
                Utils.showProgress(slice / len);
                final ImageProcessor ip;
                if (ImagePlus.GRAY8 == type) {
                    final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
                    final Graphics2D g = bi.createGraphics();
                    for (final AreaList ali : list) {
                        final Area area = ali.getArea(la);
                        if (null == area || area.isEmpty())
                            continue;
                        // Transform: the scale and the roi
                        final AffineTransform aff = new AffineTransform();
                        /* 3 - To scale: */
                        if (1 != scale)
                            aff.scale(scale, scale);
                        /* 2 - To roi coordinates: */
                        if (null != broi)
                            aff.translate(-broi.x, -broi.y);
                        /* 1 - To world coordinates: */
                        aff.concatenate(ali.at);
                        g.setTransform(aff);
                        final int label = labels.get(ali);
                        g.setColor(new Color(label, label, label));
                        g.fill(area);
                    }
                    g.dispose();
                    ip = new ByteProcessor(bi);
                    bi.flush();
                } else if (ImagePlus.GRAY16 == type) {
                    final USHORTPaint paint = new USHORTPaint((short) 0);
                    final BufferedImage bi = new BufferedImage(paint.getComponentColorModel(), paint.getComponentColorModel().createCompatibleWritableRaster(width, height), false, null);
                    final Graphics2D g = bi.createGraphics();
                    // final ColorSpace ugray = ColorSpace.getInstance(ColorSpace.CS_GRAY);
                    int painted = 0;
                    for (final AreaList ali : list) {
                        final Area area = ali.getArea(la);
                        if (null == area || area.isEmpty())
                            continue;
                        // Transform: the scale and the roi
                        final AffineTransform aff = new AffineTransform();
                        /* 3 - To scale: */
                        if (1 != scale)
                            aff.scale(scale, scale);
                        /* 2 - To roi coordinates: */
                        if (null != broi)
                            aff.translate(-broi.x, -broi.y);
                        /* 1 - To world coordinates: */
                        aff.concatenate(ali.at);
                        // Fill
                        g.setTransform(aff);
                        // The color doesn't work: paints in a stretched 8-bit mode
                        // g.setColor(new Color(ugray, new float[]{((float)labels.get(d)) / range}, 1));
                        Utils.log2("value: " + labels.get(ali).shortValue());
                        paint.setValue(labels.get(ali).shortValue());
                        g.setPaint(paint);
                        // .createTransformedArea(aff));
                        g.fill(area);
                        painted += 1;
                    }
                    g.dispose();
                    ip = new ShortProcessor(bi);
                    bi.flush();
                    Utils.log2("painted: " + painted);
                } else {
                    // Option 1: could use the same as above, but shifted by 65536, so that 65537 is 1, 65538 is 2, etc.
                    // and keep doing it until no more need to be shifted.
                    // The PROBLEM: cannot keep the order without complicated gymnastics to remember
                    // which label in which image has to be merged to the final image, which prevent
                    // a simple one-pass blitter.
                    // 
                    // Option 2: paint each arealist, extract the image, use it as a mask for filling:
                    final FloatProcessor fp = new FloatProcessor(width, height);
                    final float[] fpix = (float[]) fp.getPixels();
                    ip = fp;
                    final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
                    final Graphics2D gbi = bi.createGraphics();
                    for (final AreaList ali : list) {
                        final Area area = ali.getArea(la);
                        if (null == area || area.isEmpty()) {
                            continue;
                        }
                        // Transform: the scale and the roi
                        // reverse order of transformations:
                        final AffineTransform aff = new AffineTransform();
                        /* 3 - To scale: */
                        if (1 != scale)
                            aff.scale(scale, scale);
                        /* 2 - To ROI coordinates: */
                        if (null != broi)
                            aff.translate(-broi.x, -broi.y);
                        /* 1 - To world coordinates: */
                        aff.concatenate(ali.at);
                        final Area s = area.createTransformedArea(aff);
                        final Rectangle sBounds = s.getBounds();
                        // Need to paint at all?
                        if (0 == sBounds.width || 0 == sBounds.height || !sBounds.intersects(0, 0, width, height))
                            continue;
                        // Paint shape
                        gbi.setColor(Color.white);
                        gbi.fill(s);
                        // Read out painted region
                        final int x0 = Math.max(0, sBounds.x);
                        final int y0 = Math.max(0, sBounds.y);
                        final int xN = Math.min(width, sBounds.x + sBounds.width);
                        final int yN = Math.min(height, sBounds.y + sBounds.height);
                        // Get the array
                        final byte[] bpix = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
                        final float value = labels.get(ali);
                        // For every non-black pixel, set a 'value' pixel in the FloatProcessor
                        for (int y = y0; y < yN; ++y) {
                            for (int x = x0; x < xN; ++x) {
                                final int pos = y * width + x;
                                // black
                                if (0 == bpix[pos])
                                    continue;
                                fpix[pos] = value;
                            }
                        }
                        // Clear image region
                        gbi.setColor(Color.black);
                        gbi.fill(s);
                    }
                    gbi.dispose();
                    bi.flush();
                }
                slices.put(slice, ip);
            }
        }));
    }
    Utils.wait(fus);
    exec.shutdownNow();
    for (final Map.Entry<Integer, ImageProcessor> e : slices.entrySet()) {
        final Layer la = layers.get(e.getKey());
        stack.addSlice(la.getZ() * cal.pixelWidth + "", e.getValue());
        if (ImagePlus.GRAY8 != type) {
            e.getValue().setMinAndMax(lowest, highest);
        }
    }
    Utils.showProgress(1);
    // Save via file dialog:
    final ImagePlus imp = new ImagePlus("Labels", stack);
    if (as_amira_labels)
        imp.setProperty("Info", amira_params);
    imp.setCalibration(layer_set.getCalibrationCopy());
    if (to_file) {
        if (as_amira_labels) {
            final AmiraMeshEncoder ame = new AmiraMeshEncoder(path);
            if (!ame.open()) {
                Utils.log("Could not write to file " + path);
                return;
            }
            if (!ame.write(imp)) {
                Utils.log("Error in writing Amira file!");
                return;
            }
        } else {
            new FileSaver(imp).saveAsTiff(path);
        }
    } else
        imp.show();
}
Also used : ByteProcessor(ij.process.ByteProcessor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) BufferedImage(java.awt.image.BufferedImage) ImageProcessor(ij.process.ImageProcessor) USHORTPaint(ini.trakem2.display.paint.USHORTPaint) FileSaver(ij.io.FileSaver) TreeSet(java.util.TreeSet) ImageStack(ij.ImageStack) FloatProcessor(ij.process.FloatProcessor) Color(java.awt.Color) Calibration(ij.measure.Calibration) ImagePlus(ij.ImagePlus) Point(java.awt.Point) USHORTPaint(ini.trakem2.display.paint.USHORTPaint) Graphics2D(java.awt.Graphics2D) ShortProcessor(ij.process.ShortProcessor) Area(java.awt.geom.Area) ExecutorService(java.util.concurrent.ExecutorService) AmiraMeshEncoder(amira.AmiraMeshEncoder) Future(java.util.concurrent.Future) AffineTransform(java.awt.geom.AffineTransform) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Aggregations

ArrayList (java.util.ArrayList)24 Layer (ini.trakem2.display.Layer)22 LayerSet (ini.trakem2.display.LayerSet)20 HashMap (java.util.HashMap)20 HashSet (java.util.HashSet)19 Rectangle (java.awt.Rectangle)14 Patch (ini.trakem2.display.Patch)13 ImagePlus (ij.ImagePlus)11 GenericDialog (ij.gui.GenericDialog)11 ProjectThing (ini.trakem2.tree.ProjectThing)11 AffineTransform (java.awt.geom.AffineTransform)11 Map (java.util.Map)11 Future (java.util.concurrent.Future)11 Displayable (ini.trakem2.display.Displayable)10 ZDisplayable (ini.trakem2.display.ZDisplayable)10 Area (java.awt.geom.Area)10 Worker (ini.trakem2.utils.Worker)9 Point (java.awt.Point)9 TreeMap (java.util.TreeMap)9 DBObject (ini.trakem2.persistence.DBObject)8