Reputation: 1
I am making a script to merge channels together into a 4D stack. It should take three images in a folder at a time, merge them, and save the composite image.
What I don't understand is that in the first iteration, it skips the 3rd image (opening only the first two), and then starts the next iteration with the third image. So only the first iteration is wrong, but it's not like it skips the first image all together.
// Specify the directory containing the images
dir = getDirectory("Select a directory");
output = dir + "Merged" + File.separator;
File.makeDirectory(output);
// Get a list of all the files in the directory
list = getFileList(dir);
// Loop through the list of files, three at a time
for (i = 0; i < list.length; i += 3) {
if (endsWith(list[i], ".tif")) {
open(dir + list[i]);
}
if (i + 1 < list.length) {
if (endsWith(list[i + 1], ".tif")) {
open(dir + list[i + 1]);
}
}
if (i + 2 < list.length) {
if (endsWith(list[i + 2], ".tif")) {
open(dir + list[i + 2]);
}
}
// Get name of the image without channel number
imageName = substring(getTitle(), 0, (lengthOf(getTitle())-9));
imageList = getList("image.titles");
Array.sort(imageList);
// Merge the channels and save
run("Merge Channels...", "c1=" + imageList[0] + " c2=" + imageList[1] + " c7=" + imageList[2]);
rename(imageName + "_merged");
saveAs("Tiff", output + getTitle() + ".tif");
close("*");
}
After replacing the merge channel command by a print array command and waitForUser, I could see it opens image 1, 2, prompts a waitForUser, opens 3, 4, 5, prompts a waitForUser, etc etc..
I feel the mistake I made is minor, but I cant seem to find it..
Upvotes: 0
Views: 954
Reputation: 1
Might be a little late of an answer but, I was encountering the issue that only two files would open and it seemed to be skipping the first "open" command completely.
After playing around for a while, I got to open three files but only when I had 4 interations of "if ... open" listed. After some more playing around I made the small change detailed below and this got it opening all three files, with only 3 open commands do some renaming and saving of files and then open up the next 3 files in the list and repeat:
for (i = 0; i < list.length; i +=3) { //Adjust += number to appropriate number of channels
file = list[i];
if (i < list.length) {
if (file.endsWith(".tif")) {
open(dir+file);
}
}
if (i + 1 < list.length) {
if (file.endsWith(".tif")) {
open(dir + list[i + 1]);
}
}
if (i + 2 < list.length) {
if (file.endsWith(".tif")) {
open(dir + list[i + 2]);
}
}
and so on for I'm guessing however many channels you want.
I can not state just how much your code helped me! I'm fairly new to fiji macro-ing and have been trying the entire time to figure out how to get multiple tif files to open together.
Hope this helps, and again massive thanks for code that opens multiple files!
Upvotes: 0
Reputation: 1
I don't know if the answer could possibly be this simple... but I noticed that here :
// Merge the channels and save run("Merge Channels...", "c1=" + imageList[0] + " c2=" + imageList[1] + " c7=" + imageList[2]); rename(imageName + "_merged");
you have written c1, c2, and then c7... I tried running this on my own images (also in sets of three), and changed c7 to c3, and now it seems to be working :P
Upvotes: 0