7
$\begingroup$

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.

$\endgroup$
0

1 Answer 1

6
$\begingroup$

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
$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$
    – mrz
    Jan 22, 2016 at 17:26
  • $\begingroup$ 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. $\endgroup$
    – mrz
    Jan 22, 2016 at 17:35
  • $\begingroup$ 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. $\endgroup$ Jan 22, 2016 at 17:54
  • $\begingroup$ 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. $\endgroup$ Jan 22, 2016 at 19:10
  • $\begingroup$ @SimonWoods The Import heavily depends on your OS and its configuration. Also on your HW, of course (SSDs, anyone :)?) $\endgroup$ Jan 22, 2016 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.