Search in sources :

Example 1 with ImageFileFilter

use of ini.trakem2.io.ImageFileFilter in project TrakEM2 by trakem2.

the class DNDInsertImage method importImageFile.

private boolean importImageFile(File f, String path, Point point) throws Exception {
    if (f.exists()) {
        final Layer layer = display.getLayer();
        Bureaucrat burro = null;
        if (f.isDirectory()) {
            // ask:
            GenericDialog gd = new GenericDialog("Import directory");
            String[] choice = new String[] { "Stack", "Grid", "Sequence as grid" };
            gd.addChoice("Directory as: ", choice, choice[0]);
            gd.showDialog();
            if (gd.wasCanceled()) {
                // the user cancel it, so all is ok.
                return true;
            }
            display.getLayerSet().addLayerContentStep(layer);
            switch(gd.getNextChoiceIndex()) {
                case // as stack
                0:
                    // if importing image sequence as a stack:
                    // don't filter by name  "^[^\\.].*[\\.][a-zA-Z1-9_]{3,4}$"
                    String[] names = f.list(new ImageFileFilter());
                    Utils.log2("stack size: " + names.length);
                    for (int i = 0; i < names.length; i++) {
                        Utils.log2(names[i]);
                    }
                    Arrays.sort(names);
                    // I don't care about the dimensions
                    VirtualStack stack = new VirtualStack(10, 10, null, f.getAbsolutePath().replace('\\', '/'));
                    for (int k = 0; k < names.length; k++) {
                        IJ.redirectErrorMessages();
                        // ignore trakem2 files
                        if (names[k].toLowerCase().endsWith(".xml"))
                            continue;
                        stack.addSlice(names[k]);
                    }
                    if (stack.getSize() > 0) {
                        burro = display.getProject().getLoader().importStack(layer, point.x, point.y, new ImagePlus("stack", stack), true, path, false);
                    }
                    break;
                case // as grid
                1:
                    burro = display.getProject().getLoader().importGrid(layer, path);
                    break;
                case // sequence as grid
                2:
                    burro = display.getProject().getLoader().importSequenceAsGrid(layer, path);
                    break;
            }
        } else {
            layer.getParent().addLayerContentStep(layer);
            // single image file (single image or a stack)
            burro = display.getProject().getLoader().importImage(layer, point.x, point.y, path, false);
        }
        if (null != burro) {
            burro.addPostTask(new Runnable() {

                public void run() {
                    // The current state
                    layer.getParent().addLayerContentStep(layer);
                }
            });
        }
        return true;
    } else {
        Utils.log("File not found: " + path);
        return false;
    }
}
Also used : GenericDialog(ij.gui.GenericDialog) VirtualStack(ij.VirtualStack) Layer(ini.trakem2.display.Layer) ImageFileFilter(ini.trakem2.io.ImageFileFilter) ImagePlus(ij.ImagePlus) Point(java.awt.Point)

Example 2 with ImageFileFilter

use of ini.trakem2.io.ImageFileFilter in project TrakEM2 by trakem2.

the class Loader method importGrid.

/**
 * Import a grid of images and put them in the layer. If the directory (@param dir) is null, it'll be asked for.
 */
