Search in sources :

Example 11 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class Util method serializePointMatches.

/**
 * Save a {@link Collection} of {@link PointMatch PointMatches} two-sided.
 * Creates two serialization files which is desperately required to clean
 * up properly invalid serializations on change of a {@link Patch}.
 *
 * @param project
 * @param key
 * @param prefix
 * @param id1
 * @param id2
 * @param m
 * @return
 */
public static final boolean serializePointMatches(final Project project, final Object key, final String prefix, final long id1, final long id2, final Collection<PointMatch> m) {
    final ArrayList<PointMatch> list = new ArrayList<PointMatch>();
    list.addAll(m);
    final ArrayList<PointMatch> tsil = new ArrayList<PointMatch>();
    PointMatch.flip(m, tsil);
    final String name = prefix == null ? "pointmatches" : prefix + ".pointmatches";
    final Loader loader = project.getLoader();
    return loader.serialize(new PointMatches(key, list), new StringBuilder(loader.getUNUIdFolder()).append("pointmatches.ser/").append(FSLoader.createIdPath(Long.toString(id1) + "_" + Long.toString(id2), name, ".ser")).toString()) && loader.serialize(new PointMatches(key, tsil), new StringBuilder(loader.getUNUIdFolder()).append("pointmatches.ser/").append(FSLoader.createIdPath(Long.toString(id2) + "_" + Long.toString(id1), name, ".ser")).toString());
}
Also used : PointMatch(mpicbg.models.PointMatch) ArrayList(java.util.ArrayList) Loader(ini.trakem2.persistence.Loader) FSLoader(ini.trakem2.persistence.FSLoader)

Example 12 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class MatchIntensities method run.

/**
 * @param layers
 * @param radius
 * @param scale
 * @param numCoefficients
 * @param lambda1
 * @param lambda2
 * @param neighborWeight
 * @param roi
 */
