Having trouble with Run (Concatenate . . . using images from list = getList("image.titles")

This macro (see the entire code at the end of this post) starts by chopping a stack into a user defined number of parts. In my test .tiff file i have been chopping, I start with 2 channels and 2 slices per channel (to simplify everything). This creates 16 new, open image windows with unique names that can be sorted numerically (the original Tiff is closed). In the second part of this macro, I create an ordered list of these image titles. I have put in place several print functions to see what is going wrong (you can ignore those). What is strange is imag1 and imag2 are the correct 2 images to concatenate as confirmed by the print function but the Run concatenate actually chooses two different images to concatenate ignoring the original value of imag1 and imag2! I don’t understand why those values are not working once they are placed in the “Run Concatenate” function.

Test file I am using if anyone wants to try it out: MEG122820.lif - Series016-TESTFILE.tif (7.7 MB)

list = getList(“image.titles”);
print(“Image windows (not sorted):”);
for (i=0; i<list.length; i++) {
print(list[i]);
}
Array.sort(list);
if (list.length==0)
print(“No image windows are open”);
else {
print(“Image windows (sorted):”);
for (i=0; i<list.length; i++) {
print(list[i]);
}
}
imag1 = list[0];
imag2 = list[1];
print(“first 2”);
print (imag1);
print (imag2);

run(“Concatenate…”, " title=[Concatenated Stack] image1=[imag1] image2=[imag2] image3=[-- None --]");

list = getList(“image.titles”);
print(“Image windows (not sorted):”);
for (i=0; i<list.length; i++) {
print(list[i]);
}

Array.sort(list);
print(“Image windows (sorted):”);
for (i=0; i<list.length; i++) {
print(list[i]);
}

Sample image and/or code:

n = getNumber(“How many divisions (e.g., 2 means quarters)?”, 2);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
Stack.setPosition(1, 1, 1);
getDimensions(w, h, channels, slices, frames);
Stack.getPosition(channel, slice, frame);
print("Position (before): “+channel+”, “+slice+”, "+frame);
Stack.setPosition(channels, slices, frames);
Stack.getPosition(channel, slice, frame);
print("Position (after): “+channel+”, “+slice+”, "+frame);
Stack.setPosition(1, 1, 1);
for (a=1; a<=channels; a++) {
for (b=1; b<=slices; b++) {
Stack.setChannel(a);
wait(50);
Stack.setSlice(b);
wait(50);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
id = getImageID();
title = getTitle();
getLocationAndSize(locX, locY, sizeW, sizeH);
i=1;
for (y = 0; y < n; y++) {
offsetY = y * height / n;
for (x = 0; x < n; x++) {
offsetX = x * width / n;
selectImage(id);
call(“ij.gui.ImageWindow.setNextLocation”, locX + offsetX, locY + offsetY);
tileTitle = “[” + x + “,” + y + “]” + a + “_” + b + title;
run(“Duplicate…”, “title=” + tileTitle);
makeRectangle(offsetX, offsetY, tileWidth, tileHeight);
run(“Crop”);
rename(tileTitle);
i++;
}
}
selectImage(id);
}
}
close();

list = getList(“image.titles”);
print(“Image windows (not sorted):”);
for (i=0; i<list.length; i++) {
print(list[i]);
}
Array.sort(list);
if (list.length==0)
print(“No image windows are open”);
else {
print(“Image windows (sorted):”);
for (i=0; i<list.length; i++) {
print(list[i]);
}
}
imag1 = list[0];
imag2 = list[1];
print(“first 2”);
print (imag1);
print (imag2);

run(“Concatenate…”, " title=[Concatenated Stack] image1=[[0,0]1_1MEG122820.lif - Series016-TESTFILE.tif] image2=[[0,0]1_1MEG122820.lif - Series016-TESTFILE.tif] image3=[-- None --]");

list = getList(“image.titles”);
print(“Image windows (not sorted):”);
for (i=0; i<list.length; i++) {
print(list[i]);
}

Array.sort(list);
print(“Image windows (sorted):”);
for (i=0; i<list.length; i++) {
print(list[i]);
}

  • Upload an original image file here directly or share via a link to a file-sharing site (such as Dropbox) – (make sure however that you are allowed to share the image data publicly under the conditions of this forum).
  • Share a minimal working example of your macro code.

Background

  • What is the image about? Provide some background and/or a description of the image. Try to avoid field-specific “jargon”.

Analysis goals

  • What information are you interested in getting from this image?

Challenges

  • What stops you from proceeding?
  • What have you tried already?
  • Have you found any related forum topics? If so, cross-link them.
  • What software packages and/or plugins have you tried?

And I should mention that the shorter bit of code at the top is the part I am having trouble with. This is what my print log looks like:

Image windows (not sorted):
[0,0]1_1MEG122820.lif - Series016-TESTFILE.tif
[1,0]1_1MEG122820.lif - Series016-TESTFILE.tif
[0,1]1_1MEG122820.lif - Series016-TESTFILE.tif
[1,1]1_1MEG122820.lif - Series016-TESTFILE.tif
[0,0]1_2MEG122820.lif - Series016-TESTFILE.tif
[1,0]1_2MEG122820.lif - Series016-TESTFILE.tif
[0,1]1_2MEG122820.lif - Series016-TESTFILE.tif
[1,1]1_2MEG122820.lif - Series016-TESTFILE.tif
[0,0]2_1MEG122820.lif - Series016-TESTFILE.tif
[1,0]2_1MEG122820.lif - Series016-TESTFILE.tif
[0,1]2_1MEG122820.lif - Series016-TESTFILE.tif
[1,1]2_1MEG122820.lif - Series016-TESTFILE.tif
[0,0]2_2MEG122820.lif - Series016-TESTFILE.tif
[1,0]2_2MEG122820.lif - Series016-TESTFILE.tif
[0,1]2_2MEG122820.lif - Series016-TESTFILE.tif
[1,1]2_2MEG122820.lif - Series016-TESTFILE.tif
Image windows (sorted):
[0,0]1_1MEG122820.lif - Series016-TESTFILE.tif
[0,0]1_2MEG122820.lif - Series016-TESTFILE.tif
[0,0]2_1MEG122820.lif - Series016-TESTFILE.tif
[0,0]2_2MEG122820.lif - Series016-TESTFILE.tif
[0,1]1_1MEG122820.lif - Series016-TESTFILE.tif
[0,1]1_2MEG122820.lif - Series016-TESTFILE.tif
[0,1]2_1MEG122820.lif - Series016-TESTFILE.tif
[0,1]2_2MEG122820.lif - Series016-TESTFILE.tif
[1,0]1_1MEG122820.lif - Series016-TESTFILE.tif
[1,0]1_2MEG122820.lif - Series016-TESTFILE.tif
[1,0]2_1MEG122820.lif - Series016-TESTFILE.tif
[1,0]2_2MEG122820.lif - Series016-TESTFILE.tif
[1,1]1_1MEG122820.lif - Series016-TESTFILE.tif
[1,1]1_2MEG122820.lif - Series016-TESTFILE.tif
[1,1]2_1MEG122820.lif - Series016-TESTFILE.tif
[1,1]2_2MEG122820.lif - Series016-TESTFILE.tif
first 2
[0,0]1_1MEG122820.lif - Series016-TESTFILE.tif
[0,0]1_2MEG122820.lif - Series016-TESTFILE.tif
Image windows (not sorted):
[0,1]1_1MEG122820.lif - Series016-TESTFILE.tif
[1,1]1_1MEG122820.lif - Series016-TESTFILE.tif
[0,0]1_2MEG122820.lif - Series016-TESTFILE.tif
[1,0]1_2MEG122820.lif - Series016-TESTFILE.tif
[0,1]1_2MEG122820.lif - Series016-TESTFILE.tif
[1,1]1_2MEG122820.lif - Series016-TESTFILE.tif
[0,0]2_1MEG122820.lif - Series016-TESTFILE.tif
[1,0]2_1MEG122820.lif - Series016-TESTFILE.tif
[0,1]2_1MEG122820.lif - Series016-TESTFILE.tif
[1,1]2_1MEG122820.lif - Series016-TESTFILE.tif
[0,0]2_2MEG122820.lif - Series016-TESTFILE.tif
[1,0]2_2MEG122820.lif - Series016-TESTFILE.tif
[0,1]2_2MEG122820.lif - Series016-TESTFILE.tif
[1,1]2_2MEG122820.lif - Series016-TESTFILE.tif
Concatenated Stack
Image windows (sorted):
[0,0]1_2MEG122820.lif - Series016-TESTFILE.tif
[0,0]2_1MEG122820.lif - Series016-TESTFILE.tif
[0,0]2_2MEG122820.lif - Series016-TESTFILE.tif
[0,1]1_1MEG122820.lif - Series016-TESTFILE.tif
[0,1]1_2MEG122820.lif - Series016-TESTFILE.tif
[0,1]2_1MEG122820.lif - Series016-TESTFILE.tif
[0,1]2_2MEG122820.lif - Series016-TESTFILE.tif
[1,0]1_2MEG122820.lif - Series016-TESTFILE.tif
[1,0]2_1MEG122820.lif - Series016-TESTFILE.tif
[1,0]2_2MEG122820.lif - Series016-TESTFILE.tif
[1,1]1_1MEG122820.lif - Series016-TESTFILE.tif
[1,1]1_2MEG122820.lif - Series016-TESTFILE.tif
[1,1]2_1MEG122820.lif - Series016-TESTFILE.tif
[1,1]2_2MEG122820.lif - Series016-TESTFILE.tif
Concatenated Stack

Hi Maria,

I’ve tried to tweak your code and wondered if the issue was in the list sorting. The below code appears to work properly (i.e. concatenation is correct as far as I can see). If the issue is not solved, can you resend your code by first clicking on </> so it’s correctly formatted?

Sincerely,

Matthieu


//starting by splitting the image

n = getNumber("How many divisions (e.g., 2 means quarters)?", 2);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
Stack.setPosition(1, 1, 1);
getDimensions(w, h, channels, slices, frames);
Stack.getPosition(channel, slice, frame);
print("Position (before): "+channel+", "+slice+", "+frame);
Stack.setPosition(channels, slices, frames);
Stack.getPosition(channel, slice, frame);
print("Position (after): "+channel+", "+slice+", "+frame);
Stack.setPosition(1, 1, 1);
for (a=1; a<=channels; a++) {
	for (b=1; b<=slices; b++) {
		Stack.setChannel(a);
		wait(50);
		Stack.setSlice(b);
		wait(50);
		width = getWidth();
		height = getHeight();
		tileWidth = width / n;
		tileHeight = height / n;
		id = getImageID();
		title = getTitle();
		getLocationAndSize(locX, locY, sizeW, sizeH);
		i=1;
		for (y = 0; y < n; y++) {
			offsetY = y * height / n;
			for (x = 0; x < n; x++) {
				offsetX = x * width / n;
				selectImage(id);
				call("ij.gui.ImageWindow.setNextLocation", locX + offsetX, locY + offsetY);
				tileTitle = "[" + x + "," + y + "]" + a + "_" + b + title;
				run("Duplicate...", "title=" + tileTitle);
				makeRectangle(offsetX, offsetY, tileWidth, tileHeight);
				run("Crop");
				rename(tileTitle);
				i++;
			}
		}
	selectImage(id);
	}
}
close();

//image should be split, original image is closed
//Now sorting the list
list = getList("image.titles");
print("Image windows (not sorted):");
for (i=0; i<list.length; i++) {
	print(list[i]);
	}
NewList=Array.sort(list); //creating a new list
if (list.length==0){
	print("No image windows are open");
}
else {
	print("Image windows (sorted):");
	for (i=0; i<NewList.length; i++) {
		print(NewList[i]);
	}
}

//list sorted, now concatenating

for (j=0; j<NewList.length; j=j+2) {	
	k=1+j/2;
	print(j);
	imag1 = NewList[j];
	imag2 = NewList[j+1];
	print("selected two");
	print (imag1);
	print (imag2);
	run("Concatenate...", " title=[Concatenated Stack_"+k+"] keep image1=["+imag1+"] image2=["+imag2+"]");
}

Thank you for showing me how to do comments and pasting code into this forum! I am going to try out the code now.

THANK YOU!!! It works perfectly.