Percentile value

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

Percentile value

Pang, Zhengyu (GE Global Research)
Dear all,

ImageJ could provide you with Mean, median, min and max. How do I get 1
percentile, or 5 percentile values? And how do I get the average for
first percentile.?

Thanks!

Zhengyu

Dr. Zhengyu Pang
General Electric Company Global Research Center
Niskayuna, NY 12309.
Reply | Threaded
Open this post in threaded view
|

Re: Percentile value

BenTupper
Hi,

On Jun 9, 2009, at 11:19 AM, Pang, Zhengyu (GE, Research) wrote:

> Dear all,
>
> ImageJ could provide you with Mean, median, min and max. How do I  
> get 1
> percentile, or 5 percentile values? And how do I get the average for
> first percentile.?
>

I Wonder if the cumulative histogram (normalized) could work for you.  
The following converts the histogram to a cumulative histogram, then  
normalizes it to the number of pixels in the image.   I'm no stats  
guy, so this might be considered an approximation by purists.

Cheers,
Ben

//START MACRO
//run("Blobs (25K)");
nBins = 256;
getHistogram(values, counts, 256);

//create a cumulative histogram
cumHist = newArray(nBins);
cumHist[0] = values[counts[0]];
for (i = 1; i < nBins; i++){ cumHist[i] = counts[i] + cumHist[i-1]; }

//normalize the cumulative histogram
normCumHist = newArray(nBins);
for (i = 0; i < nBins; i++){  normCumHist[i] = cumHist[i]/
cumHist[nBins-1]; }


