How to create substacks from images!

Hi All,

I am new in imagej world. Suppose we have images labeled as; 1, 2, 3,4,5,6,7. What I want to do is to create substacks so that I have;

Stack1 has images: 1,2,3
Stake2 has images: 3,4,5
Stack3: 5,6,7

so each stack (stack2 onwards) should start with the last image in the previous stack

I have tried to use ‘make substacks’ and also ‘split stack’ but the problem was that these commands have sorted the data in order as follows;
Stack1 has images: 1,2,3
Stake2 has images: 4,5,6
Stack3: 7,8,…

Your help is highly appreciated.
Many thanks

You can create substacks from your stack (Menu: Image->Stacks->Tools-> Make Substack…) by entering a list in the dialog, see screenshot below:

In additon you can automatize this with a macro created in the macro recorder if you have several stacks (replace the stack title in the macro below with your window title!):

run("MRI Stack (528K)");
selectWindow("mri-stack.tif");
run("Make Substack...", "  slices=1,2,3");
selectWindow("mri-stack.tif");
run("Make Substack...", "  slices=3,4,5");
selectWindow("mri-stack.tif");
run("Make Substack...", "  slices=5,6,7");

Please don’t forget to select the original stack window if you create a substack manually!

2 Likes

Many Thanks Bio7.

I have tried to use substack maker but I couldn’t get the right answer.

in fact I have 1500 Images, but let us suppose that we have a stack which consists of 9 slices (images) 1 to 9, in this case we want to produce 4 substacks from the orginal stack which contains 9 images automatically so that;

substack 1 has: images 1,2,3
substack 2 has: images 3,4,5
substack 3 has: images 5,6,7
substack 4 has: images 7,8,9

so how can I define the range in substack maker slice box above (a snapshot you provided) ?
Regards
A

Did you try the macro that @Bio7 posted above? It calls the Substack Maker several times, to accomplish what you describe.

Here is a slightly modified macro which you can execute with the script editor (Menu: Plugins->New->Macro).
Open you image and then execute the script.

See: http://imagej.net/Scripting

title=getTitle();
selectWindow(title);
run("Make Substack...", "  slices=1,2,3");
selectWindow(title);
run("Make Substack...", "  slices=3,4,5");
selectWindow(title);
run("Make Substack...", "  slices=5,6,7");
selectWindow(title);
run("Make Substack...", "  slices=7,8,9");

If your 1500 images are stacks then you could extend this macro to whatever image analysis sequence.

Use the macro recorder to record your commands, see:

Then you can apply the polished macro with the Process->Batch->Macro to all your images, see:

(You have to extend this macro to save the substacks and close them before you open the next image else you get a lot of open images).

1 Like

Thanks,
I just use 1 to 9 images as an example. I have about 1500 images in each run of expriment (around 15 expriments).

It is not practical to do this manually.

I have extracted 905 images (slices) in one single stack and the question is that is there any automatic way to convert this single stack into substakes so that in each substack we have only 3 images (slices) with the order i have already described before.

Then extend this macro with a loop to extract the slices, save and close them.

1 Like

Something like this:

title=getTitle();
s = 1;
while (s <= nSlices) {
  selectWindow(title);
  slices=""+s+","+(s+1)+","+(s+2);
  run("Make Substack...", "  slices=" + slices);
  save(title + "-" + slices + ".tif");
  close();
  s += 2;
}

I did not test, but this should get you close.

1 Like

Many thanks to all for your valuable contribution.

ctrueden- Thanks a lot, I will try your code and get back with feedback.

Hi ctrueden,

I have tried to run your code but I got the following error message:

invalid input string
"11"

Any suggestions!

Many thanks

There was a minor error in the original macro:

should be:

while (s <= nSlices-2) {

I am doing the similar thing but stuck with coding. I have a 288-slice stack and want to create substacks from it. The substack contains 4 slides, for example, s1, s55, s109, s163 for substack 1, s2, s56, s110, s164 for substack 2 and so on. Could you help me how to write the loop? Thanks a lot.

It looks like you want to grab slices at a 54-slice interval. Not sure how many substacks you want, but I’ll guess you want 54.

Make Substack supports intervals and you can find the syntax using the Macro Recorder.

To get the first substack the command would be:

run("Make Substack...", " slices=1-164-54");

To generalize it you substitute with variables:

run("Make Substack...", " slices="+startSlice+"-"+endSlice+"-54");

Then you can put it in a loop with @ctrueden’s code :

// input:  stack with at least 216 slices
// extracts 54 substacks consisting of 4 slices at 54-slice intervals
title=getTitle();
path = getDirectory("image");
s = 1;
while (s <= 54) {
	startSlice = s;
	endSlice = s + 163;
	selectWindow(title);
	run("Make Substack...", "  slices="+startSlice+"-"+endSlice+"-54");
        saveAs("Tiff", path + title + "- substack " + s + ".tif");
        close();
	s++;
}

Thank you for your feedback but I already figured out the loop.

Glad to hear it. Maybe you could post your code to help other forum readers?

I just modified your original code (see below):

title=getTitle();
s = 1;
t = nSlices();
i = t/4;
while (s <= i) {
  selectWindow(title);
  slices=""+s+","+(s+i)+","+(s+2*i)+","+(s+3*i);
  run("Make Substack...", "  slices=" + slices);
  save( title + "-" + slices + ".tif");
  close();
  s += 1;
}
1 Like

Sorry, the original code was from ctrueden.

1 Like

I don’t know if anyone is still following this thread, but I’m trying to downsample images from a TIFF stack, but I can’t get the macro to run. I’ve borrowed bits of this code, but it isn’t quite working out. Ideally, I’d like a dialog box that lets me select a folder (I tried using the batch processing, but that didn’t work either).

input = "E:\My_FullDirectory\";
output = "E:\My_FullDIrectory_to_outputs\";

setBatchMode(true); 
list = getFileList(input);
for (i = 0; i < list.length; i++)
        action(input, output, list[i]);
setBatchMode(false);

function action(input, output, filename) {
        open(input + filename);
        run("Make Substack...", "  slices=1-60-5");
        saveAs("TIFF", output + filename + "reduced");
        close();
}