# Finding mean brightness, standard deviation, count of pixels in gray scale images

I have many sequential gray scale images (2048*2048 pixels, 8bit).

To determine the brightness, standard deviation, and count of pixels with in two bins, zero and non-zero brightness, I use the code below, which I hope can be improved.

For a single image I need about 0.5 sec.

SetSharedVariable[stdDeviation, mean, zeroPixels, nonzeroPixels];

(* for testing with a single image*)
numFiles = 1;
fNames={"F:\\20160122_example_image.png"};

stdDeviation = Array[0 &, numFiles];
mean = Array[0 &, numFiles];
zeroPixels = Array[0 &, numFiles];
nonzeroPixels = Array[0 &, numFiles];

ParallelTable[
image = Import[fNames[[i]]];
imageData = Flatten[ImageData[image]];
mean[[i]] = Mean[imageData];
zeroPixels[[i]] = Length[ImageValuePositions[image, 0.]];
nonzeroPixels[[i]] = 2^22 - zeroPixels[[i]];
stdDeviation[[i]] = StandardDeviation[imageData],

{i, 1, numFiles}

]; // AbsoluteTiming


{0.460753, Null}

Here is an example image: https://drive.google.com/open?id=0B9wKP6yNcpyfbTlRU096dWhxT1U

This image is an CMOS sensor image taken with the lens was covered. Therefore it is mainly black. I am interested in investigating sensor noise and the number of 'defect' pixels.

## 1 Answer

The following is approx 30 times faster:

(imageData      = Flatten@ImageData@image;
mean          = Mean@imageData;
nonzeroPixels = Total@Unitize@imageData;
zeroPixels    = Length@imageData - nonzeroPixels;
stdDeviation  = StandardDeviation@imageData) // Timing

• Thanks for your help. For the single image case I get the same (factor >=30). Results for 681 images with image reading (Import): a) AbsoluteTiming: my code needs 94 sec, your improved code needs 47 sec. b) Timing: my code needs 0.48 sec, your improved code 0.25 sec. – mrz Jan 22 '16 at 17:26
• Could you copy this single example file simply 1000 times and try out how long the whole code with Import embedded in ParallelTable needs? To me it is not clear why effectively only a factor of 2 comes out. – mrz Jan 22 '16 at 17:35
• Nah, that's the dirty profiling job. You should do it yourself and check where all those seconds are going. Afterwards you may ask for more localized help if you can't do it. – Dr. belisarius Jan 22 '16 at 17:54
• The slowest part is probably importing the images. For the single image case the OS will cache the file in memory so repeated timings will be faster. – Simon Woods Jan 22 '16 at 19:10
• @SimonWoods The Import heavily depends on your OS and its configuration. Also on your HW, of course (SSDs, anyone :)?) – Dr. belisarius Jan 22 '16 at 19:34