ImageJ macro to merge channels to composite

I’m trying to merge 3 channels into a composite image using a macro in batch processing. The files are named “Animal1 Section1 image1–C00” where C00 is the channel name. I’m entirely new to writing macros. How do I make this work? I have googled it but it is all beyond my current abilities and I have 40+ merges to make at the moment and more to come. The current macro I am working from is simply this:

run(“Merge Channels…”, “c1=[C3-”+imageTitle +"] c2=[C2-"+imageTitle+"] c4=[C1-"+imageTitle+"] create ignore");

Example files: L20 SE i11–C00.tif (2.6 MB) L20 SE i11–C01.tif (2.6 MB) L20 SE i11–C02.tif (2.6 MB) L20 SE i12–C00.tif (2.6 MB) L20 SE i12–C01.tif (2.6 MB) L20 SE i12–C02.tif (2.6 MB)

Have a look at the macro language introduction:
https://imagej.nih.gov/ij/developer/macro/macros.html

The macro stub seems to be ok.
I guess you want to batch the processing and that is where you are stuck?

First thing you can try if your naming pattern is consistent is to use a for loop.
Where you loop over the number after the “i”.

Like so:

for (i = 11; i <=12; i++) {

selectImage("L20 SE i" + i + "–C00.tif");
selectImage("L20 SE i" + i + "–C01.tif");
selectImage("L20 SE i" + i + "–C02.tif");

}

You can instead of selectImage open the image in sequence and merge them.
If your naming pattern is not that consistent you need to first get the unique names and then loop over them. But at the end of the day you will end up with a for loop.

Cheers,
Christopher

1 Like

I would also recommend using a few getTitle() just to ensure that your naming is correct

1 Like

Thank you for the response. Yes, I’m completely stuck using macros in batching. They work fine if I open files one at a time but I cannot apply them to batch. I have looked through the link you sent and did so again. I don’t understand how to apply the code you posted. I do have a limited understanding of a for loop. If I just add that code it does not work so I’m missing some step(s) entirely.

Hi @mosst137,

since your macro seems to run fine individually, have you tried to run it via:

process -> batch -> macro ?

This would avoid putting the batch part into your macro script.
Best regards,
Mario

Hi,

ok here is a minimal example for your problem. The macro assumes that all your files are within the same folder and follow the same naming pattern “L20 SE i11–C00.tif”.
If you have a different structure or a different naming pattern you need more stuff to account for that in the batch processing.

directory = getDirectory("Choose a Directory");


for (i = 11; i <=12; i++) {


	open(directory + File.separator + "L20 SE i" + i + "--C00.tif");
	imageTitle1 = getTitle();

	open(directory + File.separator + "L20 SE i" + i + "--C01.tif");
	imageTitle2 = getTitle();
	
	open(directory + File.separator +  "L20 SE i" + i + "--C02.tif");
	imageTitle3 = getTitle();

	run("Merge Channels...", "c1=[" + imageTitle1 +"] c2=[" + imageTitle2 + "] c4=[" + imageTitle3 + "] create ignore");

}
1 Like

Thank you for your help. I tried that code and I got the error "File is not in a supported format, a reader plugin is not available, or it was not found and it gives me a file name. Then it keeps cycling through that window with each file in the folder until I close ImageJ.

I tried it by taking a 2 merges worth of channels and putting them in a different set of folders and selecting that folder for the batch initially and when prompted but the error message is referencing a different image, which I don’t understand. The directory prompt is for the input folder right? It doesn’t work the other way anyway.

Hi, @marioK that’s the only way I know how to run it.

Could you send me a list of filenames or a screenshot of the directory?
I need to know what I am working with.

Yes the GUI prompt is for the input directory.

The subset folder I am now using is included with a screenshot and is what I was referring to in my last comment with the error message. I’ve also included the files themselves. The actual set of files is/will be much longer.

Screen Shot 2019-12-17 at 9.30.00 AM L20 SE i13–C01.tif (7.9 MB) L20 SE i13–C02.tif (7.9 MB) L20 SE i13–C00.tif (7.9 MB) L20 SE i12–C01.tif (7.9 MB) L20 SE i12–C02.tif (7.9 MB) L20 SE i12–C00.tif (7.9 MB)

Hi,

you need to adjust the indices of the for loop.

If the index of the name after the i is changing you need to adjust the program.

Here now for exactly the files you provided in your last message. I use now the variables firstIndex and lastIndex to reflect that.

directory = getDirectory("Choose a Directory");

firstIndex = 12;
lastIndex = 13;

for (i = firstIndex; i <= lastIndex ; i++) {


	open(directory + File.separator + "L20 SE i" + i + "--C00.tif");
	imageTitle1 = getTitle();

	open(directory + File.separator + "L20 SE i" + i + "--C01.tif");
	imageTitle2 = getTitle();
	
	open(directory + File.separator +  "L20 SE i" + i + "--C02.tif");
	imageTitle3 = getTitle();

	run("Merge Channels...", "c1=[" + imageTitle1 +"] c2=[" + imageTitle2 + "] c4=[" + imageTitle3 + "] create ignore");

}
1 Like

This modified each file into black and white and the directory window popped up each time. It did not create any merges. What is the function of setting and changing the indices to 12 and 13 here?

Your images are greyscale thus appear black and white when opened without a look up table. This macro does not modify your images, except opening them and merging them into a multi channel image.

If I use the images you provided, put them into a folder. Execute the macro and specify the directory I put the images into then it works.

You filenames contain a sequence of numbers after the character “i”.
If you want to loop over a sequence of numbers let say 12, 13, 14 you need to specify when to begin the sequence and when to end it. The variable firstIndex specifies with what number the sequence starts (e.g. 12) and the variable lastIndex specifies when that sequence ends (e.g.14).

Ok. I understand the index now, thank you.

If I run it from Macro>Run it does create a properly colored merge from the right files but then gets stuck in errors for files I didn’t give it, which I don’t understand. Previously, I had been just trying to run it from Batch>Macro.

Hi @mosst137
Here is a macro which uses “Microsoft windows”.
my Starting Folder: “F:\Stack to image 1”
my Arrival Folder: “F:\Stack to image 2”
Can you test?

for(j=1;j<3;j++)
{
for(i=0;i<3;i++)
{
imageDirectory ="F:\\Stack to image 1\\";
imageName ="L20 SE i1"+j+"--C0"+i+".tif";
imageAdress =imageDirectory + imageName;
open(imageAdress);
selectImage("L20 SE i1"+j+"--C0"+i+".tif");
rename( i);
}
run("Merge Channels...", "c1=0 c2=1 c3=2 create");
wait(100);
rename(j);
//run("Channels Tool...");
run("Stack to RGB");
saveAs("Tiff", "F:\\Stack to image 2\\"+j+"(RGB).tif");
wait(100);
close(j);
close(j+"(RGB).tif");
}

Hi @Mathew. Thanks, I did test it. I get the error " File is not in a supported format, a reader plugin is not available, or it was not found. L20 SE i11–C00.tif" I’m running on mac so I’m not sure I putting the directory in correctly for imageDirectory. I tried to run something more simple and that also failed (Error when running example function for macro on Mac). Interestingly, and this is what we saw above, the file mentioned in the error box is not in the destination folder. I don’t know why it gets included like this.

The directory I used was “imageDirectory =”/Users/myfullname/Desktop/Analysis Cohort 1/Merge test subset/Input/";