number of pixels with given value

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

number of pixels with given value

Neil Fazel
Does ImageJ have a way to count the number of pixels having a given value, e.g. 0 or NaN, inside a selection?

I can loop over the pixels, and do the counting in a macro or plugin, but I thought maybe this has already been implemented.

Thanks,
Neil

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

Re: number of pixels with given value

Jim Passmore-2
Hmmm.  Sounds like one datapoint in a histogram (Analyze/Histogram, then
hit the "list" button), although you may have to do something like # of
Nans = total pixels in selection - cumulative sum of histogram entries.


On Sun, Jan 5, 2014 at 6:47 PM, Neil Fazel <[hidden email]> wrote:

> Does ImageJ have a way to count the number of pixels having a given value,
> e.g. 0 or NaN, inside a selection?
>
> I can loop over the pixels, and do the counting in a macro or plugin, but
> I thought maybe this has already been implemented.
>
> Thanks,
> Neil
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>




*-- Jim Passmore*
Research Associate
Sealed Air Corporation

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

Re: number of pixels with given value

Neil Fazel
In reply to this post by Neil Fazel
Thanks for the tip. Histogramming followed by listing works for counting 0's; thought, if one is looking for another value then creating a bin with only that single value can be tricky. Also, it seems lineouts (as opposed to rectangular selections) cannot be histogrammed. (When I tried to do that, the full image was histogrammed.)

Neil

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

Re: number of pixels with given value

Jim Passmore-2
On Wed, Jan 8, 2014 at 11:07 AM, Neil Fazel <[hidden email]>wrote:

> Thanks for the tip. Histogramming followed by listing works for counting
> 0's; thought, if one is looking for another value then creating a bin with
> only that single value can be tricky.


I assume you must be working with 16 bit data.  When I do a histogram on 8
bit data, the histogram defaults to 256 levels, which gives exact values.
 For a list of histogram data for a 16 bit image, do the following:
1. Plugins / New / Macro
2. Copy and paste the macro below (and at
https://gist.github.com/jimpassmore/8325537) into the text area
3. Hit the "run" button

//========== start copy here ===========
// This macro generates a histogram list and
// displays the counts in the "Results" window.
// Based on HistogramLister from the ImageJ
// web site, but will handle 16 bit images using
// more than 256 bins.

getMinAndMax(min, max)
nBins = max - min;
run("Clear Results");
row = 0;
getHistogram(values, counts, nBins, min, max);
for (i=0; i<nBins; i++) {
    setResult("Value", row, values[i]);
    setResult("Count", row, counts[i]);
    row++;
 }
updateResults();
//=========== end copy here =============




> Also, it seems lineouts (as opposed to rectangular selections) cannot be
> histogrammed. (When I tried to do that, the full image was histogrammed.)


After making your line selection,  on the menu select Edit / Selection /
Line to Area, then run the above.

Look at the ImageJ documentation, particularly the Macro Language at
http://imagej.nih.gov/ij/developer/macro/macros.html.  You might find the
built in functions getHistogram, getStatistics, and getRawStatistics to be
particularly useful.

Jim







--

*Jim Passmore*[hidden email]
http://jimpassmore.wordpress.com
http://twitter.com/conpolnews
Linux User #232777

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

Re: number of pixels with given value

Jeremy Adler
Would it be simpler to use the intensity value you want to count as a threshold, generate a binary image and find the mean intensity of the binary image ?
It's not elegant but will work.

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Jim Passmore
Sent: den 8 januari 2014 23:24
To: [hidden email]
Subject: Re: number of pixels with given value

On Wed, Jan 8, 2014 at 11:07 AM, Neil Fazel <[hidden email]>wrote:

> Thanks for the tip. Histogramming followed by listing works for
> counting 0's; thought, if one is looking for another value then
> creating a bin with only that single value can be tricky.


I assume you must be working with 16 bit data.  When I do a histogram on 8 bit data, the histogram defaults to 256 levels, which gives exact values.
 For a list of histogram data for a 16 bit image, do the following:
1. Plugins / New / Macro
2. Copy and paste the macro below (and at
https://gist.github.com/jimpassmore/8325537) into the text area 3. Hit the "run" button

//========== start copy here =========== // This macro generates a histogram list and // displays the counts in the "Results" window.
// Based on HistogramLister from the ImageJ // web site, but will handle 16 bit images using // more than 256 bins.

getMinAndMax(min, max)
nBins = max - min;
run("Clear Results");
row = 0;
getHistogram(values, counts, nBins, min, max); for (i=0; i<nBins; i++) {
    setResult("Value", row, values[i]);
    setResult("Count", row, counts[i]);
    row++;
 }
updateResults();
//=========== end copy here =============




> Also, it seems lineouts (as opposed to rectangular selections) cannot
> be histogrammed. (When I tried to do that, the full image was
> histogrammed.)


After making your line selection,  on the menu select Edit / Selection / Line to Area, then run the above.

Look at the ImageJ documentation, particularly the Macro Language at http://imagej.nih.gov/ij/developer/macro/macros.html.  You might find the built in functions getHistogram, getStatistics, and getRawStatistics to be particularly useful.

Jim







--

*Jim Passmore*[hidden email]
http://jimpassmore.wordpress.com
http://twitter.com/conpolnews
Linux User #232777

--
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: number of pixels with given value

Neil Fazel
In reply to this post by Neil Fazel
Hi Jim,

  Thanks for your help. I work with 32-bit images, so comparing every pixel with the value (e.g. 0) may be the only way to count them. (Fortunately it's fast if done in Java.)

Neil

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