setThreshold on 32-bit floating point image

Hi,

I have a AFM image that (after come scaling to nanometer scale) has 32-bit floating point intensity values.
Now I want to apply a threshold using a script for pixels that are 0.5 to 1.8 nm high (i.e. their intensity values range from 0.5 to 1.8).

I run into a problem when calling setThreshold(0.5, 1.8): The whole image gets selected (in red).
However, when I do not use scripting but click “Set” in the “Image -> Adjust… -> Threshold” and enter these values the mask is ok.
Is there a problem with the setThreshold() function on 32-bit float images?

Thanks!

Manuel

PS: I used the IJ1 Macro language, but I also tried the Python scripting interface where the same bug occurs.

Try the command

resetMinAndMax()

before, see:

https://imagej.nih.gov/ij/developer/macro/functions.html#R

https://imagej.nih.gov/ij/macros/DisplayRangeMacros.txt

That seems to work.

Thanks alot, Blo7!

Looks like this command is not very reliable: in 80% of the cases this works fine, but in the other 20% the problem is not fixed (still the whole image is selected in red). Maybe there is a race condition in the code?

@manuel can you provide an example image and the exact code you used when this happens 20% of the time?

Please keep in mind that the red display of threshold on 16-bit and 32-bit image is merely an approximation of the thresholded area: it modifies the (8-bit) LUT of the image to show the thresholded region. To get accurate results, convert the image to a binary mask.

My script looks like this:

a = getInfo('33076');
b = getInfo('33077');

print('\\Clear');
run("Clear Results");
run("Set Measurements...", "area shape redirect=None decimal=5");

run("Duplicate...", "title=height");

run("Multiply...", "value="+a);
run("Add...", "value="+b);
run("Multiply...", "value=1e9"); // convert meter to nm

background = 0;

function filter(name, threshold) {
    n = roiManager("count");
    for (i = 0, j = 0; i < n; i++, j++) {
        value = parseFloat(getResult(name, i));
    
        roiManager("select", j);
        if (value < threshold) {
            roiManager("Delete");
            j--;
        }
    }
}

function rois2mask(name) {
    setThreshold(-999999, 999999);
    run("Create Mask");

    setForegroundColor(255, 255, 255);
    roiManager("Deselect");
    roiManager("Fill");

    run("32-bit");
    run("Divide...", "value=-255");
    run("Add...", "value=1");
    resetThreshold();
    run("Invert LUT");

    rename(name);
}

selectWindow('height');

resetMinAndMax();
setThreshold(0.5+background, 1.8000+background);
run("Analyze Particles...", "size=8-Infinity display clear add");
rois2mask('Objects');
resetMinAndMax();

And the file can be downloaded here (you must load it using the bioformats importer!)

Your link doesn’t seem to work anymore.

It’s not clear to me what you’re trying to achieve with this function. Wouldn’t it be sufficient to run Analyze Particles… with show=Masks?

I fixed the issue by switching from ImageJ Macro language to Python!