Tsum
Tsum

Reputation: 1

How do I batch process Z-stacks or all open images on ImageJ/Fiji?

I've really only needed the basics of ImageJ so far, but now I have Z-stacks with 400-500 raw images in each stack. They're already separated by colour, but I can't seem to get a macro to recognize the Z-stack file, convert stack to images, and then apply colour and scale bar to all the images and save each individual image as a png.

I've cobbled together a macro that will do so after I've manually opened a Z-stack file and already converted the Stack to Images, but the macro will only run on every other image? So I end up running it multiple times on the same batch because it will do image 1, 3, 5,..., and then next run 2, 6, 10,..., and etc., but I have no idea why. I've also tried it for i=1 and selectImage(i) or (i+1) but same thing happens..

/*
 * Macro template to process multiple open images
 */
output = getDirectory("output folder for results");
#@ File(label = "Output directory", style = "directory") output
#@ String(label = "Title contains") pattern

processOpenImages();

/*
 * Processes all open images. If an image matches the provided title
 * pattern, processImage() is executed.
 */
function processOpenImages() {
    n = nImages;
    setBatchMode(true);
    for (i=0; i<n; i++) {
        selectImage(i+1);
        imageTitle = getTitle();
        imageId = getImageID();
        if (matches(imageTitle, "(.*)"+pattern+"(.*)"))
            processImage(imageTitle, imageId, output);
    }
    setBatchMode(false);
}

/*
 * Processes the currently active image. Use imageId parameter
 * to re-select the input image during processing.
 */
function processImage(imageTitle, imageId, output) {
    // Do the processing here by adding your own code.
    title = getTitle();
    run("Green");
    run("Scale Bar...", "width=20 height=10 thickness=10 font=20 bold overlay");
    saveAs("PNG", output+title);
    close();
    // Leave the print statements until things work, then remove them.
    print("Processing: " + imageTitle);
    pathToOutputFile = output + File.separator + imageTitle + ".png";
    print("Saving to: " + pathToOutputFile);
}

Upvotes: 0

Views: 1015

Answers (0)

Related Questions