Re: Threshold as a percentage of image histogram?
Posted by
Ben Tupper on
Jul 03, 2008; 11:54am
URL: http://imagej.273.s1.nabble.com/Threshold-as-a-percentage-of-image-histogram-tp3695671p3695673.html
On Jul 3, 2008, at 3:03 AM, Roger Chang wrote:
> 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];
> }
Hi Roger,
The only suggestion I have for you is to consider that for 8-bit and
RGB images the total "area" under the histogram is the number of
pixels in the image. This is a design implementation specific to
ImageJ. Thus you can skip the "cumSum" loop above to calculate the
total number of pixels and go directly to ....
totalNumberOfPixels = getWidth() * getHeight();
Cheers,
Ben
> 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.
Ben Tupper
[hidden email]
I GoodSearch for Ashwood Waldorf School.
Raise money for your favorite charity or school just by searching the
Internet with GoodSearch - www.goodsearch.com - powered by Yahoo!