public <M extends Model<M> & Affine1D<M>> void run(final List<Layer> layers, final int radius, final double scale, final int numCoefficients, final double lambda1, final double lambda2, final double neighborWeight, final Rectangle roi) throws InterruptedException, ExecutionException {
    final int firstLayerIndex = layerset.getLayerIndex(layers.get(0).getId());
    final int lastLayerIndex = layerset.getLayerIndex(layers.get(layers.size() - 1).getId());
    // final PointMatchFilter filter = new RansacRegressionFilter();
    final PointMatchFilter filter = new RansacRegressionReduceFilter();
    /* collect patches */
    Utils.log("Collecting patches ... ");
    final ArrayList<Patch> patches = new ArrayList<Patch>();
    for (final Layer layer : layers) patches.addAll((Collection) layer.getDisplayables(Patch.class, roi));
    /* delete existing intensity coefficients */
    Utils.log("Clearing existing intensity maps ... ");
    for (final Patch p : patches) p.clearIntensityMap();
    /* generate coefficient tiles for all patches
		 * TODO consider offering alternative models */
    final HashMap<Patch, ArrayList<Tile<? extends M>>> coefficientsTiles = (HashMap) generateCoefficientsTiles(patches, new InterpolatedAffineModel1D<InterpolatedAffineModel1D<AffineModel1D, TranslationModel1D>, IdentityModel>(new InterpolatedAffineModel1D<AffineModel1D, TranslationModel1D>(new AffineModel1D(), new TranslationModel1D(), lambda1), new IdentityModel(), lambda2), numCoefficients * numCoefficients);
    /* completed patches */
    final HashSet<Patch> completedPatches = new HashSet<Patch>();
    /* collect patch pairs */
    Utils.log("Collecting patch pairs ... ");
    final ArrayList<ValuePair<Patch, Patch>> patchPairs = new ArrayList<ValuePair<Patch, Patch>>();
    for (final Patch p1 : patches) {
        completedPatches.add(p1);
        final Rectangle box1 = p1.getBoundingBox().intersection(roi);
        final ArrayList<Patch> p2s = new ArrayList<Patch>();
        /* across adjacent layers */
        final int layerIndex = layerset.getLayerIndex(p1.getLayer().getId());
        for (int i = Math.max(firstLayerIndex, layerIndex - radius); i <= Math.min(lastLayerIndex, layerIndex + radius); ++i) {
            final Layer layer = layerset.getLayer(i);
            if (layer != null)
                p2s.addAll((Collection) layer.getDisplayables(Patch.class, box1));
        }
        for (final Patch p2 : p2s) {
            /*
				 * if this patch had been processed earlier, all matches are
				 * already in
				 */
            if (completedPatches.contains(p2))
                continue;
            patchPairs.add(new ValuePair<Patch, Patch>(p1, p2));
        }
    }
    final int numThreads = Integer.parseInt(layerset.getProperty("n_mipmap_threads", Integer.toString(Runtime.getRuntime().availableProcessors())));
    Utils.log("Matching intensities using " + numThreads + " threads ... ");
    final ExecutorService exec = Executors.newFixedThreadPool(numThreads);
    final ArrayList<Future<?>> futures = new ArrayList<Future<?>>();
    for (final ValuePair<Patch, Patch> patchPair : patchPairs) {
        futures.add(exec.submit(new Matcher(roi, patchPair, (HashMap) coefficientsTiles, filter, scale, numCoefficients)));
    }
    for (final Future<?> future : futures) future.get();
    /* connect tiles within patches */
    Utils.log("Connecting coefficient tiles in the same patch  ... ");
    for (final Patch p1 : completedPatches) {
        /* get the coefficient tiles */
        final ArrayList<Tile<? extends M>> p1CoefficientsTiles = coefficientsTiles.get(p1);
        for (int y = 1; y < numCoefficients; ++y) {
            final int yr = numCoefficients * y;
            final int yr1 = yr - numCoefficients;
            for (int x = 0; x < numCoefficients; ++x) {
                identityConnect(p1CoefficientsTiles.get(yr1 + x), p1CoefficientsTiles.get(yr + x), neighborWeight);
            }
        }
        for (int y = 0; y < numCoefficients; ++y) {
            final int yr = numCoefficients * y;
            for (int x = 1; x < numCoefficients; ++x) {
                final int yrx = yr + x;
                identityConnect(p1CoefficientsTiles.get(yrx), p1CoefficientsTiles.get(yrx - 1), neighborWeight);
            }
        }
    }
    /* optimize */
    Utils.log("Optimizing ... ");
    final TileConfiguration tc = new TileConfiguration();
    for (final ArrayList<Tile<? extends M>> coefficients : coefficientsTiles.values()) {
        // for ( final Tile< ? > t : coefficients )
        // if ( t.getMatches().size() == 0 )
        // IJ.log( "bang" );
        tc.addTiles(coefficients);
    }
    try {
        tc.optimize(0.01f, iterations, iterations, 0.75f);
    } catch (final NotEnoughDataPointsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final IllDefinedDataPointsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    /* save coefficients */
    final double[] ab = new double[2];
    final FSLoader loader = (FSLoader) layerset.getProject().getLoader();
    final String itsDir = loader.getUNUIdFolder() + "trakem2.its/";
    for (final Entry<Patch, ArrayList<Tile<? extends M>>> entry : coefficientsTiles.entrySet()) {
        final FloatProcessor as = new FloatProcessor(numCoefficients, numCoefficients);
        final FloatProcessor bs = new FloatProcessor(numCoefficients, numCoefficients);
        final Patch p = entry.getKey();
        final double min = p.getMin();
        final double max = p.getMax();
        final ArrayList<Tile<? extends M>> tiles = entry.getValue();
        for (int i = 0; i < numCoefficients * numCoefficients; ++i) {
            final Tile<? extends M> t = tiles.get(i);
            final Affine1D<?> affine = t.getModel();
            affine.toArray(ab);
            /* coefficients mapping into existing [min, max] */
            as.setf(i, (float) ab[0]);
            bs.setf(i, (float) ((max - min) * ab[1] + min - ab[0] * min));
        }
        final ImageStack coefficientsStack = new ImageStack(numCoefficients, numCoefficients);
        coefficientsStack.addSlice(as);
        coefficientsStack.addSlice(bs);
        final String itsPath = itsDir + FSLoader.createIdPath(Long.toString(p.getId()), "it", ".tif");
        new File(itsPath).getParentFile().mkdirs();
        IJ.saveAs(new ImagePlus("", coefficientsStack), "tif", itsPath);
    }
    /* update mipmaps */
    for (final Patch p : patches) p.getProject().getLoader().decacheImagePlus(p.getId());
    final ArrayList<Future<Boolean>> mipmapFutures = new ArrayList<Future<Boolean>>();
    for (final Patch p : patches) mipmapFutures.add(p.updateMipMaps());
    for (final Future<Boolean> f : mipmapFutures) f.get();
    Utils.log("Matching intensities done.");
}
Also used : NotEnoughDataPointsException(mpicbg.models.NotEnoughDataPointsException) HashMap(java.util.HashMap) ValuePair(net.imglib2.util.ValuePair) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) InterpolatedAffineModel1D(mpicbg.models.InterpolatedAffineModel1D) IdentityModel(mpicbg.models.IdentityModel) TranslationModel1D(mpicbg.models.TranslationModel1D) HashSet(java.util.HashSet) FloatProcessor(ij.process.FloatProcessor) ImageStack(ij.ImageStack) IllDefinedDataPointsException(mpicbg.models.IllDefinedDataPointsException) Tile(mpicbg.models.Tile) Layer(ini.trakem2.display.Layer) ImagePlus(ij.ImagePlus) Point(mpicbg.models.Point) FSLoader(ini.trakem2.persistence.FSLoader) ExecutorService(java.util.concurrent.ExecutorService) Collection(java.util.Collection) InterpolatedAffineModel1D(mpicbg.models.InterpolatedAffineModel1D) AffineModel1D(mpicbg.models.AffineModel1D) Future(java.util.concurrent.Future) TileConfiguration(mpicbg.models.TileConfiguration) Patch(ini.trakem2.display.Patch) File(java.io.File)

