Counting pixels above a certain grey value in a stack

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

Counting pixels above a certain grey value in a stack

Jon Bass
Hi,


     I have a stack of images (512X512 pixels) where I want to count the
number of pixels above a certain grey value and put the data in a results
table showing the pixel counts for each image (slice), much like the
"Analyze Particles..." function works.  For example, when an image is
saturated with all pixels at the maximum of 65535 grey values, I would like
the output for that slice to be 262144 rather than 1 or 0 which is what I
get when I try to use the "Analyze particles..." function.  I'd like the
data put in a table so that I can copy it to Excel and plot the decay in the
number of pixels above a certain value over time (i.e. over all the slices
in the stack).  Is there a way to do this?  

     I should mention that I am an organic chemist with very little
programming experience, so if the solution involves a lot of macro writing,
I will probably need to find another way to analyze my data.  Thanks!


Jon
Reply | Threaded
Open this post in threaded view
|

Re: Counting pixels above a certain grey value in a stack

Marcel
This plugin should do the trick.
If you need the class file contact me and i will send it to you.

Please test it before you use it!

With kind regards

Marcel

****************************************************

/*
This plugin counts pixels in a 8-bit or 16-bit image (Stack) in the defined interval.
Author: M. Austenfeld
*/

import java.awt.*;
import ij.plugin.*;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.measure.ResultsTable;
import ij.gui.GenericDialog;

public class Count_Stack_Pixels implements PlugIn {

        public int pixelCount = 0;// The variable for the pixel count!
        public int min = 0;// The min value for the pixel count!
        public int max = 0;// The max value for the pixel count!

        public void run(String arg) {
                ImagePlus imp = WindowManager.getCurrentImage();
                if (imp != null) {
                        /*We proof if the image is an 8-bit or 16-bit image!*/
                                     ImageProcessor im= imp.getProcessor() ;
                        if (im instanceof ShortProcessor||im instanceof ByteProcessor ) {

                                /*We create a dialog!*/
                                GenericDialog gd = new GenericDialog("Count Pixels");
                                gd.addNumericField("Min: ", min, 0);
                                gd.addNumericField("Max: ", max, 0);
                                gd.showDialog();
                                if (gd.wasCanceled())
                                        return;

                                /*We get the values from the dialog!*/
                                min = (int) gd.getNextNumber();
                                max = (int) gd.getNextNumber();

                                /*We create a results table!*/
                                ResultsTable rt = ResultsTable.getResultsTable();
                                rt.reset();

                                /*Get the active image !*/

                                /*We get the stack!*/
                                ImageStack stack = imp.getStack();
                                /*We loop through all stack images!*/
                                for (int n = 1; n <= stack.getSize(); n++) {
                                        /*Get the image processor of the image !*/
                                        ImageProcessor ip = stack.getProcessor(n);
                                        int w = ip.getWidth();
                                        int h = ip.getHeight();

                                        /*We loop through all pixels in a stack image!*/
                                        for (int v = 0; v < h; v++) {
                                                for (int u = 0; u < w; u++) {

                                                        /*Get the pixel value!*/
                                                        int i = ip.getPixel(u, v);

                                                        /*All values which match the interval are counted!*/
                                                        if (i >= min && i <= max) {

                                                                pixelCount++;

                                                        }

                                                }
                                        }

                                        rt.incrementCounter();
                                        /*We add the counted values to the results table!*/
                                        rt.addValue("Count", pixelCount);
                                        pixelCount = 0;

                                }//end of the stack!
                                rt.show("Results");
                        }

                        else {
                               
                                                IJ.showMessage("Requires an 8-bit or 16-bit image !");
                        }

                } else {
                       
                                    IJ.showMessage("No image opened !");
                }

        }

}
Reply | Threaded
Open this post in threaded view
|

Re: Counting pixels above a certain grey value in a stack

dscho
Hi,

On Tue, 4 Dec 2007, Bio7 wrote:

