Batch Processing images with different z-stacks



Hi all,

I am new to ImageJ and here is my problem: I have a set of .ome.tiff images I would like to run this macro on:

run("Stack to Hyperstack...", "order=xyzct channels=4 slices=x frames=1 display=Color");
run("Split Channels");
run("Merge Channels...", "c1=[C3-imagename.ome.tiff] c2=[C2-imagename-0015.ome.tiff] create");
run("Subtract Background...", "rolling=50");
run("Detect Particles", "  two=[Detect in both channels independently] ch1a=4 ch1s=3 ch2a=4 ch2s=3 calculate max=2 plot add=Nothing");

I would like to apply this macro to all images individually via batch processing, however I am struggling with these problems:
1.) Each image consists of 4 separate fluorescent channels, 1 timepoint and with different amounts of z-stacks. How, when the macro runs Stack to Hyperstack, can I run this command across all images so that I end up with 4 channels and a varying number of slices?
2.) After I have split the channels, I only would like to run the background correction on 2 channels, and so I close the other two. How can I make this occur in each image?
3.) Same problem as 2.) I would like to merge the remaining two channels into one. How do I do this across all images?

Thanks for the help!



In general - you can use theBuilt-In Macro Functions page to help search for appropriate function calls… it’s super helpful!

For this you can use something like this to generalize that Stack to Hyperstack... call:

getDimensions(width, height, channels, slices, frames);
run("Stack to Hyperstack...", "order=xyzct channels=" + channels + " slices="+ slices + " frames=" + frames + " display=Color");

I have to run to a meeting… so I"ll give you a quick answer on these latter two questions… but will tell you the best thing to do to generalize these code calls is to concatenate strings… so using code calls like:

title = getTitle();
selectWindow("C1-" + title);
run("Merge Channels...", "c1=[C3-" + title +"] c2=[C2-" + title +"] create");

Hope this at least helps get you started! If you have more questions - just post again. We are here to help you!

eta :slight_smile:


Sorry for the late reply & thanks for the help, much appreciated!
When I try and run this code nothing happens, I’m wondering where I can input the number of channels I want (4) and where i can set the number of z-slices to the total number of slices/4. Sorry I’m not good at coding at all!



No worries. You can always brush up a bit and use these helpful links:

Essentially, the getDimensions() function call is asking for that current image’s dimensions… and those values are stored in those variables - called width, height, channels, slices, frames. Then those values can be used in the following run() call by concatenating the values held within those variables into the String of the settings/parameters for that call.

So when you say

What do you mean? What is the exact code you are calling at that moment? On what image (perhaps share it with us so we can better reproduce your scenario)?

For example, if I use the sample image, “Confocal Series”… by going File > Open Samples > Confocal Series (2.2MB) and then copy/paste this code:

getDimensions(width, height, channels, slices, frames); 
run("Stack to Hyperstack...", "order=xyczt(default) channels="+channels+" slices="+slices+" frames="+frames+" display=Color");

into the Script Editor and go to Language > IJ1 Macro - then run it. It runs. You can always test it by adding print() statements after… such as:

print("The number of channels is " + channels +".");
print("The number of slices is " + slices + ".");

To be sure the values within those variables are what you expect, etc.



Thanks for the quick response! I will have a look through those links now. I just ran the above code on my images and it returns the correct number of channels and slices. Now I would like to split those images into four channels. I am thinking to use the Stack.setDimensions(channels, slices, frames) function?
Attached is a link for one of the files on Dropbox


Then all you need to do @mflee after those lines is to run("Split Channels");. Then it’s done.

Perhaps it’s best to take a step back and better describe exactly what you wish to do with this macro code - step-by-step. Perhaps things can be better implemented/streamlined??

But before you start going down that road… read those links I sent you above. I’m sure after going through them - you’ll feel more confident to handle things.

As always though - you can ask us more questions if you need. We are here to help.

eta :slight_smile:


Thanks for all the help! I was able to batch process my files in the end after familiarizing myself with everything!