Search code examples
imagej

How to get threshold value used by auto threshold Plugin


I have the following code, where I read images from directory and use ImageJ Auto Threshold plugin to segment my images.

dir = getDirectory("path");
list = getFileList(dir);

for (i=0; i<list.length; i++)
{
   if (endsWith(list[i], ".tif")) 
   {
        open(dir + list[i]);
        run("8-bit");
        run("Gaussian Blur...", "sigma=2");
        setAutoThreshold("Otsu dark");
        run("Convert to Mask");
        saveAs("TIFF", dir+list[i]);
        close();
    }
}

I would like to get the threshold value using "Otsu dark" method, and modify that value (e.g. scale it by a factor) and apply it to my images for segmentation.


Solution

  • In an ImageJ macro, use the getThreshold(lower,upper) and setThreshold(lower,upper) methods (here's the documentation).

    Your code would look like this then:

    dir = getDirectory("path");
    list = getFileList(dir);
    factor = 1.5;
    
    for (i=0; i<list.length; i++)
    {
       if (endsWith(list[i], ".tif")) 
       {
            open(dir + list[i]);
            run("8-bit");
            run("Gaussian Blur...", "sigma=2");
            setAutoThreshold("Otsu dark");
            getThreshold(lower,upper);
            setThreshold(lower,upper*factor);
            run("Convert to Mask");
            saveAs("TIFF", dir+list[i]);
            close();
        }
    }
    

    If you plan to do more complicated things, consider using another scripting language like the ones provided by Fiji.