Limit to Threshold setting isn't giving me area of thresholded pixels on FIJI

I’m trying to determine the fraction of overlapping pixels between two channels. I thresholded my two images, and generated a new image with just the overlapping pixels using the image calculator. Now I want to get the area or pixel number from each image but something weird is going on.

I’m just trying to get an area measurement of the white pixels by doing Analyze>Measure after having check the “limit to threshold” box in measurement settings. But it is just giving the area of the whole image, can anyone tell me what I’m doing wrong?

Screen Shot 2020-01-09 at 4.16.51 PM

Hi @scor!

Perhaps your are missing a final thresholding step of the final image:

See the second result, after thresholding. Both measurments have “Limit to threshold” set, but only the second one has a threshold in place.

Hope this helps,
Cheers,
Nico

1 Like

So I need to re-apply my threshold before doing Analyze>Measure? When I do that I do get different numbers on area (thank you!)… but the area calculated seems to be black pixels. I can invert to get the actual value I care about, but I thought that white pixels were what would be counted here.

The actual color you’re thresholding depends on the actual LUT that’s on the image. If you look closely, the image example that I posted has an inverting LUT (which comes by default after creating a mask from a threshold). In that image, white means 0, and black 255.

Also, you can check wether the threshold is set with the option “Dark background” checked, which will invert the criteria for the thresholded region.

In any case, the area that’s measured is the one shown in read when thresolding.

1 Like

Hi,
I have a similar problem. It seems that I need to click “select” after I apply a threshold in order to get the correct area. My problem is that I am trying to record a macro, so the macro doesn’t record when I click “select”. Is there any solution for this problem?

Hi @NeuroStereo,

Which dialog is the one showing the “select” option? Or do you mean “Set” from the Threshold dialog?

Hi Nicolas,
I am working on RGB images so “select” option is from “color threshold” dialog

Oh, I see. The Color Threshold plugin does not work in the same way as the regular single-channel threshold. In fact, the way it is “recorded” is quite different from most recordable plugins.

To be able to get a macro code to reproduce the thresholding you’ve chosen, you have to press the “macro” button, which will dump to the recorder a snippet of code like:

// Color Thresholder 2.1.0/1.53c
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=0;
max[0]=14;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=108;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
// Colour Thresholding-------------

If you run just this block, it’ll turn your your original RGB image into a binary image representing the thresholded regions, and you’ll lose your original image.

So, to go from your image to the same image with a selection of the thresholded regions you’ll have to wrap this code with some additional lines:

setBatchMode(true);
imgID = getImageID();
run("Duplicate...", " ");

/*
code snippet from Color Threshold
*/

run("Create Selection");
selectImage(imgID);
run("Restore Selection");
setBatchMode(false);

imagen

Then you can just measure on this ROI, there’s no need for the “limit to threshold” option in Set Measurements....

Cheers,
Nico

(post withdrawn by author, will be automatically deleted in 72 hours unless flagged)

Hi Nicloas,
Thanks a lot for help. So I have now recorded the macro and I have added the lines that you suggested. This is how it looks like:

setBatchMode(true);
imgID = getImageID();
run("Duplicate...", " ");
run("Color Threshold...");
// Color Thresholder 1.50i
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=0;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=59;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
run("Create Selection");
selectImage(imgID);
run("Restore Selection");
setBatchMode(false);
run("Measure");
// Colour Thresholding-------------

I have used the macro to run individual image, but also to run batch of images. It seems yo work for both. May question is, will the same brightness and saturation values, specified in this macro, be applied to all images that I run using this macro? or will the brightness and saturation values automatically change according to the histogram of each image I run?
One more question, As I am going to use this macro to run batch of images, I want to save a copy of the images with the delineation (to check afterwards if the delineation is working fine). Currently, if I run the macro on batch of images (process—> batch—> macro), the image J will generate a copy of these images without saving the delineation. Is there a way to save the delineation on these?

Hi, @NeuroStereo,

First of all, please note that the code that you posted has an additional line (line 4) that was not included in my suggested code:

run("Color Threshold...");

The whole block generated by the plugin replaces the recorded action of opening the plugin, so you should remove it.

Yes, the values that define the thresholded region are hard-coded in the snippet (they are stored in the three arrays: min, max and pass), so the very same parameters willl be used each time. You should evaluate if this approach is adequate for your set of images.

If you I run this code in batch (using Process > Batch > Macro...), and you specify an output folder, it should produce duplicate .tif files that include the last ROI generated, which can be viewed by opening again in Fiji. I just confirmed this on my PC.

There are other ways of saving the generated ROIs for each image, by sending each ROi to the ROI manager and then saving a small .zip file along with the original image. This is a good strategy if you don’t want to duplicate the images.

Cheers,
Nico

1 Like

Thanks again for help.
-Yes true I should delete run(“Color Threshold…”);

  • I will try to apply the macro on couple of images and evaluate if the output makes sense.
    -Yes, you are correct. Now I can also see the delineation on the duplicate.