Counting number of pixels above threshold value

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

Counting number of pixels above threshold value

Lee Hampson
Hello

I am working with 16-bit images and want to count the number of pixels within a ROI that are above a certain grey value.  

Would anyone be able to give me some pointers to create a macro to count the number of pixels in my histogram that have grey values ranging from a threshold value up to the maximum grey value

Thanks
Lee

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

Re: Counting number of pixels above threshold value

José María Mateos
On 14/06/2013 15:43, Lee Hampson wrote:
> I am working with 16-bit images and want to count the number of
> pixels within a ROI that are above a certain grey value.
>
> Would anyone be able to give me some pointers to create a macro to
> count the number of pixels in my histogram that have grey values
> ranging from a threshold value up to the maximum grey value

This is the most immediate approach:

-----
threshold = 100;

run("Blobs (25K)");
//setTool("oval");
//setTool("rectangle");
makeRectangle(60, 65, 153, 126);
run("Crop");

w = getWidth();
h = getHeight();
count = 0;
for (x = 0; x < w; x++) {
        for(y = 0; y < h; y++) {
                v = getPixel(x, y);
                if (v > threshold)
                        count++;
        }
}

print(count);
-----

Also, you could perform a regular threshold and measure the statistics
within the ROI, then divide by 255 the total sum of pixels.

Best,

José.

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

Re: Counting number of pixels above threshold value

dscho
In reply to this post by Lee Hampson
Hi Lee,

On Fri, 14 Jun 2013, Lee Hampson wrote:

> I am working with 16-bit images and want to count the number of pixels
> within a ROI that are above a certain grey value.  
>
> Would anyone be able to give me some pointers to create a macro to count
> the number of pixels in my histogram that have grey values ranging from
> a threshold value up to the maximum grey value

This is how I would go about it:

- start the macro recorder

- Image>Duplicate...

- Edit>Selection>Clear Outside

- Process>Math>Divide... (and specify the known cutoff)

- set threshold 1

- Analyze>Measure

The idea being: work in a copy of the image because we will change the
pixels, clear the outside of the ROI, divide by the threshold so that all
"inside" pixels have positive value, then measure that.

I would not go directly to the threshold because ImageJ sometimes goes
through 8-bit histograms before thresholding.

Ciao,
Johannes

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

Re: Counting number of pixels above threshold value

Michael Schmid
Hi Lee, Johannes,

beware that ImageJ does rounding upon division, it does not truncate. So, if you divide by 200, the previous values 0-99 become 0, 100-299 become 1.
Thus, the final threshold of 1 will get everything that was half the cutoff or above.

Here is a macro using the getHistogram function (for images without gray value calibration):

  threshold = 200; //or whatever you like; uncalibrated value
  getHistogram(values, counts, 65536,0,65536-1);
  n=0;
  for (i=threshold; i<65536; i++) n=n+counts[i];
  print (n);


Michael
________________________________________________________________
On Jun 14, 2013, at 18:35, Johannes Schindelin wrote:

> Hi Lee,
>
> On Fri, 14 Jun 2013, Lee Hampson wrote:
>
>> I am working with 16-bit images and want to count the number of pixels
>> within a ROI that are above a certain grey value.  
>>
>> Would anyone be able to give me some pointers to create a macro to count
>> the number of pixels in my histogram that have grey values ranging from
>> a threshold value up to the maximum grey value
>
> This is how I would go about it:
>
> - start the macro recorder
>
> - Image>Duplicate...
>
> - Edit>Selection>Clear Outside
>
> - Process>Math>Divide... (and specify the known cutoff)
>
> - set threshold 1
>
> - Analyze>Measure
>
> The idea being: work in a copy of the image because we will change the
> pixels, clear the outside of the ROI, divide by the threshold so that all
> "inside" pixels have positive value, then measure that.
>
> I would not go directly to the threshold because ImageJ sometimes goes
> through 8-bit histograms before thresholding.
>
> Ciao,
> Johannes
>
> --
> 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: Counting number of pixels above threshold value

vischer
Hi Lee,

Isn't it enough to "Set Measurements" with  option
"Limit to Threshold"
so the "Measure" command gives you the area that you want?

Norbert

>
> Hi Lee, Johannes,
>
> beware that ImageJ does rounding upon division, it does not truncate. So, if you divide by 200, the previous values 0-99 become 0, 100-299 become 1.
> Thus, the final threshold of 1 will get everything that was half the cutoff or above.
>
> Here is a macro using the getHistogram function (for images without gray value calibration):
>
>  threshold = 200; //or whatever you like; uncalibrated value
>  getHistogram(values, counts, 65536,0,65536-1);
>  n=0;
>  for (i=threshold; i<65536; i++) n=n+counts[i];
>  print (n);
>
>
> Michael
> ________________________________________________________________
> On Jun 14, 2013, at 18:35, Johannes Schindelin wrote:
>
>> Hi Lee,
>>
>> On Fri, 14 Jun 2013, Lee Hampson wrote:
>>
>>> I am working with 16-bit images and want to count the number of pixels
>>> within a ROI that are above a certain grey value.  
>>>
>>> Would anyone be able to give me some pointers to create a macro to count
>>> the number of pixels in my histogram that have grey values ranging from
>>> a threshold value up to the maximum grey value
>>
>> This is how I would go about it:
>>
>> - start the macro recorder
>>
>> - Image>Duplicate...
>>
>> - Edit>Selection>Clear Outside
>>
>> - Process>Math>Divide... (and specify the known cutoff)
>>
>> - set threshold 1
>>
>> - Analyze>Measure
>>
>> The idea being: work in a copy of the image because we will change the
>> pixels, clear the outside of the ROI, divide by the threshold so that all
>> "inside" pixels have positive value, then measure that.
>>
>> I would not go directly to the threshold because ImageJ sometimes goes
>> through 8-bit histograms before thresholding.
>>
>> Ciao,
>> Johannes

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

Re: Counting number of pixels above threshold value

dscho
In reply to this post by Michael Schmid
Hi Michael,

On Fri, 14 Jun 2013, Michael Schmid wrote:

> beware that ImageJ does rounding upon division, it does not truncate.
> So, if you divide by 200, the previous values 0-99 become 0, 100-299
> become 1.

Ouch!

Thanks for pointing this out, I did not know!
Johannes

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