# Mean brightness and the standard deviation of columns in a gray scale image

I want to calculate the mean brightness and the standard deviation of columns in a gray scale image (8bit).

Can the following calculation code be made faster or/and shorter:

imageData = ImageData[Import["http://i.stack.imgur.com/i6qjE.png"], "Byte"];

dim = Dimensions@imageData;
list = Range@dim[[2]];

meanColumnBrightness = Mean@imageData[[All, #]] & /@ list;
stdDevColumnBrightness = StandardDeviation@imageData[[All, #]] & /@ list;


The result is:

Needs["ErrorBarPlots"];

yWithErrors =
Transpose[{Transpose[{list, meanColumnBrightness}],
ErrorBar /@ stdDevColumnBrightness}]

Show[ErrorListPlot[yWithErrors], Joined -> True, PlotStyle -> {Blue},
Epilog -> {PointSize[Small],
Point[Transpose[{list, meanColumnBrightness}]]}, PlotRange -> All,
Frame -> True, FrameLabel -> {{"y", ""}, {"x", ""}},
BaseStyle -> {FontSize -> 20, FontFamily -> "Calibri"},
ImageSize -> 600]


• why not Transpose？ – Wjx Jul 20 '16 at 8:11
• Please post your solution? – mrz Jul 20 '16 at 8:12
• actually, for mean part, simply do Mean will be okay – Wjx Jul 20 '16 at 8:12
• same image output~ great~ – Wjx Jul 20 '16 at 8:25

A few points to make here:

1. Always use Listable attributes of functions, that will speed things up.

2. When unnecessary, do not use symbolic processing, use numeric processing instead.

Thus, I'll first change the data to N form, then use Listable attributes of Mean and StandardDeviation to get the result in a shorter and faster code.

imgd = N@imageData;
Mean@imgd; // AbsoluteTiming
StandardDeviation@imgd; // AbsoluteTiming


The Mean part accelerated for 10 times while the StandardDeviation part accelerated for 100 times.

# Edit 1

When calculating in rows, try using "/@" instead of simple Transpose will be a good choice:

imgd = N@imageData;
a = Mean /@ imgd; // AbsoluteTiming
b = StandardDeviation /@ imgd; // AbsoluteTiming


Actually I think this will be slower than Transpose at the first place, but as the result shows that this is a better solution, let's use this one.

I'm quite confused why this form is slower, I always consider using one function's Listable form will be better than /@. Intriguing......

So similarly, if you want to speed it up instead of making the code short, for your first problem, you can use this either......

imgd = N@Transpose@imageData;
a = Mean /@ imgd; // AbsoluteTiming
b = StandardDeviation /@ imgd; // AbsoluteTiming
`
• This is incredible ... wau ... how would you do it when you want to calculate the mean and standard deviation in rows instead of columns? – mrz Jul 20 '16 at 8:38
• edited~ in edit 1you can find out how to calculate them in rows. – Wjx Jul 20 '16 at 8:44
• Great solution ... – mrz Jul 20 '16 at 8:46