Search code examples
averageimagej-macro

How to average all files within a folder by image j macro


Hey I am new very new to programming in ImageJ macro, I would like to average all images in a folder, save that single average image in a separate folder


Solution

  • Presuming your images are logically ordered (e.g., image001, image002, image003 etc...) - try this:

    setBatchMode(true);
    
    //retrieve images from directory
    dir = getDirectory("Choose a directory of images...");
    list = getFileList(dir);
    
    for (i=0; i <list.length; i++) {
        path = dir + list[i];
        open(path); 
    }
    run("Images to Stack", "name=Stack title=[] use");
    stackImage = getTitle();
    
    //make an average intensity image
    run("Z Project...", "projection=[Average Intensity]");
    
    //save out to new folder
    outputPath = dir + File.separator + "separateFolder";
    if (!File.exists(outputPath)) File.makeDirectory(outputPath);
    newPath = outputPath + File.separator + "averagedImage";
    run("Save","save=[newPath]");
    close(stackImage);
    close();
    
    setBatchMode(false);