Image J - Macro to Adjust Contrast


Thank you for your interest in helping me!

I have tons of images to analyze and I am trying to create a Macro to save as much time as possible! I followed some recommendations provided online.

My macro is not that complicated (that’s what I think) but I am failing. I want to split the channels of my images and subsequently, I want to adjust the contrast of each channel to a different value of Minimum and Maximum (Image>Adjust>Brightness Contrast) and then save each one of my channels as independent figures with the newly adjusted contras (I hope this makes sense). I have been trying to set up the contrast by using the Macro SetMinAndMax but I don’t see any effect in the saved pictures.

Here is the macro I am using for ImageJ


        inputFolder= getDirectory("Choose a Directory");
        outputRed= inputFolder + "/RedChannel/";
        outputGreen= inputFolder + "/GreenChannel/";
        outputBlue= inputFolder + "/BlueChannel/";
        images= getFileList(inputFolder);

        for (i=0; i<images.length; i++) {
                setBatchMode(true); //batch mode
                inputPath= inputFolder + images[i];
                open (inputPath);
                print("Splitting Image: " + imagesName);
                run("Split Channels");
                selectWindow(imagesName + " (blue)");
                setMinAndMax(0, 124);
                saveAs("Tiff", outputBlue + "blue_" +imagesName);
                selectWindow(imagesName + " (green)");
                setMinAndMax(0, 174);
                saveAs("Tiff", outputGreen + "green_" +imagesName);
                selectWindow(imagesName + " (red)");
               setMinAndMax(0, 70);
                saveAs("Tiff", outputRed + "red_"+imagesName);
                write("Conversion Complete");

I hope you guys can help me!!


Hi Simon,
Setting the minimum and maximum only affects how the image is displayed on the screen without affecting the underlying pixel values. So, your saved images will be exactly the same as the original images. If you want to actual change the pixel values, you have to apply the settings to the image. In a macro you can do this with:

run("Apply LUT");

However, this permanently causes an irreversible change of the pixel values and will affect all subsequent measurements. I can only think of very few situations when you actually want to do this, and I would always avoid doing this to the original images.

I see a problem in your macro, splitting channels gives images named like “C1-title” “C2-title” etc.
So using selectWindow("C1-"+imagesName); should be better
in my Fiji it works and the new contrast display is saved.
Your macro is for visualization purpose only right?