I try to write a very simple macro with Fiji in order to merge channels and enhance contrast automatically.
dir = getDirectory("Select input directory"); out = getDirectory("Select destination directory");
files = getFileList(dir);
//foreach tiff couple files
for (j=0; j<lengthOf(dir);j+2) {
channel1 = dir+files[j];
channel2 = dir+files[j+1];
open(channel1);
open(channel2);
run("Enhance Contrast", "saturated=0.35"); // the same for the channel1
run("Apply LUT", "stack"); // the same for the channel1
run("Merge Channels...", "c1="+channel1+" c2="+channel2);
run("Z Project...", "projection=[Sum Slices]");
saveAs("Tiff", out+"merge"+files[j]);
run("Close");
}
With "enhance contrast", I don't know how I can use the button "auto" of the brightness&contrast window in the macro. The channel 2 is stronger than the first.
And with "apply LUT", an error occur when I have this line : "The display range must first be updated using Image>Adjust>Brightness/Contrast or threshold levels defined using Image>Adjust>Threshold." I changed the threshold level and it still doesn't work...
What could you suggest to me ?
The Apply Lut command (source) gives this error if the display min and max are 0 and 255. This could occur if your image already has high contrast. Below I added a condition using getMinAndMax
where it skips the Apply step if the display range is unchanged (i.e. 0-255).
With "enhance contrast", I don't know how I can use the button "auto" of the brightness&contrast window in the macro.
According to the documentation, run("Enhance Contrast", "saturated=0.35")
is the same as clicking Auto on the B&C window.
I'm not clear if you want to repeat this process on all the images or just the 2nd channel. Below is a macro that enhances contrast on each channel individually, and fixes some issues with the loops.
dir = getDirectory("Select input directory"); out = getDirectory("Select destination directory");
files = getFileList(dir);
//foreach tiff couple files
for (j=0; j<lengthOf(files);j+=2) { //fixed loop length and incrementor
channel1 = dir+files[j];
channel2 = dir+files[j+1];
open(channel1);
image1 = getTitle(); // get the window name
open(channel2);
image2 = getTitle(); // get the window name
selectWindow(image1); // focus on the first channel
run("Enhance Contrast", "saturated=0.35 process_all"); // process all slices
getMinAndMax(min,max); // get display range
if (min != 0 && max != 255) { // check if the display has been changed
run("Apply LUT", "stack");
}
selectWindow(image2); // repeating for the 2nd channel
run("Enhance Contrast", "saturated=0.35 process_all"); // process all slices
getMinAndMax(min,max); // get display range
if (min != 0 && max != 255) { // check if the display has been changed
run("Apply LUT", "stack");
}
run("Merge Channels...", "c1="+image1+" c2="+image2); // use window names rather than full paths
run("Z Project...", "projection=[Sum Slices]");
saveAs("Tiff", out+"merge"+files[j]);
run("Close All"); // make sure everything is closed
}