Complete novice - how to execute batch script or macro

Hello all!

Just starting out with some very basics with ImageJ scripting, and any help would be much appreciated. I am acquiring confocal microscopy images in three channels, each containing several Z-stacks, and saving them as .lsm files. For downstream analysis of images, this is what I would normally do (for each individual file):

  • Import using Bio-Formats, with “Split Channels” option selected
  • This opens up three separate windows, each maintaining the original file name, but with the additional tags “…C=0”, “…C=1”, and “…C=2” for each of the separate acquisition channel windows
  • For each separate channel window, I would carry out a Max Intensity Z-projection, and save the resultant single image file with its original name, but with a prefix and/or suffix indicating that it’s a Max Intensity projection

Eventually what I would like to accomplish is carry out the above, but in batch to a large number of files in a specific folder.

I found a good starting point script from one of the tutorials, and have attempted to edit it to make it work for my intents and purposes, but I’m hung up fairly early in the process! Here is what I’ve attempted:

#@File(label = “Input directory”, style = “directory”) input
#@File(label = “Output directory”, style = “directory”) output
#@String(label = “File suffix”, value = “.lsm”) suffix

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
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) {

run(“Bio-Formats”, “open=[” + input + “/” + file +"] autoscale color_mode=Default rois_import=[ROI manager] split_channels view=Hyperstack stack_order=XYCZT");
id = getImageID(); // get original image id

// To carry out Max intensity Z projections
selectWindow("*C=0");

My thought process with the very last line was based on what I normally do with an individual file, but obviously using the wildcard “*C=0” since that’s the suffix that is by default tagged at the end of the file name for the first channel (and because I eventually want to do this on separate files, each with a different name), and it’s also the command that’s indicated if I go through the process manually while the Recorder window is open. But if I run the script as is, it stops after opening the three separate channel windows of the first image and gives Macro Error “No window with the title “*C=0” found.”. But clearly there is a window containing “C=0” in its title?!

Any pointers on how I can proceed with attempting to build up this script? Or is this an altogether illogical approach?!

Thanks in advance!

I’m not sure the wildcard works like that in a string: https://stackoverflow.com/questions/29776100/wildcard-character-in-imagej-macros

You might try something like Wayne’s post here:
http://imagej.1557.x6.nabble.com/Get-windows-titles-td3700495.html

Hi @hafalt,

Welcome to the forum.

A way you could to do this is to utilise a for loop to go through the open images, get their titles and find the one that matches your expression like below.

for (i=1; i<=nImages(); i++) {
    selectImage(i);
    current = getTitle();
    if (matches(current, ".*C=0")){
    	print("Yes");
    }   else {
    	print("No");
    }
 }

BUT I would say that usually what I do is not split channels using Bio-Formats but open the image, get the title of the unsplit image, split the channels and then use the resultant pattern (in this case the channel siginifier becomes a prefix rather than suffix and it starts at 1 instead of 0) to select whatever channel I want, like below.

run(“Bio-Formats”, “open=[” + input + “/” + file +"] autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
title = getTitle();
run("Split Channels");
selectWindow("C1-" + title);

Hope that helps.

1 Like

Thanks all for the suggestions,

I found a seemingly simpler way to handle that as described in this previous post

For whatever reason, it seems that the way I was scripting the Bio-formats Import function was causing some unnecessary grief, although it worked fine if I wanted to make a script to handle single plane images?!