First read in an image

In[793]:=

img = Import["/Users/jmk/class/cs530/queen_butterfly_fish.ppm"] ;

An image is a mathematica "Graphic" element .   You can just "Show" it

In[799]:=

Show[img] ;

[Graphics:HTMLFiles/Mathematica-Images_5.gif]

Get the data out of the image "Graphics", grab it ' s first element .

In[801]:=

imgdata = img[[1, 1]] ;

Dimensions[imgdata]

Out[802]=

{264, 320, 3}

Notice that the value at (y = 1, x = 1)   is a vector of {r, g, b} values :

In[786]:=

imgdata[[1, 1]]

Out[786]=

{0, 56, 89}

This is a 3D array indexed by [[yindex, xindex, colorindex]] .   Here is the red value at pixel location (y = 2, x = 128)

In[804]:=

imgdata[[2, 128, 1]]

Out[804]=

29

In[800]:=

Show[Graphics[Raster[imgdata/255, ColorFunction→ (RGBColor[#[[1]], #[[2]], #[[3]]] &)]], AspectRatio→ (Dimensions[imgdata][[1]]/Dimensions[imgdata][[2]])] ;

[Graphics:HTMLFiles/Mathematica-Images_21.gif]

Let ' s flip the image upside down :

imgdataflip = Table[ imgdata[[ Dimensions[imgdata][[1]] - i ]], {i, 0, Dimensions[imgdata][[1]] - 1}] ;

In[881]:=

Show[Graphics[Raster[imgdataflip/255, ColorFunction→ (RGBColor[#[[1]], #[[2]], #[[3]]] &)]], AspectRatio→ (Dimensions[imgdata][[1]]/Dimensions[imgdata][[2]])] ;

[Graphics:HTMLFiles/Mathematica-Images_25.gif]

Let ' s flip it horizontally :

In[883]:=

imgdataflip = Table[ imgdata[[ i, Dimensions[imgdata][[2]] - j ]], {i, 1, Dimensions[imgdata][[1]]}, {j, 0, Dimensions[imgdata][[2]] - 1}] ;

In[884]:=

Show[Graphics[Raster[imgdataflip/255, ColorFunction→ (RGBColor[#[[1]], #[[2]], #[[3]]] &)]], AspectRatio→ (Dimensions[imgdata][[1]]/Dimensions[imgdata][[2]])] ;

[Graphics:HTMLFiles/Mathematica-Images_29.gif]

Grab individual color channels

In[787]:=

red = imgdata[[All, All, 1]] ;

green = imgdata[[All, All, 2]] ;

blue = imgdata[[All, All, 3]] ;

Notice that the ColorFunction makes the image red

In[785]:=

Show[Graphics[Raster[red/255, ColorFunction→ (Hue[0, 1, #] &)]], AspectRatio→ (Dimensions[imgdata][[1]]/Dimensions[imgdata][[2]])]

[Graphics:HTMLFiles/Mathematica-Images_37.gif]

Out[785]=

-Graphics -

This is the red channel as "gray scale" intensity .   I think it ' s easier to see this way .

In[783]:=

Show[Graphics[Raster[red/255]], AspectRatio→ (Dimensions[imgdata][[1]]/Dimensions[imgdata][[2]])]

[Graphics:HTMLFiles/Mathematica-Images_41.gif]

Out[783]=

-Graphics -

This is the green channel as intensity

In[790]:=

Show[Graphics[Raster[green/255]], AspectRatio→ (Dimensions[imgdata][[1]]/Dimensions[imgdata][[2]])]

[Graphics:HTMLFiles/Mathematica-Images_45.gif]

Out[790]=

-Graphics -

This is the blue channel as intensity

In[791]:=

Show[Graphics[Raster[blue/255]], AspectRatio→ (Dimensions[imgdata][[1]]/Dimensions[imgdata][[2]])]

[Graphics:HTMLFiles/Mathematica-Images_49.gif]

Out[791]=

-Graphics -

In[837]:=

Clear[redhisto]

In[842]:=

redhisto = Table[0, {256}] ;

For[i = 1, i<Dimensions[red][[1]], ++iFor[j = 1, j<Dimensions[red][[2]], ++j, idx = Floor[red[[i, j]]] + 1 ; redhisto[[ idx ]] = redhisto[[ idx ]] + 1] ]

Here ' s what it looks like

In[878]:=

ListPlot[redhisto, PlotJoined→True, AxesLabel→ {"red", "count"}]

[Graphics:HTMLFiles/Mathematica-Images_57.gif]

Out[878]=

-Graphics -

Now let ' s do the red - green joint histogram .

In[849]:=

rgjhisto = Table[0, {256}, {256}] ;

For[i = 1, i<Dimensions[red][[1]], ++i, For[j = 1, j<Dimensions[red][[2]], ++j, idxr = Floor[red[[i, j]]] + 1 ; idxg = Floor[green[[i, j]]] + 1 ;  ++rgjhisto[[idxg, idxr]] ; ] ]

In[851]:=

Dimensions[rgjhisto]

Out[851]=

{256, 256}

Here ' s what it looks like :

In[874]:=

Show[ Graphics[ Raster[rgjhisto], AspectRatio→1, Axes→True, AxesLabel→ {"red", "green"}] ] ;

[Graphics:HTMLFiles/Mathematica-Images_66.gif]

But, it looks yucky .   Let ' s make it look better by manipulating the bin values so that the pixel intensity tells us something about the frequency in the bins .

In[875]:=

Show[ Graphics[ Raster[ Log[1 + rgjhisto]/3], AspectRatio→1, Axes→True, AxesLabel→ {"red", "green"}] ] ;

[Graphics:HTMLFiles/Mathematica-Images_69.gif]

In[885]:=

HTMLSave["/Users/jmk/class/cs530/Image-Manip/Mathematica-Images.html", "Intro-image-manip.nb"]


Created by Mathematica  (September 11, 2007) Valid XHTML 1.1!