2
$\begingroup$

I have a large set of subsequent 8bit gray scale png images (50 Hz). They show the noise of a CMOS camera, with the lens covered.

A subset of 50 images is available here: https://goo.gl/Sa3P9P

I want to plot for each image its brightness histogram and export it to a numbered png file.

An histogram example is given here:

enter image description here

The problem is that my code is extremely slow. How can I improve the speed?

ChoiceDialog[{FileNameSetter[Dynamic[imageDir], "Directory"],
    Dynamic[imageDir]}];
SetDirectory[imageDir];

fNames = FileNames["*.png"];
numFiles = Length[fNames];

Table[

  strCounter = ToString@PaddedForm[i, 2, NumberPadding -> {"0", ""}];

  image = Import[fNames[[i]]];
  imageData = Flatten[ImageData[image, "Byte"]];
  imageDataHistogram =
   Histogram[imageData, {1}, Frame -> True, ScalingFunctions -> "Log",
     PlotRange -> {{0, 255}, All}, ImageSize -> 2000, 
    BaseStyle -> {FontWeight -> "Bold", FontSize -> 40, 
      FontFamily -> "Calibri"}, 
    FrameLabel -> {{"# of Pixels", ""}, {"Brightness [0,255]", strCounter}}];

  fileName = StringJoin[imageDir, "\\histogram_", strCounter, ".png"];
  Export[fileName, imageDataHistogram, "PNG"],

  {i, 1, numFiles}

  ];

Unfortunately ParallelTable behaves very strange: Replacing Table by ParallelTable produces once images with axis labeling and once not ... very randomly.

Here are the first 10 histogram plots using Table: https://goo.gl/H3ftw3

and here using ParallelTable: https://goo.gl/sk6uHu

$\endgroup$
3
  • 4
    $\begingroup$ ImageHistogram is much faster than Histogram in my experience. Sorry, I didn't read through your code other than looking for which of these two you used ... I hope this will help though. $\endgroup$
    – Szabolcs
    Jan 15, 2016 at 16:47
  • 1
    $\begingroup$ Table $\rightarrow$ ParallelTable $\endgroup$
    – rhermans
    Jan 15, 2016 at 16:56
  • $\begingroup$ Please see my comment above ... $\endgroup$
    – mrz
    Jan 15, 2016 at 17:30

1 Answer 1

1
$\begingroup$

Thank you very much to Szabolcs and rhermans.

I have used ImageHistogram together with ParallelTable. For 10 images the improved code needs on my computer 2.51 sec (AbsoluteTiming) whereby the old code needed 124.22 sec. The speed is improved to a factor of about 50 ... great ...

The only small but important difference is: I need a logarithmic y scale for ImageHistogram. Is this possible?

ChoiceDialog[{FileNameSetter[Dynamic[imageDir], "Directory"], 
   Dynamic[imageDir]}];
SetDirectory[imageDir];

fNames = FileNames["*.png"];
numFiles = Length[fNames];

ParallelTable[

   strCounter = ToString@PaddedForm[i, 2, NumberPadding -> {"0", ""}];

   image = Import[fNames[[i]]];
   imageHistogram =
    ImageHistogram[image, 255, Frame -> True, FrameTicks -> True, 
     ImageSize -> 2000, AspectRatio -> 2/3, 
     BaseStyle -> {FontWeight -> "Bold", FontSize -> 40, 
     FontFamily -> "Calibri"}, 
     FrameLabel -> {{"# of Pixels", ""}, {"Brightness [0,1]", strCounter}}]
   ;

   fileName = StringJoin[imageDir, "\\histogram_", strCounter, ".png"];   
   Export[fileName, imageHistogram, "PNG"],

   {i, 1, numFiles}

   ];

The solution on how to get a logarithmic vertical scale in ImageHistogram was send to me by the the Mathematics support team:

ImageHistogram with logarithmic y scale

$\endgroup$
2
  • $\begingroup$ Alternative solution: see my answer in mathematica.stackexchange.com/questions/104328/… $\endgroup$
    – mrz
    Mar 15, 2016 at 11:16
  • $\begingroup$ mrz you should accept your answer so this question doesnt keep getting bumped as unanswered. (Unless obviously you still hold out for a better answer..). BTW ParallelDo would seem more appropriate here, though it probably doesn't make any difference. $\endgroup$
    – george2079
    May 8, 2016 at 16:00

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.