A question about ImageJ's threshold values

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

A question about ImageJ's threshold values

Avital Steinberg
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
Reply | Threaded
Open this post in threaded view
|

Re: A question about ImageJ's threshold values

Michael Schmid
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
Reply | Threaded
Open this post in threaded view
|

Re: A question about ImageJ's threshold values

Avital Steinberg
Thanks - I get the same answer when I use the macro you wrote and the macro
I found in this link:

http://imagej.1557.x6.nabble.com/Threshold-as-a-percentage-of-image-histogram-td3695671.html

But when I choose adjust, threshold and manually adjust the sliding bar, I
get a different result.

Why would this be?

Thanks,

Avital


On Tue, Jun 16, 2015 at 4:10 PM, Michael Schmid <[hidden email]>
wrote:

> 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
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: A question about ImageJ's threshold values

Avital Steinberg
In reply to this post by Michael Schmid
Thanks, Michael - I tweaked your macro a little and now it works. I had to
subtract the background pixels.

I appreciate your help - it really made things clear,

Avital

On Tue, Jun 16, 2015 at 4:10 PM, Michael Schmid <[hidden email]>
wrote:

> 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
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: A question about ImageJ's threshold values

Michael Schmid
In reply to this post by Michael Schmid
Hi Avital and everyone,

there was a bug in the percentile display of the Threshold panel; the highest pixel value (255 for uncalibrated 8-bit images) was not included in the statistics if it was not inside the thresholded range.
Wayne has fixed this in the latest daily build, 1.49v4.

Michael
________________________________________________________________

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
Reply | Threaded
Open this post in threaded view
|

Re: A question about ImageJ's threshold values

Avital Steinberg
Thank you Michael and Wayne!

On Wed, Jun 17, 2015 at 9:20 PM, Michael Schmid <[hidden email]>
wrote:

> Hi Avital and everyone,
>
> there was a bug in the percentile display of the Threshold panel; the
> highest pixel value (255 for uncalibrated 8-bit images) was not included in
> the statistics if it was not inside the thresholded range.
> Wayne has fixed this in the latest daily build, 1.49v4.
>
> Michael
> ________________________________________________________________
>
> 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
Reply | Threaded
Open this post in threaded view
|

Re: A question about ImageJ's threshold values

melkor2.0
In reply to this post by Michael Schmid
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
Reply | Threaded
Open this post in threaded view
|

Re: A question about ImageJ's threshold values

Avital Steinberg
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