Pairwise stitching macro

We have a LIF file with 5 timepoints, each containing 2 images with 2 channels each. The files are named lexicographically in this order:

  1. timepoint 1, top
  2. timepoint 1, bottom
  3. timepoint 2, top
  4. timepoint 2, bottom

We’re trying to write a macro (below) to stitch each pair of consecutive images and save the channels separately.
We’re having trouble selecting each channel to save it individually. We see that there’s a selectWindow function… how do we use it in our script? How do we always know the name of the window?

More generally, we’re not sure we’re even going about this the correct way at all. Is there a better approach or simplification? (We’d also much prefer to use Scala, Groovy, Java, Python, or Javascript but struggled to learn the APIs.)

Thanks!

setBatchMode(true);

input_file = File.openDialog("Select an input file");
target_dir = getDirectory("Select a target directory");

run("Bio-Formats Macro Extensions");
Ext.setId(input_file);
Ext.getCurrentFile(file);
Ext.getSeriesCount(seriesCount);

for (i = 0; i < seriesCount / 2 + 1; i++) {

	run("Bio-Formats Importer", "open=&input_file color_mode=Default view=Hyperstack stack_order=XYCZT series_" + 2*i+1);
	imgName1 = getTitle();
	run("Bio-Formats Importer", "open=&input_file color_mode=Default view=Hyperstack stack_order=XYCZT series_" + 2*i);
	imgName2 = getTitle();

	setMinAndMax(0, 4096);
	getVoxelSize(width, height, depth, unit);

	run("Pairwise stitching", "first_image='"+imgName1+"' second_image='"+imgName2+"' fusion_method=[Linear Blending] check_peaks=5 compute_overlap x=0 y=0 z=0 registration_channel_image_1=[Average all channels] registration_channel_image_2=[Average all channels]");

	setVoxelSize(width, height, depth, unit);

	run("Add...", "value=1");

	run("Split Channels");

    // The following only apply to one channel! We need to apply these to both

	run("Reverse");
	run("Nrrd ... ", imgName1 + "-" + imgName2 + ".nrrd");
  //saveAs("Tiff", target_dir + toString(i) + ".nrrd");

}
setBatchMode(false);

Bump…
We still haven’t resolved this ourselves.

Well, you are running in batchMode. I can’t see the saveAs function in your macro, but when it’s located in the batchMode = on part, than it’s impossible to select a window (because there isn’t any). You can solve this problem with the functions getImageID(), selectImage(id) and close().