Discrete Wavelets with Applications

032 - Color Images and Color Spaces

Helmut  Knaust - Department of Mathematical Sciences, University of Texas at El Paso, hknaust@utep.edu
02/03/2009. Last edit 01/30/2023.

SetDirectory[NotebookDirectory[]];
<<DW.m

Representation of color images

pic0=RandomInteger[{0,255},{4,10,3}]

ImagePlot[pic0,PixelConstrained->40,Scaled->False]

d=2;tab=Flatten[Table[{RGBColor[i/2^d,j/2^d,k/2^d],EdgeForm[],Cuboid[{i,j,k},{i,j,k}+.7]},{i,0,2^d},{j,0,2^d},{k,0,2^d}],2];Show[Graphics3D[tab],AspectRatio->1,Lighting->"Neutral"]

Let' s use a DW package command  to extract the color channels :

?ColorChannels

{r,g,b}=ColorChannels[pic0];
pic0
ImagePlot[pic0,PixelConstrained->40,Scaled->False]
r
ImagePlot[r,PixelConstrained->40,Scaled->False]
g
ImagePlot[g,PixelConstrained->40,Scaled->False]
b
ImagePlot[b,PixelConstrained->40,Scaled->False]

If we add the three matrices, we get the original image back :

ImagePlot[r+g+b,PixelConstrained->40,Scaled->False]

SetDirectory["Images\\Color"]
FileNames[]

file="legos_small.png";pic=Import[file,"Data"];

ImagePlot[pic,Scaled->False]

{r,g,b}=ColorChannels[pic];
ImagePlot[r,Scaled->False]
ImagePlot[g,Scaled->False]
ImagePlot[b,Scaled->False]

Similarly, the next command extracts the three color channels in gray format :

?GrayChannels

{r,g,b}=GrayChannels[pic];
ImagePlot[r,Scaled->False]
ImagePlot[g,Scaled->False]
ImagePlot[b,Scaled->False]

Dimensions[pic]
Dimensions[r]

The next command from DW.m combines these three images into a color image :

?JoinGrayChannels

ImagePlot[JoinGrayChannels[r,g,b]]

RGB Test Image

SetDirectory[NotebookDirectory[]];pic=;
data=ImageData[pic,"Byte"];
ImagePlot[data]

{r,g,b}=GrayChannels[data];{R,G,B}=ColorChannels[data];GraphicsGrid[Transpose[{{ImagePlot[r],ImagePlot[R]},{ImagePlot[g],ImagePlot[G]},{ImagePlot[b],ImagePlot[B]}}],Frame->All]

NTSC color-to-grayscale conversion

The average of the three color channels:

SetDirectory["Images\\Color"];file="legos_small.png";pic=Import[file,"Data"];
{r,g,b}=GrayChannels[pic];
ImagePlot[pic]

ImagePlot[Floor[(r+g+b)/3.]]

Instead of the average of the three color channels, the NTSC (=National Television System Committee) standard takes a weighted average :

ImagePlot[Floor[0.299r+0.587g+0.114b],Scaled->False]

This is also available as a DW.m command :

ImagePlot[NTSC[pic],Scaled->False]

YCbCr color space

The three pieces of information stored (instead of r, g and b values in the RGB model) are :
  1. Y is the Luminance (this is basically the same as the NTSC grayscale) :  	Y =219( .299 r/255 + .587 g/255 + .114 b/255)+16
  	Y will lie between 16 and 235.
  2. Cb is the weighted difference of luminance and blue intensity:  	Cb=224(b/255-Y)/1.772+128
  3. Cr is the weighted difference of luminance and red intensity:  	Cr=224(r/255-Y)/1.402+128
  	Cb and Cr will lie between 16 and 240.

RGB2YCbCr[{r_,g_,b_}]:=Round[{16+1/256.(65.738r+129.057g+25.064b),128+1/256.(-37.945r-74.494g+112.439b),128+1/256.(112.439r-94.154g-18.285b)}]
Simplify[RGB2YCbCr[{red,green,blue}]]
RGB2YCbCr[{255,20,51}]

Pictures of the original image and the Y channel in the first row, and the Cb and Cr channels in the second row:

SetDirectory[NotebookDirectory[]];pic=Import["Images2\\bugsbunny.jpg","Data"];
YCbCr=Map[RGB2YCbCr,pic,{2}];Grid[{{ImagePlot[pic,Scaled->False],ImagePlot[YCbCr[[All,All,1]],Scaled->False],ImagePlot[YCbCr[[All,All,2]],Scaled->False],ImagePlot[YCbCr[[All,All,3]],Scaled->False]}},Frame->All]

For comparison, pictures of the original image and the R channel in the first row, and the G and B channels in the second row:

{r,g,b}=GrayChannels[pic];
Grid[{{ImagePlot[pic],ImagePlot[r,Scaled->False],ImagePlot[g,Scaled->False],ImagePlot[b,Scaled->False]}}]