Use to advance the slide
Let's consider re-usability and generalization
Output
[File > New > Script...]
) and start with an ImageJ macro template:
[Templates > ImageJ 1.x > Batch > Process Folder (ImageJ macro)]
[Templates > ImageJ 1.x > Batch > Process Folder (IJ1 macro)]
#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}
#@
at the top of a macro define script parameters for declaring inputs/output dialogs (Fiji, ImageJ2 only)
#@ Type variable_name
()
after the type, e.g. (label="File to open")
#@ output Type variable_name
can be used to generate outputRun the template macro on the data folder. Does it do what you expect?
i.e. you shouldn't have all of the image open at the end
#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+File.separator+file);
print(nSlices);
close();
}
#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+ File.separator +file);
print(nSlices);
run("Duplicate...", " ");
saveAs("Tiff", output+File.separator+File.nameWithoutExtension+"_slice1" + suffix);
close();
close();
//or close("*")
}
#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+ File.separator +file);
print(nSlices);
setSlice(5);
run("Duplicate...", " ");
run("Subtract Background...", "rolling=20");
saveAs("tiff", output+File.separator+File.nameWithoutExtension+"_slice5"+ suffix);
close("*");
}
#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+ File.separator +file);
print(nSlices);
for (i=1; i<=nSlices; i++){
setSlice(i);
run("Duplicate...", " ");
run("Subtract Background...", "rolling=20");
// Note: if you change the file type, e.g. to PNG, saveAs will yield the new name as File.nameWithoutExtension
// So then you will want to store the initial File.nameWithoutExtension in a variable before this loop
saveAs("tiff", output+File.separator+File.nameWithoutExtension+"_slice"+i+suffix);
close();
}
close();
}
#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+ File.separator +file);
print(nSlices);
for (i=1; i<=nSlices; i++){
setSlice(i);
run("Duplicate...", " ");
run("Subtract Background...", "rolling=20");
// Note: if you change the file type, e.g. to PNG, saveAs will yield the new name as File.nameWithoutExtension
// So then you will want to store the initial File.nameWithoutExtension in a variable before this loop
saveAs("tiff", output+File.separator+File.nameWithoutExtension+"_slice"+i+suffix);
close();
}
run("Z Project...", "start=3 stop=7 projection=[Average Intensity]");
run("Set Measurements...", "mean redirect=None decimal=7");
run("Measure");
saveAs("tiff", output+File.separator+File.nameWithoutExtension+"_projection_BackSubtract" + suffix);
close("*");
}
// Important: this is outside the processFile function!
saveAs("Results", output+File.separator+"MeanIntensity.csv");
.ijm
macro file onto the Fiji bar OR use [Plugins > Macros > Edit...]
run
[Plugins > Macros > Run...]
won't work for macros with Script Parameters[Plugins > Install...]
to install a macro into the Plugins menu (bottom section)
plugins
folder inside your Fiji installation
Fiji/plugins
, e.g. "ZZ My Macros"
to collect your macros and have easy accessFiji/plugins/Macros
(or make a folder there) to make it appear at bottom of [Plugins > Macros]Note: For the menus, the file name must have an _
e.g. Process_Folder.ijm
or _MyMacro.ijm