macro "Batch_Zooms" { Conc = false; Montage = false; // setBatchMode("true"); stackID = getImageID(); stackTitle = getTitle(); nROI = roiManager("count"); ConcString = "open"; for (r = 0; r < nROI; r++) { // loop on ROIs in the ROI manager selectImage(stackID); roiManager("select", r); if (Roi.getType == "polygon" || Roi.getType == "freehand") { // case of a rotated rectangle (after rotating a rectange ROI or using the specific tool) inID = getImageID(); inTitle = getTitle(); inLabel = getInfo("slice.label"); getDimensions(w, h, ch, sl, fr); Stack.getPosition(currch, currsl, currfr); setBatchMode(true); // Get angle of ROI from the ellipse fit (between -90° and 90°) angle = getValue("Angle"); if (angle > 90) angle = angle-180; angleString = toString(angle,0); //angle = getNumber("angle", angle); // Duplicate full image run("Select None"); run("Duplicate...", "duplicate slices=" + currsl); dupID = getImageID(); // Restore selection on input image and store it in the ROI manager selectImage(inID); run("Restore Selection"); // Restore ROI on duplicate image, addto overlay selectImage(dupID); run("Restore Selection"); run("Add Selection..."); // Enlarge duplicate image and rotate image by the ROI angle (will rotate the overlayed ROI too) w2 = 1.5 * w; h2 = 1.5 * h ; run("Canvas Size...", "width=" + w2 + " height=" + h2 + " position=Center"); run("Rotate... ", "angle=" + angle + " grid=1 interpolation=Bicubic"); // Activate the selection from overlay on image and crop Overlay.activateSelection(0); run("Crop"); // Remove overlay and selection from crop run("Select None"); run("Remove Overlay"); // Rename output image from the input image label or title (if no label) if (inLabel == "") outTitle1 = inTitle; else outTitle1 = inLabel; outTitle2 = replace(outTitle1, "-C=.", "_zrot" + angleString); if (outTitle2 == outTitle1)outTitle2 = replace(outTitle1, ".tif", "_zrot" + angleString + "_.tif"); if (outTitle2 == outTitle1) outTitle2 = outTitle1 + "_zrot" + angleString; rename(outTitle2); // Rename each slice if multicolor if (ch > 1) { for (c = 0; c < ch; c++) { Stack.setPosition((c+1), 1, 1); chLabel = getInfo("slice.label"); if (inLabel == "") outLabel1 = inTitle; else outLabel1 = chLabel; outLabel2 = replace(outLabel1, "-C=", "_zrot" + angleString + "-C="); if (outLabel2 == outLabel1)outLabel2 = replace(outLabel1, ".tif", "_zrot" + angleString + "_.tif"); if (outLabel2 == outLabel1) outLabel2 = outLabel1 + "_zrot" + angleString; Property.setSliceLabel(outLabel2); } } setBatchMode("exit and display"); } else if (Roi.getType == "rectangle") { // case of a rectangle ROI // Get info in input image and dupolicate the ROI inID = getImageID(); inTitle = getTitle(); inLabel = getInfo("slice.label"); getDimensions(w, h, ch, sl, fr); Stack.getPosition(currch, currsl, currfr); run("Duplicate...", "duplicate slices=" + currsl); // Rename output image from the input image label or title (if no label) if (inLabel == "") outTitle1 = inTitle; else outTitle1 = inLabel; outTitle2 = replace(outTitle1, "-C=.", "_zrot0"); if (outTitle2 == outTitle1)outTitle2 = replace(outTitle1, ".tif", "_zrot0_.tif"); if (outTitle2 == outTitle1) outTitle2 = outTitle1 + "_zrot0"; rename(outTitle2); // Rename each slice if multicolor if (ch > 1) { for (c = 0; c < ch; c++) { Stack.setPosition((c+1), 1, 1); chLabel = getInfo("slice.label"); if (inLabel == "") outLabel1 = inTitle; else outLabel1 = chLabel; outLabel2 = replace(outLabel1, "-C=", "_zrot0-C="); if (outLabel2 == outLabel1)outLabel2 = replace(outLabel1, ".tif", "_zrot0_.tif"); if (outLabel2 == outLabel1) outLabel2 = outLabel1 + "_zrot0"; Property.setSliceLabel(outLabel2); } } } ConcString = ConcString + " image" + (r+1) + "=[" + outTitle2 + "]"; } // Concatenate all zoom images if option is chosen if (Conc == true) { // Close the input stack (to avoid concatenating it with the zooms) selectImage(stackID); run("Select None"); Stack.setPosition(1, 1, 1); // close(); run("Concatenate...", "title=" + stackTitle + "_zooms " + ConcString); if (Montage == true) { run("Make Montage...", "columns=1 rows=" + nROI + " scale=1 border=10"); } } // setBatchMode("exit and display");; }