4
$\begingroup$

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!

$\endgroup$
8
  • 1
    $\begingroup$ You could use ImageData[] to get the values being binned for the histograms... $\endgroup$
    – J. M.'s torpor
    Oct 12 '12 at 13:27
  • $\begingroup$ Thank you for responding!!I am trying it right now! $\endgroup$
    – onemonkey
    Oct 12 '12 at 13:31
  • $\begingroup$ 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.}," $\endgroup$
    – onemonkey
    Oct 12 '12 at 13:33
  • $\begingroup$ And if I input: StandardDeviation[Out[37]] I get pretty much the same result. $\endgroup$
    – onemonkey
    Oct 12 '12 at 13:34
  • $\begingroup$ You might need the Interleaving option... $\endgroup$
    – J. M.'s torpor
    Oct 12 '12 at 13:40
7
$\begingroup$

There is one suggestion.

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

Creating Histogram:

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

enter image description here

Geting statistics:

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

enter image description here

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

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?

small multiple

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}]

better small multiple

$\endgroup$
4
  • 1
    $\begingroup$ 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}] $\endgroup$ Oct 12 '12 at 19:26
  • $\begingroup$ @b.gatessucks Good idea! As usual, your comments are better than my answers! :) $\endgroup$
    – cormullion
    Oct 12 '12 at 19:30
  • $\begingroup$ Wow! I have been getting some help from a friend, and we will definitely come back to this! $\endgroup$
    – onemonkey
    Oct 15 '12 at 9:46
  • $\begingroup$ 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? $\endgroup$
    – onemonkey
    Oct 19 '12 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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