Posted by
Michael Schmid on
Jun 16, 2015; 1:10pm
URL: http://imagej.273.s1.nabble.com/A-question-about-ImageJ-s-threshold-values-tp5013177p5013179.html
Hi Avital,
yes, the small number below the histogram in the Threshold panel is the percentage of pixels within the histogram bounds selected by the threshold.
Here is a macro that sets the threshold such that roughly 80% of the pixels are included:
percentage = 80;
nBins = 256;
resetMinAndMax();
getHistogram(values, counts, nBins);
// find culmulative sum
nPixels = 0;
for (i = 0; i<counts.length; i++)
nPixels += counts[i];
nBelowThreshold = nPixels * percentage / 100;
sum = 0;
for (i = 0; i<counts.length; i++) {
sum = sum + counts[i];
if (sum >= nBelowThreshold) {
setThreshold(values[0], values[i]);
print(values[0]+"-"+values[i]+": "+sum/nPixels*100+"%");
i = 99999999;//break
}
}
For 16-bit and floating-point images, there will be a small difference between the exact number of pixels actually selected and the number printed by the macro. That's because the macro uses only an 8-bit histogram (256 values). When using an 8-bit histogram, for 16-bit and 32-bit images it would be usually better to include one more histogram bin.
Michael
________________________________________________________________
Michael Schmid email:
[hidden email]
Institut für Angewandte Physik, Technische Universität Wien
Wiedner Hauptstr. 8-10/E134, A 1040 Wien, Austria
Tel. +43 1 58801-13452 or -13453, Fax +43 1 58801 13499
________________________________________________________________
On Jun 16, 2015, at 14:36, Avital Steinberg wrote:
> Hi,
> When I choose any auto thresholding method in ImageJ, I get a histogram. I
> can change the threshold values using a slide bar and I see percent values.
> Are these percent cumulative frequencies for the histogram, or something
> else?
>
> I would like to write a macro which, when given a percent value from this
> slide bar, would give me a threshold value. I tried doing this with the
> following macro:
>
>
http://imagej.1557.x6.nabble.com/Threshold-as-a-percentage-of-image-histogram-td3695671.html>
> but the values it gave were different from what I expected.
>
> Also, there must be something that I don't understand, because the
> percentile auto thresholding method shows a value of 70.17% percent on the
> slide bar. I thought it would give me 50%.
>
> I'm confused and would appreciate your help.
>
> Thanks,
>
> Avital
>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html