// find the 5th percentile (= 0.05)
target = 0.05;
i = 0;
do {
        i = i + 1;
        print("i=" + i + "  value=" + values[i] +  "  count=" + counts[i] + "  
cumHist= " + cumHist[i] + "  normCumHist= " + normCumHist[i] );
} while (normCumHist[i] < target)

print("5th percentile located at either " + (i-1) + "th or " + (i) +  
"th bin");
print("    which has a location of " + values[i-1] +" or " + values[i]);

//END MACRO



Ben Tupper
Reply | Threaded
Open this post in threaded view
|

Re: Percentile value

Pang, Zhengyu (GE Global Research)
Ben,

Thanks for your help. I tested your macro and it worked. As you said,
this is only an approximation.

I am wondering if ImageJ could read an image and then convert image to a
matrix like what Matlab can do. Could you then sort the matrix and found
5% percentile?

Best Regards,

Zhengyu

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
Ben Tupper
Sent: Wednesday, June 10, 2009 8:34 PM
To: [hidden email]
Subject: Re: Percentile value

Hi,

On Jun 9, 2009, at 11:19 AM, Pang, Zhengyu (GE, Research) wrote:

> Dear all,
>
> ImageJ could provide you with Mean, median, min and max. How do I get
> 1 percentile, or 5 percentile values? And how do I get the average for

> first percentile.?
>

I Wonder if the cumulative histogram (normalized) could work for you.  
The following converts the histogram to a cumulative histogram, then  
normalizes it to the number of pixels in the image.   I'm no stats  
guy, so this might be considered an approximation by purists.

Cheers,
Ben

//START MACRO
//run("Blobs (25K)");
nBins = 256;
getHistogram(values, counts, 256);

//create a cumulative histogram
cumHist = newArray(nBins);
cumHist[0] = values[counts[0]];
for (i = 1; i < nBins; i++){ cumHist[i] = counts[i] + cumHist[i-1]; }

//normalize the cumulative histogram
normCumHist = newArray(nBins);
for (i = 0; i < nBins; i++){  normCumHist[i] = cumHist[i]/
cumHist[nBins-1]; }


// find the 5th percentile (= 0.05)
target = 0.05;
i = 0;
do {
        i = i + 1;
        print("i=" + i + "  value=" + values[i] +  "  count=" +
counts[i] + "  
cumHist= " + cumHist[i] + "  normCumHist= " + normCumHist[i] );
} while (normCumHist[i] < target)

print("5th percentile located at either " + (i-1) + "th or " + (i) +  
"th bin");
print("    which has a location of " + values[i-1] +" or " + values[i]);

//END MACRO



Ben Tupper
Reply | Threaded
Open this post in threaded view
|

Re: Percentile value

Pang, Zhengyu (GE Global Research)
Hi all,

To get the percentile value, I think that I could get all the pixel
value by using getpixel (I,j) and make an array.
Then I need to sort the array. How do I sort the array using ImageJ
macro language? Do you have to make a function?

Thanks!

Zhengyu

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
Pang, Zhengyu (GE, Research)
Sent: Thursday, June 11, 2009 10:15 AM
To: [hidden email]
Subject: Re: Percentile value

Ben,

Thanks for your help. I tested your macro and it worked. As you said,
this is only an approximation.

I am wondering if ImageJ could read an image and then convert image to a
matrix like what Matlab can do. Could you then sort the matrix and found
5% percentile?

Best Regards,

Zhengyu

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
Ben Tupper
Sent: Wednesday, June 10, 2009 8:34 PM
To: [hidden email]
Subject: Re: Percentile value

Hi,

On Jun 9, 2009, at 11:19 AM, Pang, Zhengyu (GE, Research) wrote:

> Dear all,
>
> ImageJ could provide you with Mean, median, min and max. How do I get
> 1 percentile, or 5 percentile values? And how do I get the average for

> first percentile.?
>

I Wonder if the cumulative histogram (normalized) could work for you.  
The following converts the histogram to a cumulative histogram, then  
normalizes it to the number of pixels in the image.   I'm no stats  
guy, so this might be considered an approximation by purists.

Cheers,
Ben

//START MACRO
//run("Blobs (25K)");
nBins = 256;
getHistogram(values, counts, 256);

//create a cumulative histogram
cumHist = newArray(nBins);
cumHist[0] = values[counts[0]];
for (i = 1; i < nBins; i++){ cumHist[i] = counts[i] + cumHist[i-1]; }

//normalize the cumulative histogram
normCumHist = newArray(nBins);
for (i = 0; i < nBins; i++){  normCumHist[i] = cumHist[i]/
cumHist[nBins-1]; }


// find the 5th percentile (= 0.05)
target = 0.05;
i = 0;
do {
        i = i + 1;
        print("i=" + i + "  value=" + values[i] +  "  count=" +
counts[i] + "  
cumHist= " + cumHist[i] + "  normCumHist= " + normCumHist[i] ); } while
(normCumHist[i] < target)

print("5th percentile located at either " + (i-1) + "th or " + (i) + "th
bin");
print("    which has a location of " + values[i-1] +" or " + values[i]);

//END MACRO



Ben Tupper
Reply | Threaded
Open this post in threaded view
|

Re: Percentile value

Wayne Rasband
> Hi all,
>
> To get the percentile value, I think that I could get all the pixel
> value by using getpixel (I,j) and make an array.
> Then I need to sort the array. How do I sort the array using ImageJ
> macro language? Do you have to make a function?

Use the Array.sort() macro function to sort an array. Here is an
example macro that creates an array from all pixels in an image then
sorts the array:

    requires ("1.42j");
    w = getWidth;
    h = getHeight;
    a = newArray(w*h);
    i = 0;
    for (y=0; y<h; y++)
       for (x=0; x<w; x++)
           a[i++] = getPixel(x,y);
    Array.sort(a);

-wayne


> Thanks!
>
> Zhengyu
>
> -----Original Message-----
> From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
> Pang, Zhengyu (GE, Research)
> Sent: Thursday, June 11, 2009 10:15 AM
> To: [hidden email]
> Subject: Re: Percentile value
>
> Ben,
>
> Thanks for your help. I tested your macro and it worked. As you said,
> this is only an approximation.
>
> I am wondering if ImageJ could read an image and then convert image to
> a
> matrix like what Matlab can do. Could you then sort the matrix and
> found
> 5% percentile?
>
> Best Regards,
>
> Zhengyu
>
> -----Original Message-----
> From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
> Ben Tupper
> Sent: Wednesday, June 10, 2009 8:34 PM
> To: [hidden email]
> Subject: Re: Percentile value
>
> Hi,
>
> On Jun 9, 2009, at 11:19 AM, Pang, Zhengyu (GE, Research) wrote:
>
>> Dear all,
>>
>> ImageJ could provide you with Mean, median, min and max. How do I get
>> 1 percentile, or 5 percentile values? And how do I get the average for
>
>> first percentile.?
>>
>
> I Wonder if the cumulative histogram (normalized) could work for you.
> The following converts the histogram to a cumulative histogram, then
> normalizes it to the number of pixels in the image.   I'm no stats
> guy, so this might be considered an approximation by purists.
>
> Cheers,
> Ben
>
> //START MACRO
> //run("Blobs (25K)");
> nBins = 256;
> getHistogram(values, counts, 256);
>
> //create a cumulative histogram
> cumHist = newArray(nBins);
> cumHist[0] = values[counts[0]];
> for (i = 1; i < nBins; i++){ cumHist[i] = counts[i] + cumHist[i-1]; }
>
> //normalize the cumulative histogram
> normCumHist = newArray(nBins);
> for (i = 0; i < nBins; i++){  normCumHist[i] = cumHist[i]/
> cumHist[nBins-1]; }
>
>
> // find the 5th percentile (= 0.05)
> target = 0.05;
> i = 0;
> do {
> i = i + 1;
> print("i=" + i + "  value=" + values[i] +  "  count=" +
> counts[i] + "
> cumHist= " + cumHist[i] + "  normCumHist= " + normCumHist[i] ); } while
> (normCumHist[i] < target)
>
> print("5th percentile located at either " + (i-1) + "th or " + (i) +
> "th
> bin");
> print("    which has a location of " + values[i-1] +" or " +
> values[i]);
>
> //END MACRO
>
>
>
> Ben Tupper
>
Reply | Threaded
Open this post in threaded view
|

Re: Percentile value

Pang, Zhengyu (GE Global Research)
Wayne,

That is great! This is the best solution I am looking for. I just did
not realize that Array.sort function is available now. I had an old
verion of Image J built-in macro function.

Thanks

Zhengyu

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
Wayne Rasband
Sent: Thursday, June 11, 2009 4:34 PM
To: [hidden email]
Subject: Re: Percentile value

> Hi all,
>
> To get the percentile value, I think that I could get all the pixel
> value by using getpixel (I,j) and make an array.
> Then I need to sort the array. How do I sort the array using ImageJ
> macro language? Do you have to make a function?

Use the Array.sort() macro function to sort an array. Here is an example
macro that creates an array from all pixels in an image then sorts the
array:

    requires ("1.42j");
    w = getWidth;
    h = getHeight;
    a = newArray(w*h);
    i = 0;
    for (y=0; y<h; y++)
       for (x=0; x<w; x++)
           a[i++] = getPixel(x,y);
    Array.sort(a);

-wayne


> Thanks!
>
> Zhengyu
>
> -----Original Message-----
> From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
> Pang, Zhengyu (GE, Research)
> Sent: Thursday, June 11, 2009 10:15 AM
> To: [hidden email]
> Subject: Re: Percentile value
>
> Ben,
>
> Thanks for your help. I tested your macro and it worked. As you said,
> this is only an approximation.
>
> I am wondering if ImageJ could read an image and then convert image to

> a matrix like what Matlab can do. Could you then sort the matrix and
> found 5% percentile?
>
> Best Regards,
>
> Zhengyu
>
> -----Original Message-----
> From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
> Ben Tupper
> Sent: Wednesday, June 10, 2009 8:34 PM
> To: [hidden email]
> Subject: Re: Percentile value
>
> Hi,
>
> On Jun 9, 2009, at 11:19 AM, Pang, Zhengyu (GE, Research) wrote:
>
>> Dear all,
>>
>> ImageJ could provide you with Mean, median, min and max. How do I get
>> 1 percentile, or 5 percentile values? And how do I get the average
>> for
>
>> first percentile.?
>>
>
> I Wonder if the cumulative histogram (normalized) could work for you.
> The following converts the histogram to a cumulative histogram, then
> normalizes it to the number of pixels in the image.   I'm no stats
> guy, so this might be considered an approximation by purists.
>
> Cheers,
> Ben
>
> //START MACRO
> //run("Blobs (25K)");
> nBins = 256;
> getHistogram(values, counts, 256);
>
> //create a cumulative histogram
> cumHist = newArray(nBins);
> cumHist[0] = values[counts[0]];
> for (i = 1; i < nBins; i++){ cumHist[i] = counts[i] + cumHist[i-1]; }
>
> //normalize the cumulative histogram
> normCumHist = newArray(nBins);
> for (i = 0; i < nBins; i++){  normCumHist[i] = cumHist[i]/
> cumHist[nBins-1]; }
>
>
> // find the 5th percentile (= 0.05)
> target = 0.05;
> i = 0;
> do {
> i = i + 1;
> print("i=" + i + "  value=" + values[i] +  "  count=" +
counts[i] + "

> cumHist= " + cumHist[i] + "  normCumHist= " + normCumHist[i] ); }
> while (normCumHist[i] < target)
>
> print("5th percentile located at either " + (i-1) + "th or " + (i) +
> "th bin");
> print("    which has a location of " + values[i-1] +" or " +
> values[i]);
>
> //END MACRO
>
>
>
> Ben Tupper
>
Reply | Threaded
Open this post in threaded view
|

Re: Percentile value

Jan Eglinger-5
In reply to this post by Wayne Rasband
Hi all,

following up on the issue of measuring percentiles in ImageJ...

Wayne Rasband wrote:

>> To get the percentile value, I think that I could get all the pixel
>> value by using getpixel (I,j) and make an array.
>> Then I need to sort the array. How do I sort the array using ImageJ
>> macro language? Do you have to make a function?
>
> Use the Array.sort() macro function to sort an array. Here is an example
> macro that creates an array from all pixels in an image then sorts the
> array:
>
>    requires ("1.42j");
>    w = getWidth;
>    h = getHeight;
>    a = newArray(w*h);
>    i = 0;
>    for (y=0; y<h; y++)
>       for (x=0; x<w; x++)
>           a[i++] = getPixel(x,y);
>    Array.sort(a);
>

Is there an easy way to limit that array to the thresholded pixels or to
an ROI only?

Or is there even a chance to integrate an option "percentile" into the
"Set Measurements..." dialog, together with a parameter telling which
percentile (0.01-0.99)?
That would permit to make use of e.g. the "Redirect" option while
measuring percentiles.

Best,
Jan
Reply | Threaded
Open this post in threaded view
|

Re: Percentile value

Gabriel Landini
On Tuesday 29 September 2009  15:30:07 Jan Eglinger wrote:
> Is there an easy way to limit that array to the thresholded pixels or to
> an ROI only?

Isn't this just a matter of integrating the histogram?
Depending on the definition of "easy", the easy way might be to use the macro
language.

If you know the threshold, then just add bins that are above (or below) it.
If you do not know the threshold and the thresholded area is white, you can
AND the original and the binary threshold image, get the histogram and count
for bins>0 (or <255, depending the image polarity).

For restricting the histogram to an ROI: set the ROI, get the histogram and
count.

> Or is there even a chance to integrate an option "percentile" into the
> "Set Measurements..." dialog, together with a parameter telling which
> percentile (0.01-0.99)?

That is not currently possible, as far as I know.
I hope this helps,
G.
Reply | Threaded
Open this post in threaded view
|

Re: Percentile value

Michael Schmid
In reply to this post by Jan Eglinger-5
Hi Jan,

Concerning looping over all pixels and sorting them - if you don't  
need the pixel coordinates, it would be much faster to use the  
histogram. The getRawStatistics macro function uses the pixels inside  
the selection and returns also the histogram.
In case you have float (32 bit) images and you need better resolution  
than 256 levels, use getHistogram; there you can specify the number  
of bins.

Concerning your second question, it might be a nice addition to  
ImageJ to have an option for the "Percentile" threshold method, where  
one can specify the percentile - currently it is always 50%.

Michael
________________________________________________________________

On 29 Sep 2009, at 16:02, Jan Eglinger wrote:

> Hi all,
>
> following up on the issue of measuring percentiles in ImageJ...
>
> Wayne Rasband wrote:
>>> To get the percentile value, I think that I could get all the pixel
>>> value by using getpixel (I,j) and make an array.
>>> Then I need to sort the array. How do I sort the array using ImageJ
>>> macro language? Do you have to make a function?
>>
>> Use the Array.sort() macro function to sort an array. Here is an  
>> example
>> macro that creates an array from all pixels in an image then sorts  
>> the
>> array:
>>
>>    requires ("1.42j");
>>    w = getWidth;
>>    h = getHeight;
>>    a = newArray(w*h);
>>    i = 0;
>>    for (y=0; y<h; y++)
>>       for (x=0; x<w; x++)
>>           a[i++] = getPixel(x,y);
>>    Array.sort(a);
>>
>
> Is there an easy way to limit that array to the thresholded pixels  
> or to
> an ROI only?
>
> Or is there even a chance to integrate an option "percentile" into the
> "Set Measurements..." dialog, together with a parameter telling which
> percentile (0.01-0.99)?
> That would permit to make use of e.g. the "Redirect" option while
> measuring percentiles.
>
> Best,
> Jan