 # Calculation of pixels' mean value

Hi,

I want to measure the average of pixel’s value (intensity) within images, but I do not want to consider the back pixels ( 0 values) as these values making huge difference in the mean value, is there any way to get rid out of zero values and just consider the values above zero, I do not want to use thresholding method as it is masking and making binary images . The intensity of pixels is important.

Hi @aysouda,

If you threshold your image between 1 and 255, you can get rid of 0 values. Then you can redirect (Analyze> Set Measurements…) the measuring of mean value when you use Analyze Particle.

``````run("Blobs (25K)");
run("Duplicate...", " ");
setThreshold(0, 179); //for the sake of demonstration, I didn't use 1,255
setOption("BlackBackground", true);
run("Set Measurements...", "mean fit redirect=blobs.gif decimal=2");
run("Analyze Particles...", "size=100-Infinity display clear");
``````

Nico

2 Likes

Isn’t much simpler and efficient to compute the mean via the histogram?

1 Like

something like that :

``````getHistogram(values, counts, 256);
getDimensions(width, height, channels, slices, frames);

for (i=1, sum=0; i<256; i++)
sum += counts[i]*values[i];

mean = sum/(width*height);

print(mean);
``````
1 Like

Thank you for your reply, I did this method but it did not work, for eg: in the image below, when I am doing the mentioned procedure is giving me low values compared with when I am manually selecting whole grey level part . Aysouda,
The ‘mode’ value in the histogram is the most used value in the image, it does not consider the zeros.
Bob

Do I understand that the redirect command re-assigns the dynamic scale of the image? Would there be a way to visualize this redirected Image?

I can see where you might want to know if your most used value is zero, vs a low intensity value. Is there a way to make this comparison what will mode provide if the most used value is zero?

The redirect works as follow. Let’s say you redirect toward image 1. You make the Analyze particle on image 2.
Analyze particle uses image 2 to find connected components (objects). Then, measurements are performed inside these objects (at the same location) in image 1.

1 Like

That is an awsome feature! I could really use this. I currently make ROIS and save them and then apply then to the other image this is MUCH better!

In the Analyse>Set Measurements… there is an option to ‘Limit to threshold’. If you then do the thresholding (Image>Adjust>Threshold), only the area that is shown as red is measured.
Threshold here is not altering the pixel values, it is only used as a direct mask, see this example and notice the Min value in the Results window indeed shows 128, not 8 as in the second measurement (or 1 and 0 respectively in your image).

``````run("Set Measurements...", "mean modal min integrated limit redirect=None decimal=0");
run("Blobs (25K)");
setAutoThreshold("Default dark");
setThreshold(1,255);
run("Measure");
resetThreshold;
run("Set Measurements...", "mean modal min integrated redirect=None decimal=0");
run("Measure");
``````

The second macro does the same, provided your image as copied from this thread is on the system clipboard.

``````run("Set Measurements...", "mean modal min integrated limit redirect=None decimal=0");
run("System Clipboard");
run("8-bit");
setAutoThreshold("Default dark");
setThreshold(126,255);
run("Measure");
resetThreshold;
run("Set Measurements...", "mean modal min integrated redirect=None decimal=0");
run("Measure");
``````

BTW, your image here is an RGB jpg, I trust you use monochrome TIFF images for your measurements (see the artifact red rectangles pop up when you step the macro with your image).