如何使用斐济宏自动修改图像的亮度和对比度?

how can I modify the brightness&contrast automatically on my images with fiji macro?

我尝试用 Fiji 写一个非常简单的宏来自动合并通道和增强对比度。

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");
}

使用 "enhance contrast",我不知道如何在宏中使用亮度和对比度 window 的按钮 "auto"。第二频道比第一频道强

而对于 "apply LUT",当我有这一行时会发生错误:"The display range must first be updated using Image>Adjust>Brightness/Contrast or threshold levels defined using Image>Adjust>Threshold." 我更改了阈值级别,但它仍然不起作用...

你能给我什么建议?

如果显示的最小值和最大值为 0 和 255,则应用 Lut 命令 (source) 会出现此错误。如果您的图像已经具有高对比度,则可能会发生这种情况。下面我使用 getMinAndMax 添加了一个条件,如果显示范围不变(即 0-255),它会跳过 Apply 步骤。

With "enhance contrast", I don't know how I can use the button "auto" of the brightness&contrast window in the macro.

根据 documentationrun("Enhance Contrast", "saturated=0.35") 与在 B&C window 上单击自动相同。

我不清楚你是想在所有图像上重复这个过程还是只在第二个通道上重复这个过程。下面是一个宏,可以单独增强每个通道的对比度,并修复循环的一些问题。

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
}