I have just started using the macros and scripts in ImageJ. Have been trying to analyze about 100 data sets, each contain several 100 images.
There was a script in the ImageJ to analyze all the images in one folder. But I needed something for 100 folders. I also needed to save the analyzed stacks of images in new folders in a different path, after creating the folders by the script. In other word, the script is generalization of the ‘Batch Processing’. I used the ImageJ script for one folder and attempted to do the generalization. Here I am posting the code. Have written descriptions on the code. But one general point is that a Directory (Dir) contains several Folders; and each Folder contains several images. Hope it is helpful.
//A directory contains several folders; and each folder is a stack of images.
//User chooses the input direcotry path. The code analyzes all the images in the folders of the directory.
//It creates folders with analyzed images in the output direcotry which has been chosen by the user.
//The created folders and images have the same name as the input folders and images, respectively.
//The analysis in this specific case is segmentation of images by SRM and coversion of images to 8bit before saving.
//This part of the script - the two lines starting with ‘run’ in processFile function - can be replaced by any other analysis.
inputDir = getDirectory(“choose the input directory”);
outputDir = getDirectory(“choose the output directory”);
processDir(inputDir, outputDir);
//inputDir (direcotry) is a collection of Folders.
//The function processDir deals with processing every single Folder in the Dir and writes the results to outputDir.
//The inputDir and outputDir paths are given by the user.
//The Folders inside outputDir are created by processDir with the same names for equivalent Folders in inputDir.
function processDir(inputDir, outputDir) {
listdir = getFileList(inputDir);
for (j = 0; j < listdir.length; j++) {
print("Processing: " + listdir[j]);
File.makeDirectory(outputDir + listdir[j]);
outputFolder = outputDir + listdir[j];
inputFolder = inputDir + listdir[j];
setBatchMode(true);
processFolder(inputFolder);
setBatchMode(false);
}
}
//processFolder function manipulates a stack of images in a folder.
//It runs the processFile function for every image in the folder.
function processFolder(inputFolder) {
list = getFileList(inputFolder);
for (i = 0; i < list.length; i++) {
processFile(inputFolder, outputFolder, list[i]);
}
}
//processFile function processes single images.
//In this case the images are segmented by SRM and converted to 8bit.
//This part of code - the two lines starting with ‘run’ - can be replaced by other processes.
function processFile(inputFolder, outputFolder, file) {
print("Processing: " + inputFolder + file);
open(inputFolder + file);
run(“Statistical Region Merging”, “q=100 showaverages”);
run(“8-bit”);
print("Saving to: " + outputFolder);
saveAs(“TIFF”, outputFolder+file);
close();
}