# Calculate mean and standard deviation from ImageHistogram

I am doing my PhD in Clinical Neuropsychology; I had someone helping me analyse images (.jpg pictures) in Mathematica, but it turns out this person made some really bad decisions, and well, now I have to do it myself. But, I have no idea.

Basically, what I want is to analyze the JPEG image, get a RGB histogram, and get the values for the mean and standard deviation of this histogram.

Using ImageHistogram [[*image*], Appearance, -> "Separated"] works fine, but I can't figure out the next step, turning the histogram plot into numbers.

How can I do that?

Thank you for helping!

• You could use ImageData[] to get the values being binned for the histograms... Commented Oct 12, 2012 at 13:27
• Thank you for responding!!I am trying it right now! Commented Oct 12, 2012 at 13:31
• Hmmm... it says A very large out put was generated, here is a sample of it. "{0., 0., 0.}, {0., 0., 0.}, {0., 0., 0.}," Commented Oct 12, 2012 at 13:33
• And if I input: StandardDeviation[Out[37]] I get pretty much the same result. Commented Oct 12, 2012 at 13:34
• You might need the Interleaving option... Commented Oct 12, 2012 at 13:40

There is one suggestion.

img = ExampleData[{"TestImage", "Lena"}];
imgChannelsRGB = Transpose@Flatten[ImageData[img], 1];


Creating Histogram:

GraphicsRow[Histogram /@ imgChannelsRGB, ImageSize -> 500]


Geting statistics:

TableForm[Through[{Mean, StandardDeviation}[#]] & /@ imgChannelsRGB,
TableHeadings -> {{"Red", "Blue", "Green"}, {"Mean", "StDev"}}]


• Alternatively: imgChannelsRGB = Flatten /@ ImageData[img, Interleaving -> False]. The Lenna example image is easily accessed as ExampleData[{"TestImage", "Lena"}]. Commented Oct 12, 2012 at 14:00
• Nice tip. Changed the example. Commented Oct 12, 2012 at 14:03
• Am trying it right now! Commented Oct 12, 2012 at 14:09
• O wow, this is very promissing! I'm going to play with this (and then I might be back). Thnx! Commented Oct 12, 2012 at 14:43
• You could use ImageData[img] ~Flatten~ {{3}, {1, 2}} in place of Transpose@Flatten[ImageData[img], 1], eliminating Transpose. Commented Oct 12, 2012 at 15:19

I thought it might be interesting to combine the graphs and statistics in small multiple style, but I got stuck... How do you present two numbers as a label without showing the curly brackets?

Code:

{r, g, b} = ColorSeparate[img];
Row[
Labeled[
ImageHistogram[#, Appearance -> "Transparent",  Joined -> False,
Frame -> None],
{
Mean[Flatten[ImageData[#]]],
StandardDeviation[Flatten[ImageData[#]]]
},
ImageSize -> 200
] & /@ {r, g, b}
]


Edit with bill (?!)'s suggestion:

GraphicsRow[
Labeled[
ImageHistogram[#, Appearance -> "Transparent", Joined -> False,
Frame -> None],
Style[
Column[
{
Mean[Flatten[ImageData[#]]],
StandardDeviation[Flatten[ImageData[#]]]
}
],
14, Bold],
ImageSize -> 500
] & /@ {r, g, b}]


• Maybe add a Column ? Row[Labeled[ ImageHistogram[#, Appearance -> "Transparent", Joined -> False, Frame -> None], Column[{Mean[Flatten[ImageData[#]]], StandardDeviation[Flatten[ImageData[#]]]}], ImageSize -> 200] & /@ {r, g, b}] Commented Oct 12, 2012 at 19:26
• @b.gatessucks Good idea! As usual, your comments are better than my answers! :) Commented Oct 12, 2012 at 19:30
• Wow! I have been getting some help from a friend, and we will definitely come back to this! Commented Oct 15, 2012 at 9:46
• OK, so we are on a roll, but now I have a new, buyt related qustion. Should i post it here or open a new topic? Commented Oct 19, 2012 at 8:54