> public class Count_Stack_Pixels implements PlugIn {

If you implement a PlugInFilter (which returns DOES_8G | DOES_16 |
NO_CHANGES in setup()) you do not need to find out the current image
or check the image type yourself.

> /*We create a dialog!*/
> GenericDialog gd = new GenericDialog("Count Pixels");
> gd.addNumericField("Min: ", min, 0);
> gd.addNumericField("Max: ", max, 0);

You could reuse the min/max setting from the threshold.

Ciao,
Dscho
Reply | Threaded
Open this post in threaded view
|

Re: Counting pixels above a certain grey value in a stack

jmutterer
Hi.
I'm not sure a plugin is necessary in that case. Selecting pixel value
ranges is just exactly what the threshold does. I suggest the following
procedure :
* go to Image/Adjust/Threshold and set the value range you would like to
measure.
* go to Analyze/Set Measurements, and make sure you check the "limit to
threshold" checkbox and the "area" field.
* go to Image/Stacks/Plot Z-axis Profile
You're done.

Jerome


On Dec 4, 2007 2:42 PM, Johannes Schindelin <[hidden email]>
wrote:

> Hi,
>
> On Tue, 4 Dec 2007, Bio7 wrote:
>
> > public class Count_Stack_Pixels implements PlugIn {
>
> If you implement a PlugInFilter (which returns DOES_8G | DOES_16 |
> NO_CHANGES in setup()) you do not need to find out the current image
> or check the image type yourself.
>
> >                               /*We create a dialog!*/
> >                               GenericDialog gd = new
> GenericDialog("Count Pixels");
> >                               gd.addNumericField("Min: ", min, 0);
> >                               gd.addNumericField("Max: ", max, 0);
>
> You could reuse the min/max setting from the threshold.
>
> Ciao,
> Dscho
>
Reply | Threaded
Open this post in threaded view
|

Re: Counting pixels above a certain grey value in a stack

Marcel
You are right,

This is much more easier.

(Many ways leading to Rome. Some of them are longer!)

With kind regards

Marcel
Reply | Threaded
Open this post in threaded view
|

Re: Counting pixels above a certain grey value in a stack

Jon Bass
In reply to this post by Jon Bass
Thanks to everyone who replied!  The method of using the threshold,
making sure the measurements were set to "area" and "limit to threshold" and
then plotting the z-axis profile worked quite nicely, once I remembered to
set the scale so that 1 pixel was indeed 1 pixel (I am still a bit new at
all this).



Jon Bass
Graduate Student
University of California, Irvine
Reply | Threaded
Open this post in threaded view
|

Re: Counting pixels above a certain grey value in a stack

Dimiter Prodanov-2
In reply to this post by Jon Bass
Even simpler. What you ask is basically the cumulative histogram.
So make a macro; get the histogram per slice in the
stack; sum up in an array
and voila - you have your profile of intensities in z direction.

Cheers

Dimiter  Prodanov
Reply | Threaded
Open this post in threaded view
|

Re: Counting pixels above a certain grey value in a stack

Michael Doube-2
You can do this with my macro:
http://doube.org/macros.html#vfb

Though you might have to hack it a bit to suit your system

Cheers

Mike



Dimiter Prodanov wrote:
> Even simpler. What you ask is basically the cumulative histogram.
> So make a macro; get the histogram per slice in the
> stack; sum up in an array
> and voila - you have your profile of intensities in z direction.
>
> Cheers
>
> Dimiter  Prodanov
>  

--
Michael Doube  BPhil BVSc MRCVS
PhD Student
Dental Institute
Queen Mary, University of London
New Rd
London  E1 1BB
United Kingdom

Phone +44 (0)20 7377 7000 ext 2681
Reply | Threaded
Open this post in threaded view
|

Re: Counting pixels above a certain grey value in a stack

blarz15
In reply to this post by jmutterer
Hi Jerome,

Thank you for your helpful post.  I am trying to do what you suggested, but I'm having a hard time getting the threshold function to work properly.  When I access the threshold menu, this is what I get:



For some reason, I am not able to set the value range for my image.  Any advice would be much appreciated!

Thanks,

Barrett  
 


jerome mutterer-2 wrote
Hi.
I'm not sure a plugin is necessary in that case. Selecting pixel value
ranges is just exactly what the threshold does. I suggest the following
procedure :
* go to Image/Adjust/Threshold and set the value range you would like to
measure.
* go to Analyze/Set Measurements, and make sure you check the "limit to
threshold" checkbox and the "area" field.
* go to Image/Stacks/Plot Z-axis Profile
You're done.

Jerome


On Dec 4, 2007 2:42 PM, Johannes Schindelin <Johannes.Schindelin@gmx.de>
wrote:

> Hi,
>
> On Tue, 4 Dec 2007, Bio7 wrote:
>
> > public class Count_Stack_Pixels implements PlugIn {
>
> If you implement a PlugInFilter (which returns DOES_8G | DOES_16 |
> NO_CHANGES in setup()) you do not need to find out the current image
> or check the image type yourself.
>
> >                               /*We create a dialog!*/
> >                               GenericDialog gd = new
> GenericDialog("Count Pixels");
> >                               gd.addNumericField("Min: ", min, 0);
> >                               gd.addNumericField("Max: ", max, 0);
>
> You could reuse the min/max setting from the threshold.
>
> Ciao,
> Dscho
>
Reply | Threaded
Open this post in threaded view
|

Re: Counting pixels above a certain grey value in a stack

Joel Sheffield
I think that the problem is that you are trying to get a threshold for a
16-bit image.  As I understand it, that function works only with 8-bits.
--You might try changing the image type.  It looks as if you don't need the
16 bits of resolution --Joel

On Thu, Jul 16, 2009 at 5:43 PM, blarz15 <[hidden email]> wrote:

> Hi Jerome,
>
> Thank you for your helpful post.  I am trying to do what you suggested, but
> I'm having a hard time getting the threshold function to work properly.
> When I access the threshold menu, this is what I get:
>
> http://n2.nabble.com/file/n3269373/Picture%2B1.jpeg
>
> For some reason, I am not able to set the value range for my image.  Any
> advice would be much appreciated!
>
> Thanks,
>
> Barrett
>
>
>
>
> jerome mutterer-2 wrote:
> >
> > Hi.
> > I'm not sure a plugin is necessary in that case. Selecting pixel value
> > ranges is just exactly what the threshold does. I suggest the following
> > procedure :
> > * go to Image/Adjust/Threshold and set the value range you would like to
> > measure.
> > * go to Analyze/Set Measurements, and make sure you check the "limit to
> > threshold" checkbox and the "area" field.
> > * go to Image/Stacks/Plot Z-axis Profile
> > You're done.
> >
> > Jerome
> >
> >
> > On Dec 4, 2007 2:42 PM, Johannes Schindelin <[hidden email]>
> > wrote:
> >
> >> Hi,
> >>
> >> On Tue, 4 Dec 2007, Bio7 wrote:
> >>
> >> > public class Count_Stack_Pixels implements PlugIn {
> >>
> >> If you implement a PlugInFilter (which returns DOES_8G | DOES_16 |
> >> NO_CHANGES in setup()) you do not need to find out the current image
> >> or check the image type yourself.
> >>
> >> >                               /*We create a dialog!*/
> >> >                               GenericDialog gd = new
> >> GenericDialog("Count Pixels");
> >> >                               gd.addNumericField("Min: ", min, 0);
> >> >                               gd.addNumericField("Max: ", max, 0);
> >>
> >> You could reuse the min/max setting from the threshold.
> >>
> >> Ciao,
> >> Dscho
> >>
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/Counting-pixels-above-a-certain-grey-value-in-a-stack-tp635323p3269373.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>



--


Joel B. Sheffield, Ph.D
Department of Biology
Temple University
Philadelphia, PA 19122
Voice: 215 204 8839
e-mail: [hidden email]
URL:  http://astro.temple.edu/~jbs