Example 13 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class Patch method exportXML.

/**
 * Opens and closes the tag and exports data. The image is saved in the directory provided in @param any as a String.
 */
@Override
public void exportXML(final StringBuilder sb_body, final String indent, final XMLOptions options) {
    // TODO the Loader should handle the saving of images, not this class.
    final String in = indent + "\t";
    String path = null;
    String path2 = null;
    if (options.export_images) {
        path = options.patches_dir + title;
        // save image without overwriting, and add proper extension (.zip)
        path2 = project.getLoader().exportImage(this, path, false);
    // path2 will be null if the file exists already
    }
    sb_body.append(indent).append("<t2_patch\n");
    String rel_path = null;
    if (null != path && path.equals(path2)) {
        // this happens when a DB project is exported. It may be a different path when it's a FS loader
        // Utils.log2("p id=" + id + "  path==path2");
        rel_path = path2;
        // TrakEM2 uses paths that always have '/' and never '\', so using java.io.File.separatorChar would be an error.
        int i_slash = rel_path.lastIndexOf('/');
        if (i_slash > 0) {
            i_slash = rel_path.lastIndexOf('/', i_slash - 1);
            if (-1 != i_slash) {
                rel_path = rel_path.substring(i_slash + 1);
            }
        }
    } else {
        // Utils.log2("Setting rel_path to " + path2);
        rel_path = path2;
    }
    // For FSLoader projects, saving a second time will save images as null unless calling it
    if (null == rel_path) {
        // Utils.log2("path2 was null");
        final Object ob = project.getLoader().getPath(this);
        path2 = null == ob ? null : (String) ob;
        if (null == path2) {
            // Utils.log2("ERROR: No path for Patch id=" + id + " and title: " + title);
            // at least some clue for recovery
            rel_path = title;
        } else {
            rel_path = path2;
        }
    }
    // Utils.log("Patch path is: " + rel_path);
    super.exportXML(sb_body, in, options);
    final String[] RGB = Utils.getHexRGBColor(color);
    int type = this.type;
    if (-1 == this.type) {
        Utils.log2("Retrieving type for p = " + this);
        final ImagePlus imp = project.getLoader().fetchImagePlus(this);
        if (null != imp)
            type = imp.getType();
    }
    sb_body.append(in).append("type=\"").append(type).append("\"\n").append(in).append("file_path=\"").append(rel_path).append("\"\n").append(in).append("style=\"fill-opacity:").append(alpha).append(";stroke:#").append(RGB[0]).append(RGB[1]).append(RGB[2]).append(";\"\n").append(in).append("o_width=\"").append(o_width).append("\"\n").append(in).append("o_height=\"").append(o_height).append("\"\n");
    if (null != original_path) {
        sb_body.append(in).append("original_path=\"").append(original_path).append("\"\n");
    }
    sb_body.append(in).append("min=\"").append(min).append("\"\n");
    sb_body.append(in).append("max=\"").append(max).append("\"\n");
    final String pps = getPreprocessorScriptPath();
    if (null != pps)
        sb_body.append(in).append("pps=\"").append(project.getLoader().makeRelativePath(pps)).append("\"\n");
    sb_body.append(in).append("mres=\"").append(meshResolution).append("\"\n");
    if (hasCoordinateTransform()) {
        sb_body.append(in).append("ct_id=\"").append(ct_id).append("\"\n");
    }
    if (hasAlphaMask()) {
        sb_body.append(in).append("alpha_mask_id=\"").append(alpha_mask_id).append("\"\n");
    }
    sb_body.append(indent).append(">\n");
    if (hasCoordinateTransform()) {
        if (options.include_coordinate_transform) {
            // Write an XML entry for the CoordinateTransform
            char[] ct_chars = null;
            try {
                ct_chars = readCoordinateTransformFile();
            } catch (final Exception e) {
                IJError.print(e);
            }
            if (null != ct_chars) {
                sb_body.append(ct_chars).append('\n');
            } else {
                Utils.log("ERROR: could not write the CoordinateTransform to the XML file!");
            }
        }
    }
    if (null != filters && filters.length > 0) {
        // specify their own line termination
        for (final IFilter f : filters) sb_body.append(f.toXML(in));
    }
    super.restXML(sb_body, in, options);
    sb_body.append(indent).append("</t2_patch>\n");
}
Also used : IFilter(ini.trakem2.imaging.filters.IFilter) ImagePlus(ij.ImagePlus) NoninvertibleModelException(mpicbg.models.NoninvertibleModelException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) IOException(java.io.IOException)

