Login  Register

Re: Threshold as a percentage of image histogram?

Posted by Roger Chang-3 on Jul 03, 2008; 7:03am
URL: http://imagej.273.s1.nabble.com/Threshold-as-a-percentage-of-image-histogram-tp3695671p3695672.html

Ben,
Thanks for your suggestion and corresponding code. I did not realize that
there was the macro function getHistogram(counts, values, nBins, ...). I
adopted your macro to instead calculate a threshold as a function of a
percentage of the total area under an image histogram. It calculates the
total number of pixel values in the image histogram. Then it makes a new
cumulative array of the count array, such that the tissueValue (equals total
number of pixel values * the tissueThreshPercentage) can be found within the
cumulative array and a threshold can be outputted:

// Input a tissue threshold percentae 0-100
tissueThreshPrec = 80;
nBins = 256;
getHistogram(values, count, nBins);
size = count.length;
// find culmulative sum
cumSum = 0;
for (i = 0; i<count.length; i++)
{
cumSum += count[i];
}
tissueValue = cumSum * tissueThreshPerc / 100;
print(tissueValue);
// cumulative sum of before
cumSumValues = count;
for (i = 1; i<count.length; i++)
{
cumSumValues[i] += cumSumValues[i-1];
}

// find tissueValue
for (i = 1; i<cumSumValues.length; i++)
{
if (cumSumValues[i-1] <= tissueValue && tissueValue <= cumSumValues[i])
// output tissue threshold:
print(i);
}

Hopefully my macro makes sense.
-Roger C.