How to create ID in the photos so I can apply the same macro to them?

Hi!! I’m trying to create my first macro to process some images applying the same characteristics to all of them, as well as making a merge in trios. When I record the macro for an example of my first three photos, it works, but when I try to do the same for the following three pictures (already opened in Fiji), the macro doesn’t work because it cannot find the file due to differences in the name. Is there any kind of solution without changing the name of the photos? Here, I leave the macro I’ve created.

//Subtract background to RFP and GFP channels//
imageCalculator("Subtract create", "4_FL_GFP.tif","4_Ctrl_GFP.tif");
imageCalculator("Subtract create", "3_FL_RFP.tif","3_Ctrl_RFP.tif");
//Automatically adjustment of the contrast to see axons saturated//
selectWindow("Result of 3_FL_RFP.tif");
run("Enhance Contrast", "saturated=0.35");
selectWindow("Result of 4_FL_GFP.tif");
run("Enhance Contrast", "saturated=0.35");
//Merge 3 channels to have the photo we want to analyze//
run("Merge Channels...", "c1=[Result of 3_FL_RFP.tif] c2=[Result of 4_FL_GFP.tif] c3=1_FL_A.tif keep");
//Change to 8bits and the LUT to save the processed photos individually//
selectWindow("Result of 3_FL_RFP.tif");
run("Red");
setOption("ScaleConversions", true);
run("8-bit");
selectWindow("Result of 4_FL_GFP.tif");
run("Green");
setOption("ScaleConversions", true);
run("8-bit");
selectWindow("1_FL_A.tif");
run("Cyan");
setOption("ScaleConversions", true);
run("8-bit");

Thanks in advance!!

Dear @ana_gm,

Welcome to the forum!

When you submit code, enclose it in triple backticks ``` [your code] ``` which makes it easier to read.

[EDIT: Discourse adds “smart quotes” when you pasted the code without the backticks, which the ImageJ code editor doesn’t like. If you leave them, any user who wants to help you will need to fix the issue before being able to run your code. I have filed a request with the forum team to perhaps deactivate this option to have smart quotes…]

Now onto the fun stuff!
Congratulations on your first macro! The macro recorder is great but not super flexible when it comes to image names, as you have noticed. One way to make it more “flexible” is to use Script Parameters. These will create a prompt where you can select which image is which. Afterwards, you need to store the title of the images as variables and reuse them. Unfortunately it’s a bit contrived if you have never had any formal programming experience, but this Introduction to Macro Programming is pretty well made if you want to understand what was done in the code below.
Please note that this is one way of doing it. Another one involves reading the names of the currently open images (If they are named consistently) and figuring out which goes with which, but without further data or examples it is difficult to help.

I’ve adapted your code to show you how the script parameters could work, if you have any questions do not hesitate:

@ImagePlus gfp_image
@ImagePlus gfp_ctrl
@ImagePlus rfp_image
@ImagePlus rfp_ctrl
@ImagePlus fl_image

// Get the titles of each image
selectImage(gfp_image)
g_title = getTitle();

selectImage(gfp_ctrl)
gc_title = getTitle();

selectImage(rfp_image)
r_title = getTitle();

selectImage(rfp_ctrl)
rc_title = getTitle();

selectImage(fl_image)
fl_title = getTitle();


//Subtract background to RFP and GFP channels//
imageCalculator( "Subtract create", g_title, gc_title );
rename( "BG-Subtracted - " + g_title );

imageCalculator( "Subtract create", r_title, rc_title );
rename( "BG-Subtracted - " + r_title );

//Automatically adjustment of the contrast to see axons saturated//
selectImage( "BG-Subtracted - " + r_title );
run("Enhance Contrast", "saturated=0.35");

selectImage( "BG-Subtracted - " + g_title );
run("Enhance Contrast", "saturated=0.35");
print(fl_image)
//Merge 3 channels to have the photo we want to analyze//
run("Merge Channels...", "c1=[BG-Subtracted - " + r_title + "] c2=[BG-Subtracted - " + g_title + "] c3=[" + fl_title + "] keep");

//Change to 8bits and the LUT to save the processed photos individually//
selectImage( "BG-Subtracted - " + r_title );
run("Red");
setOption("ScaleConversions", true);
run("8-bit");
rename( "BG-Subtracted - " + g_title );
run("Green");
setOption("ScaleConversions", true);
run("8-bit");
selectWindow(fl_title);
run("Cyan");
setOption("ScaleConversions", true);
run("8-bit");

Onto the less fun part

You mention

However you are applying the “Enhance Contrast” tool, which applies the same method to all your images, namely, “Allow for 0.35% of the pixels in the image to be saturated”. While good visually, this is a bad method to compare conditions, as they might all have similar intensities by eye, but completely different values.

As an example, consider these two images:
image
They are the same but the right one has half the intensities as the one on the left.

If we apply “Enhance Contrast”, we get this:
image

Suddenly any differences are hidden!

The usual approach is to set the min and max display values to be identical between images, replacing the run(“Enhance Contrast”, “saturated=0.35”); to

min= 0;
max= 2000;
setMinAndMax(min, max);

You could have different min and max for your RFP image or your GFP image in case that is necessary.

Please do not hesitate to ask questions.

Best

Oli

2 Likes