Example 14 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class Loader method insertGrid.

/**
 * Insert grid in layer (with optional stitching)
 *
 * @param layer The Layer to inser the grid into
 * @param dir The base dir of the images to open
 * @param first_image_name name of the first image in the list
 * @param cols The list of columns, containing each an array of String file names in each column.
 * @param bx The top-left X coordinate of the grid to insert
 * @param by The top-left Y coordinate of the grid to insert
 * @param bt_overlap bottom-top overlap of the images
 * @param lr_overlap left-right overlap of the images
 * @param link_images Link images to their neighbors
 * @param stitch_tiles montage option
 * @param cc_percent_overlap tiles overlap
 * @param cc_scale tiles scaling previous to stitching (1 = no scaling)
 * @param min_R regression threshold (minimum acceptable R)
 * @param homogenize_contrast contrast homogenization option
 * @param stitching_rule stitching rule (upper left corner or free)
 */
private void insertGrid(final Layer layer, final String dir_, final String first_image_name, final int n_images, final ArrayList<String[]> cols, final double bx, final double by, final double bt_overlap, final double lr_overlap, final boolean link_images, final boolean stitch_tiles, final boolean homogenize_contrast, final StitchingTEM.PhaseCorrelationParam pc_param, final Worker worker) {
    // create a Worker, then give it to the Bureaucrat
    try {
        String dir = dir_;
        final ArrayList<Patch> al = new ArrayList<Patch>();
        Utils.showProgress(0.0D);
        // less repaints on IJ status bar
        opener.setSilentMode(true);
        int x = 0;
        int y = 0;
        int largest_y = 0;
        ImagePlus img = null;
        // open the selected image, to use as reference for width and height
        // w1nd0wz safe
        dir = dir.replace('\\', '/');
        if (!dir.endsWith("/"))
            dir += "/";
        String path = dir + first_image_name;
        // TODO arbitrary x3 factor
        releaseToFit(new File(path).length() * 3);
        IJ.redirectErrorMessages();
        ImagePlus first_img = openImagePlus(path);
        if (null == first_img) {
            Utils.log("Selected image to open first is null.");
            return;
        }
        if (null == first_img)
            return;
        final int first_image_width = first_img.getWidth();
        final int first_image_height = first_img.getHeight();
        final int first_image_type = first_img.getType();
        // start
        final Patch[][] pall = new Patch[cols.size()][((String[]) cols.get(0)).length];
        int width, height;
        // counter
        int k = 0;
        boolean auto_fix_all = false;
        boolean ignore_all = false;
        boolean resize = false;
        if (!ControlWindow.isGUIEnabled()) {
            // headless mode: autofix all
            auto_fix_all = true;
            resize = true;
        }
        // Accumulate mipmap generation tasks
        final ArrayList<Future<?>> fus = new ArrayList<Future<?>>();
        startLargeUpdate();
        for (int i = 0; i < cols.size(); i++) {
            final String[] rows = (String[]) cols.get(i);
            if (i > 0) {
                x -= lr_overlap;
            }
            for (int j = 0; j < rows.length; j++) {
                if (Thread.currentThread().isInterrupted()) {
                    Display.repaint(layer);
                    rollback();
                    return;
                }
                if (j > 0) {
                    y -= bt_overlap;
                }
                // get file name
                final String file_name = (String) rows[j];
                path = dir + file_name;
                if (null != first_img && file_name.equals(first_image_name)) {
                    img = first_img;
                    // release pointer
                    first_img = null;
                } else {
                    // open image
                    releaseToFit(first_image_width, first_image_height, first_image_type, 1.5f);
                    try {
                        IJ.redirectErrorMessages();
                        img = openImagePlus(path);
                    } catch (final OutOfMemoryError oome) {
                        printMemState();
                        throw oome;
                    }
                }
                if (null == img) {
                    Utils.log("null image! skipping.");
                    pall[i][j] = null;
                    continue;
                }
                width = img.getWidth();
                height = img.getHeight();
                int rw = width;
                int rh = height;
                if (width != first_image_width || height != first_image_height) {
                    int new_width = first_image_width;
                    int new_height = first_image_height;
                    if (!auto_fix_all && !ignore_all) {
                        final GenericDialog gdr = new GenericDialog("Size mismatch!");
                        gdr.addMessage("The size of " + file_name + " is " + width + " x " + height);
                        gdr.addMessage("but the selected image was " + first_image_width + " x " + first_image_height);
                        gdr.addMessage("Adjust to selected image dimensions?");
                        gdr.addNumericField("width: ", (double) first_image_width, 0);
                        // should not be editable ... or at least, explain in some way that the dimensions can be edited just for this image --> done below
                        gdr.addNumericField("height: ", (double) first_image_height, 0);
                        gdr.addMessage("[If dimensions are changed they will apply only to this image]");
                        gdr.addMessage("");
                        final String[] au = new String[] { "fix all", "ignore all" };
                        gdr.addChoice("Automate:", au, au[1]);
                        gdr.addMessage("Cancel == NO    OK = YES");
                        gdr.showDialog();
                        if (gdr.wasCanceled()) {
                            resize = false;
                        // do nothing: don't fix/resize
                        }
                        resize = true;
                        // catch values
                        new_width = (int) gdr.getNextNumber();
                        new_height = (int) gdr.getNextNumber();
                        final int iau = gdr.getNextChoiceIndex();
                        if (new_width != first_image_width || new_height != first_image_height) {
                            auto_fix_all = false;
                        } else {
                            auto_fix_all = (0 == iau);
                        }
                        ignore_all = (1 == iau);
                        if (ignore_all)
                            resize = false;
                    }
                    if (resize) {
                        // resize Patch dimensions
                        rw = first_image_width;
                        rh = first_image_height;
                    }
                }
                // add new Patch at base bx,by plus the x,y of the grid
                // will call back and cache the image
                final Patch patch = new Patch(layer.getProject(), img.getTitle(), bx + x, by + y, img);
                if (width != rw || height != rh)
                    patch.setDimensions(rw, rh, false);
                addedPatchFrom(path, patch);
                if (// prevent it
                homogenize_contrast)
                    // prevent it
                    setMipMapsRegeneration(false);
                else
                    fus.add(regenerateMipMaps(patch));
                // 
                // after the above two lines! Otherwise it will paint fine, but throw exceptions on the way
                layer.add(patch, true);
                // otherwise when reopening it has to fetch all ImagePlus and scale and zip them all! This method though creates the awt and the snap, thus filling up memory and slowing down, but it's worth it.
                patch.updateInDatabase("tiff_snapshot");
                pall[i][j] = patch;
                al.add(patch);
                if (ControlWindow.isGUIEnabled()) {
                    // northwest to prevent screwing up Patch coordinates.
                    layer.getParent().enlargeToFit(patch, LayerSet.NORTHWEST);
                }
                y += img.getHeight();
                Utils.showProgress((double) k / n_images);
                k++;
            }
            x += img.getWidth();
            if (largest_y < y) {
                largest_y = y;
            }
            // resetting!
            y = 0;
        }
        // build list
        final Patch[] pa = new Patch[al.size()];
        int f = 0;
        // list in row-first order
        for (int j = 0; j < pall[0].length; j++) {
            // 'j' is row
            for (int i = 0; i < pall.length; i++) {
                // 'i' is column
                pa[f++] = pall[i][j];
            }
        }
        // optimize repaints: all to background image
        Display.clearSelection(layer);
        // make the first one be top, and the rest under it in left-right and top-bottom order
        for (int j = 0; j < pa.length; j++) {
            layer.moveBottom(pa[j]);
        }
        // make picture
        // getFlatImage(layer, layer.getMinimalBoundingBox(Patch.class), 0.25, 1, ImagePlus.GRAY8, Patch.class, null, false).show();
        // optimize repaints: all to background image
        Display.clearSelection(layer);
        if (homogenize_contrast) {
            if (null != worker)
                worker.setTaskName("Enhancing contrast");
            // 0 - check that all images are of the same type
            int tmp_type = pa[0].getType();
            for (int e = 1; e < pa.length; e++) {
                if (pa[e].getType() != tmp_type) {
                    // can't continue
                    tmp_type = Integer.MAX_VALUE;
                    Utils.log("Can't homogenize histograms: images are not all of the same type.\nFirst offending image is: " + al.get(e));
                    break;
                }
            }
            if (Integer.MAX_VALUE != tmp_type) {
                // checking on error flag
                // Set min and max for all images
                // 1 - fetch statistics for each image
                final ArrayList<ImageStatistics> al_st = new ArrayList<ImageStatistics>();
                // list of Patch ordered by stdDev ASC
                final ArrayList<Patch> al_p = new ArrayList<Patch>();
                int type = -1;
                for (int i = 0; i < pa.length; i++) {
                    if (Thread.currentThread().isInterrupted()) {
                        Display.repaint(layer);
                        rollback();
                        return;
                    }
                    ImagePlus imp = fetchImagePlus(pa[i]);
                    // speed-up trick: extract data from smaller image
                    if (imp.getWidth() > 1024) {
                        releaseToFit(1024, (int) ((imp.getHeight() * 1024) / imp.getWidth()), imp.getType(), 1.1f);
                        // cheap and fast nearest-point resizing
                        imp = new ImagePlus(imp.getTitle(), imp.getProcessor().resize(1024));
                    }
                    if (-1 == type)
                        type = imp.getType();
                    final ImageStatistics i_st = imp.getStatistics();
                    // order by stdDev, from small to big
                    int q = 0;
                    for (final ImageStatistics st : al_st) {
                        q++;
                        if (st.stdDev > i_st.stdDev)
                            break;
                    }
                    if (q == al.size()) {
                        // append at the end. WARNING if importing thousands of images, this is a potential source of out of memory errors. I could just recompute it when I needed it again below
                        al_st.add(i_st);
                        al_p.add(pa[i]);
                    } else {
                        al_st.add(q, i_st);
                        al_p.add(q, pa[i]);
                    }
                }
                // shallow copy of the ordered list
                final ArrayList<Patch> al_p2 = new ArrayList<Patch>(al_p);
                // 2 - discard the first and last 25% (TODO: a proper histogram clustering analysis and histogram examination should apply here)
                if (pa.length > 3) {
                    // under 4 images, use them all
                    int i = 0;
                    while (i <= pa.length * 0.25) {
                        al_p.remove(i);
                        i++;
                    }
                    final int count = i;
                    i = pa.length - 1 - count;
                    while (i > (pa.length * 0.75) - count) {
                        al_p.remove(i);
                        i--;
                    }
                }
                // 3 - compute common histogram for the middle 50% images
                final Patch[] p50 = new Patch[al_p.size()];
                al_p.toArray(p50);
                final StackStatistics stats = new StackStatistics(new PatchStack(p50, 1));
                // 4 - compute autoAdjust min and max values
                // extracting code from ij.plugin.frame.ContrastAdjuster, method autoAdjust
                int autoThreshold = 0;
                double min = 0;
                double max = 0;
                // once for 8-bit and color, twice for 16 and 32-bit (thus the 2501 autoThreshold value)
                final int limit = stats.pixelCount / 10;
                final int[] histogram = stats.histogram;
                // else autoThreshold /= 2;
                if (ImagePlus.GRAY16 == type || ImagePlus.GRAY32 == type)
                    autoThreshold = 2500;
                else
                    autoThreshold = 5000;
                final int threshold = stats.pixelCount / autoThreshold;
                int i = -1;
                boolean found = false;
                int count;
                do {
                    i++;
                    count = histogram[i];
                    if (count > limit)
                        count = 0;
                    found = count > threshold;
                } while (!found && i < 255);
                final int hmin = i;
                i = 256;
                do {
                    i--;
                    count = histogram[i];
                    if (count > limit)
                        count = 0;
                    found = count > threshold;
                } while (!found && i > 0);
                final int hmax = i;
                if (hmax >= hmin) {
                    min = stats.histMin + hmin * stats.binSize;
                    max = stats.histMin + hmax * stats.binSize;
                    if (min == max) {
                        min = stats.min;
                        max = stats.max;
                    }
                }
                // 5 - compute common mean within min,max range
                final double target_mean = getMeanOfRange(stats, min, max);
                Utils.log2("Loader min,max: " + min + ", " + max + ",   target mean: " + target_mean);
                // 6 - apply to all
                for (i = al_p2.size() - 1; i > -1; i--) {
                    // the order is different, thus getting it from the proper list
                    final Patch p = (Patch) al_p2.get(i);
                    final double dm = target_mean - getMeanOfRange((ImageStatistics) al_st.get(i), min, max);
                    // displacing in the opposite direction, makes sense, so that the range is drifted upwards and thus the target 256 range for an awt.Image will be closer to the ideal target_mean
                    p.setMinAndMax(min - dm, max - dm);
                // OBSOLETE and wrong //p.putMinAndMax(fetchImagePlus(p));
                }
                setMipMapsRegeneration(true);
                if (isMipMapsRegenerationEnabled()) {
                    // recreate files
                    for (final Patch p : al) fus.add(regenerateMipMaps(p));
                }
                Display.repaint(layer, new Rectangle(0, 0, (int) layer.getParent().getLayerWidth(), (int) layer.getParent().getLayerHeight()), 0);
            // make picture
            // getFlatImage(layer, layer.getMinimalBoundingBox(Patch.class), 0.25, 1, ImagePlus.GRAY8, Patch.class, null, false).show();
            }
        }
        if (stitch_tiles) {
            // Wait until all mipmaps for the new images have been generated before attempting to register
            Utils.wait(fus);
            // create undo
            layer.getParent().addTransformStep(new HashSet<Displayable>(layer.getDisplayables(Patch.class)));
            // wait until repainting operations have finished (otherwise, calling crop on an ImageProcessor fails with out of bounds exception sometimes)
            if (null != Display.getFront())
                Display.getFront().getCanvas().waitForRepaint();
            if (null != worker)
                worker.setTaskName("Stitching");
            StitchingTEM.stitch(pa, cols.size(), bt_overlap, lr_overlap, true, pc_param).run();
        }
        // link with images on top, bottom, left and right.
        if (link_images) {
            if (null != worker)
                worker.setTaskName("Linking");
            for (int i = 0; i < pall.length; i++) {
                // 'i' is column
                for (int j = 0; j < pall[0].length; j++) {
                    // 'j' is row
                    final Patch p = pall[i][j];
                    // can happen if a slot is empty
                    if (null == p)
                        continue;
                    if (i > 0 && null != pall[i - 1][j])
                        p.link(pall[i - 1][j]);
                    if (i < pall.length - 1 && null != pall[i + 1][j])
                        p.link(pall[i + 1][j]);
                    if (j > 0 && null != pall[i][j - 1])
                        p.link(pall[i][j - 1]);
                    if (j < pall[0].length - 1 && null != pall[i][j + 1])
                        p.link(pall[i][j + 1]);
                }
            }
        }
        commitLargeUpdate();
        // resize LayerSet
        // int new_width = x;
        // int new_height = largest_y;
        // Math.abs(bx) + new_width, Math.abs(by) + new_height);
        layer.getParent().setMinimumDimensions();
        // update indexes
        // so its done once only
        layer.updateInDatabase("stack_index");
        // create panels in all Displays showing this layer
        /* // not needed anymore
Iterator it = al.iterator();
while (it.hasNext()) {
	Display.add(layer, (Displayable)it.next(), false); // don't set it active, don't want to reload the ImagePlus!
}
			 */
        // update Displays
        Display.update(layer);
        layer.recreateBuckets();
    // debug:
    } catch (final Throwable t) {
        IJError.print(t);
        rollback();
        setMipMapsRegeneration(true);
    }
}
Also used : ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) GenericDialog(ij.gui.GenericDialog) ZDisplayable(ini.trakem2.display.ZDisplayable) Displayable(ini.trakem2.display.Displayable) StackStatistics(ij.process.StackStatistics) ImagePlus(ij.ImagePlus) PatchStack(ini.trakem2.imaging.PatchStack) ImageStatistics(ij.process.ImageStatistics) Future(java.util.concurrent.Future) Patch(ini.trakem2.display.Patch) File(java.io.File)