public Bureaucrat importGrid(final Layer layer, String dir) {
    try {
        String file = null;
        if (null == dir) {
            final String[] dn = Utils.selectFile("Select first image");
            if (null == dn)
                return null;
            dir = dn[0];
            file = dn[1];
        }
        // char digit digit
        String convention = "cdd";
        boolean chars_are_columns = true;
        // examine file name
        /*
		if (file.matches("\\A[a-zA-Z]\\d\\d.*")) { // one letter, 2 numbers
			//means:
			//	\A		- beggining of input
			//	[a-zA-Z]	- any letter upper or lower case
			//	\d\d		- two consecutive digits
			//	.*		- any row of chars
			ini_grid_convention = true;
		}
		*/
        // ask for chars->rows, numbers->columns or viceversa
        final GenericDialog gd = new GenericDialog("Conventions");
        gd.addStringField("file_name_contains:", "");
        gd.addNumericField("base_x: ", 0, 3);
        gd.addNumericField("base_y: ", 0, 3);
        gd.addMessage("Use: x(any), c(haracter), d(igit)");
        gd.addStringField("convention: ", convention);
        final String[] cr = new String[] { "columns", "rows" };
        gd.addChoice("characters are: ", cr, cr[0]);
        gd.addMessage("[File extension ignored]");
        // as asked by Joachim Walter
        gd.addNumericField("bottom-top overlap: ", 0, 3);
        gd.addNumericField("left-right overlap: ", 0, 3);
        gd.addCheckbox("link_images", false);
        gd.addCheckbox("montage with phase correlation", false);
        gd.addCheckbox("homogenize_contrast", true);
        final Component[] c = { (Component) gd.getSliders().get(gd.getSliders().size() - 2), (Component) gd.getNumericFields().get(gd.getNumericFields().size() - 2), (Component) gd.getSliders().get(gd.getSliders().size() - 1), (Component) gd.getNumericFields().get(gd.getNumericFields().size() - 1), (Component) gd.getChoices().get(gd.getChoices().size() - 1) };
        // enable the checkbox to control the slider and its associated numeric field:
        Utils.addEnablerListener((Checkbox) gd.getCheckboxes().get(gd.getCheckboxes().size() - 1), c, null);
        // gd.addCheckbox("Apply non-linear deformation", false);
        gd.showDialog();
        if (gd.wasCanceled()) {
            return null;
        }
        // collect data
        // filter away files not containing this tag
        final String regex = gd.getNextString();
        // the base x,y of the whole grid
        final double bx = gd.getNextNumber();
        final double by = gd.getNextNumber();
        // if (!ini_grid_convention) {
        convention = gd.getNextString().toLowerCase();
        // }
        if (/*!ini_grid_convention && */
        (null == convention || convention.equals("") || -1 == convention.indexOf('c') || -1 == convention.indexOf('d'))) {
            // TODO check that the convention has only 'cdx' chars and also that there is an island of 'c's and of 'd's only.
            Utils.showMessage("Convention '" + convention + "' needs both c(haracters) and d(igits), optionally 'x', and nothing else!");
            return null;
        }
        chars_are_columns = (0 == gd.getNextChoiceIndex());
        final double bt_overlap = gd.getNextNumber();
        final double lr_overlap = gd.getNextNumber();
        final boolean link_images = gd.getNextBoolean();
        final boolean stitch_tiles = gd.getNextBoolean();
        final boolean homogenize_contrast = gd.getNextBoolean();
        // start magic
        // get ImageJ-openable files that comply with the convention
        final File images_dir = new File(dir);
        if (!(images_dir.exists() && images_dir.isDirectory())) {
            Utils.showMessage("Something went wrong:\n\tCan't find directory " + dir);
            return null;
        }
        final String[] file_names = images_dir.list(new ImageFileFilter(regex, convention));
        if (null == file && file_names.length > 0) {
            // the 'selected' file
            file = file_names[0];
        }
        Utils.showStatus("Adding " + file_names.length + " patches.", false);
        if (0 == file_names.length) {
            Utils.log("Zero files match the convention '" + convention + "'");
            return null;
        }
        // How to: select all files, and order their names in a double array as they should be placed in the Display. Then place them, displacing by offset, and resizing if necessary.
        // gather image files:
        final Montage montage = new Montage(convention, chars_are_columns);
        montage.addAll(file_names);
        // an array of Object[] arrays, of unequal length maybe, each containing a column of image file names
        final ArrayList<String[]> cols = montage.getCols();
        // !@#$%^&*
        final String dir_ = dir;
        final double bt_overlap_ = bt_overlap;
        final double lr_overlap_ = lr_overlap;
        final String file_ = file;
        return Bureaucrat.createAndStart(new Worker.Task("Insert grid", true) {

            @Override
            public void exec() {
                StitchingTEM.PhaseCorrelationParam pc_param = null;
                if (stitch_tiles) {
                    pc_param = new StitchingTEM.PhaseCorrelationParam();
                    pc_param.setup(layer);
                }
                insertGrid(layer, dir_, file_, file_names.length, cols, bx, by, bt_overlap_, lr_overlap_, link_images, stitch_tiles, homogenize_contrast, pc_param, this);
            }
        }, layer.getProject());
    } catch (final Exception e) {
        IJError.print(e);
    }
    return null;
}
Also used : ImageFileFilter(ini.trakem2.io.ImageFileFilter) IOException(java.io.IOException) FormatException(loci.formats.FormatException) GenericDialog(ij.gui.GenericDialog) Montage(ini.trakem2.utils.Montage) Worker(ini.trakem2.utils.Worker) Component(java.awt.Component) File(java.io.File)

Aggregations

GenericDialog (ij.gui.GenericDialog)2 ImageFileFilter (ini.trakem2.io.ImageFileFilter)2 ImagePlus (ij.ImagePlus)1 VirtualStack (ij.VirtualStack)1 Layer (ini.trakem2.display.Layer)1 Montage (ini.trakem2.utils.Montage)1 Worker (ini.trakem2.utils.Worker)1 Component (java.awt.Component)1 Point (java.awt.Point)1 File (java.io.File)1 IOException (java.io.IOException)1 FormatException (loci.formats.FormatException)1