Login  Register

Re: A question about ImageJ's threshold values

Posted by Avital Steinberg on Apr 07, 2016; 3:05pm
URL: http://imagej.273.s1.nabble.com/A-question-about-ImageJ-s-threshold-values-tp5013177p5016076.html

Hi Ramon,
The following code is what I used when I tweaked my macro, but I think that
the tweaking is not needed anymore. I didn't take into account the pixel
with the highest grayvalue, which is 255 for an 8-bit image. I was looking
for the threshold which covers 55% of the histogram. Notice that I didn't
set the threshold here at all, and this can be done only after finding the
optimal threshold.

With tweaking, this is the code for finding the threshold when I want to
threshold according to a specific percentile:

percentVal = 55;
nBins = 256;
resetMinAndMax();
getHistogram(values, counts, nBins);
// find culmulative sum
nPixels = 0;
percentage = newArray(counts.length);
for (i = 0; i<counts.length; i++){
  nPixels += counts[i];
  nForgroundPixels = nPixels - counts[255];
  }
for (i = 0; i<255; i++){
    sum = sum + counts[i];
    percentage[i] = sum*100/nForgroundPixels;
    if (percentage[i] > percentVal) {
        idealT = i;
        i = 999999;
    }
}
print("T is: " + idealT);

I hope this helps - good luck,

Avital



On Thu, Apr 7, 2016 at 4:40 PM, melkor2.0 <[hidden email]> wrote:

> Hi,
>
> I'm running through a similar problem here, I wonder how the macro was
> tweaked to make it run. I work with 12-bit depth images, and I am
> interested
> in thresholding and analyzing at a particular % of each image. The script
> seemed to work, but I noticed that the values are different as if I do it
> manually. I was wondering whether this might be related to the max value of
> the histogram (in the images with the greatest deviation the max value is
> around 2500 [saturated pixels are 4095], so when I manually do the 95% for
> that particular image the threshold value is around 1200 but the macro
> prints a value of 1800...
>
> What am I missing?
>
> origen = getDirectory("Images to process");
> lista = getFileList(origen);
> setBatchMode(true);
> for (j=0; j<lista.length; j++) {
>        showProgress(j+1, lista.length);
>        open(origen+lista[j]);
>        nombre = lista[j];
>        run("Split Channels");
>        id = getImageID();
>        selectImage(id);
>        run("Duplicate...", " ");
>         tissueThreshPerc = 95;
>                 nBins = 4095;
>                 getHistogram(values, count, nBins);
>                 size = count.length;
>                 cumSum = getWidth() * getHeight();
>                 tissueValue = cumSum * tissueThreshPerc / 100;
>                 cumSumValues = count;
>                 for (i = 1; i<count.length; i++)
>                         {
>                         cumSumValues[i] += cumSumValues[i-1];
>                         }
>                 for (i = 1; i<cumSumValues.length; i++)
>                         if (cumSumValues[i-1] <= tissueValue &&
> tissueValue <= cumSumValues[i])
>                 {
>                         setThreshold(i,4095);
>                         print(i);
>                 }
>        run("Convert to Mask");
>        run("Open");
>        run("Close-");
>        run("Create Selection");
>        run("Copy");
>        run("Analyze Particles...", "size=75-Infinity summarize");
>        selectImage(id);
>        run("Restore Selection");
>        run("Measure");
>   }
>
> I just tried to change the cumSum loop by the total pixel size as suggested
> in the other thread:
>
> http://imagej.1557.x6.nabble.com/Threshold-as-a-percentage-of-image-histogram-td3695671.html
>
> But if I try to reset min and max right after i give the value of nBins, it
> doesn't work :(
>
> Thanks for your patience and help!!
>
> Ramon
>
>
>
> --
> View this message in context:
> http://imagej.1557.x6.nabble.com/A-question-about-ImageJ-s-threshold-values-tp5013177p5016073.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html