Example 15 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class TMLHandler method endElement.

public void endElement(String namespace_URI, String local_name, String qualified_name) {
    if (null == loader)
        return;
    if (skip) {
        // reset
        skip = false;
        return;
    }
    String orig_qualified_name = qualified_name;
    // Utils.log2("endElement: " + qualified_name);
    // iterate over all open things and find the one that matches the qualified_name, and set it closed (pop it out of the list):
    qualified_name = qualified_name.toLowerCase().trim();
    if (0 == qualified_name.indexOf("t2_")) {
        qualified_name = qualified_name.substring(3);
    }
    for (int i = al_open.size() - 1; i > -1; i--) {
        Thing thing = al_open.get(i);
        if (thing.getType().toLowerCase().equals(qualified_name)) {
            al_open.remove(i);
            break;
        }
    }
    if (null != last_annotation && null != last_displayable) {
        last_displayable.setAnnotation(last_annotation.toString().trim().replaceAll("&lt;", "<"));
        last_annotation = null;
    }
    // terminate non-single clause objects
    if (orig_qualified_name.equals("t2_node")) {
        // Remove one node from the stack
        nodes.removeLast();
        taggables.removeLast();
    } else if (orig_qualified_name.equals("t2_connector")) {
        if (null != last_connector) {
            tree_root_nodes.put(last_connector, last_root_node);
            last_root_node = null;
            last_connector = null;
            last_tree = null;
            nodes.clear();
        }
        last_displayable = null;
    } else if (orig_qualified_name.equals("t2_area_list")) {
        last_area_list = null;
        last_displayable = null;
    } else if (orig_qualified_name.equals("t2_area")) {
        if (null != reca) {
            if (null != last_area_list) {
                // it's local
                last_area_list.addArea(last_area_list_layer_id, reca.getArea());
            } else {
                ((AreaTree.AreaNode) nodes.getLast()).setData(reca.getArea());
            }
            reca = null;
        }
    } else if (orig_qualified_name.equals("ict_transform_list")) {
        ct_list_stack.remove(ct_list_stack.size() - 1);
    } else if (orig_qualified_name.equals("t2_patch")) {
        if (last_patch_filters.size() > 0) {
            last_patch.setFilters(last_patch_filters.toArray(new IFilter[last_patch_filters.size()]));
        }
        if (null != last_ct) {
            last_patch.setCoordinateTransformSilently(last_ct);
            last_ct = null;
        } else if (!last_patch.checkCoordinateTransformFile()) {
            Utils.log("ERROR: could not find a file for the coordinate transform #" + last_patch.getCoordinateTransformId() + " of Patch #" + last_patch.getId());
        }
        if (!last_patch.checkAlphaMaskFile()) {
            Utils.log("ERROR: could not find a file for the alpha mask #" + last_patch.getAlphaMaskId() + " of Patch #" + last_patch.getId());
        }
        last_patch = null;
        last_patch_filters.clear();
        last_displayable = null;
    } else if (orig_qualified_name.equals("t2_ball")) {
        last_ball = null;
        last_displayable = null;
    } else if (orig_qualified_name.equals("t2_dissector")) {
        last_dissector = null;
        last_displayable = null;
    } else if (orig_qualified_name.equals("t2_treeline")) {
        if (null != last_treeline) {
            // old format:
            if (null == last_root_node && null != last_treeline_data && last_treeline_data.length() > 0) {
                last_root_node = parseBranch(Utils.trim(last_treeline_data));
                last_treeline_data = null;
            }
            // new
            tree_root_nodes.put(last_treeline, last_root_node);
            last_root_node = null;
            // always:
            last_treeline = null;
            last_tree = null;
            nodes.clear();
        }
        last_displayable = null;
    } else if (orig_qualified_name.equals("t2_areatree")) {
        if (null != last_areatree) {
            tree_root_nodes.put(last_areatree, last_root_node);
            last_root_node = null;
            last_areatree = null;
            last_tree = null;
            // the absence of this line would have made the nodes list grow with all nodes of all areatrees, which is ok but consumes memory
            nodes.clear();
        }
        last_displayable = null;
    } else if (orig_qualified_name.equals("t2_stack")) {
        if (null != last_ict) {
            last_stack.setInvertibleCoordinateTransformSilently(last_ict);
            last_ict = null;
        }
        last_stack = null;
        last_displayable = null;
    } else if (in(orig_qualified_name, all_displayables)) {
        last_displayable = null;
    }
}
Also used : AreaTree(ini.trakem2.display.AreaTree) LayerThing(ini.trakem2.tree.LayerThing) TemplateThing(ini.trakem2.tree.TemplateThing) ProjectThing(ini.trakem2.tree.ProjectThing) Thing(ini.trakem2.tree.Thing)

Aggregations

Loader (ini.trakem2.persistence.Loader)20 ImagePlus (ij.ImagePlus)13 Patch (ini.trakem2.display.Patch)11 FSLoader (ini.trakem2.persistence.FSLoader)11 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 LayerThing (ini.trakem2.tree.LayerThing)6 Rectangle (java.awt.Rectangle)6 ImageProcessor (ij.process.ImageProcessor)5 Layer (ini.trakem2.display.Layer)5 TemplateThing (ini.trakem2.tree.TemplateThing)5 ZDisplayable (ini.trakem2.display.ZDisplayable)4 ProjectThing (ini.trakem2.tree.ProjectThing)4 IllDefinedDataPointsException (mpicbg.models.IllDefinedDataPointsException)4 NotEnoughDataPointsException (mpicbg.models.NotEnoughDataPointsException)4 ImageStack (ij.ImageStack)3 GenericDialog (ij.gui.GenericDialog)3 Displayable (ini.trakem2.display.Displayable)3 LayerSet (ini.trakem2.display.LayerSet)3 LayerTree (ini.trakem2.tree.LayerTree)3