(* By Nasser AbbasiEECS 207A, Lab 5 Color detection, see report for algorithm Decmber 2, 2004 *)Remove["Global`*"] ;

<<Miscellaneous`Audio`

<<ImageProcessing`

<<Statistics`DiscreteDistributions`

<<Graphics`Graphics` ;

<<DiscreteMath`Combinatorica` ;

<<Statistics`DataManipulation` ;

<<nma.m

SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]], {1}, FrontEnd`FileName]]] ;

blueSound = ReadSoundFile["blue.wav", PrintHeaderTrue] ;

redSound = ReadSoundFile["red.wav", PrintHeaderFalse] ;

vSound = ReadSoundFile["vertical.wav", PrintHeaderFalse] ;

hSound = ReadSoundFile["horizontal.wav", PrintHeaderFalse] ; 

yellowSound = ReadSoundFile["yellow.wav", PrintHeaderFalse] ;

greenSound = ReadSoundFile["green.wav", PrintHeaderFalse] ;

cyanSound = ReadSoundFile["cyan.wav", PrintHeaderFalse] ;

magentaSound = ReadSoundFile["magenta.wav", PrintHeaderFalse] ; 

kRed = 1 ;

kYellow = 2 ;

kGreen = 3 ;

kCyan = 4 ;

kBlue = 5 ;

kMagenta = 6 ; 

img = ImageRead["allred.jpg"] ; 

Show[Graphics[img], ImageSize {100, 100}] ;

nColor = ImageDepth[img] ; 

Print["Number of color channels in image is " <> ToString[ nColor]] ;

{nRow, nCol} = ImageDimensions[img] ;

Print["image size in pixels is " <> ToString[nRow] <>" rows x " <> ToString[nCol]   <>" cols"] ;

Print["Other image information " <> ToString[img[[2]]] <> " " <> ToString[img[[3]] ]] ;

Print["Displaying the top right 5x5 image area..."] ;

Print[MatrixForm/@ RawImageData[PlanarImageData[ImageTake[img , {1, 5}, {1, 5} ]]]] ; 

Print[".... Extracting the RGB channels from image..."] ; 

raw = RawImageData[img] ; (*same as image[[1]] *)r = N[Take[raw, {1, nRow}, {1, nCol}, {1, 1}]] ;

g = N[Take[raw, {1, nRow}, {1, nCol}, {2, 2}]] ;

b = N[Take[raw, {1, nRow}, {1, nCol}, {3, 3}]] ; 

r = Flatten[r] ; r = Partition[r, nCol] ;

g = Flatten[g] ; g = Partition[g, nCol] ;

b = Flatten[b] ; b = Partition[b, nCol] ; 

h = ImageHistogram[img, {0, 255}] ; 

Print["... generating histograms for each color channel..."] ; 

ShowImageHistogram[h, PointStyleNone] ; 

Print[".... generating Cummulative histogram for each color channel..."] ; 

c = CumulativeHistogram /@ h ;

ShowImageHistogram[c, PointStyleNone] ; 

Print["Displaying the separate color channels"] ; 

Show[GraphicsArray[Graphics/@ToChannels[img]]] ; 

r = N[r] ;

b = N[b] ;

g = N[g] ; 

{nRow, nCol} = Dimensions[r] ; 

nPixels = nRow * nCol ; 

hRed = N[nma`makeHistogram[Flatten[r]]] ;

hGreen = N[nma`makeHistogram[Flatten[g]]] ;

hBlue = N[nma`makeHistogram[Flatten[b]]] ; 

pdfRed = N[nma`makePDF[hRed, nPixels]] ;

cdfRed = N[nma`makeCDF[pdfRed]] ; 

 pdfGreen = N[nma`makePDF[hGreen, nPixels]] ;

cdfGreen = N[nma`makeCDF[pdfGreen]] ; 

 pdfBlue = N[nma`makePDF[hBlue, nPixels]] ;

cdfBlue = N[nma`makeCDF[pdfBlue]] ;

Print["Finsihed Making CDF and PDF..."] ; 

Block[{$DisplayFunction = Identity}, pdfRedp = ListPlot[pdfRed〚All, 2〛 ... ;True, PlotLabel {"CDF Blue. num pixels=",   nPixels}] ; ] ;

Show[GraphicsArray[ {pdfRedp, cdfRedp}, GraphicsSpacing {0.3, 0}]] ;

Show[GraphicsArray[ {pdfGreenp, cdfGreenp}], GraphicsSpacing {0.3, 0}] ;

Show[GraphicsArray[ {pdfBluep, cdfBluep}], GraphicsSpacing {0.3, 0}] ; ɯ ... ;Graphics`Graphics`BarChart[cdfGreen] ; Graphics`Graphics`BarChart[cdfBlue] ; *)

Print["Converting to HSV color space to determine colors..."] ; 

h = Table[0, {i, nRow}, {j, nCol}] ; 

s = h ;

v = h ; 

For[i = 1, i≤nRow, i = i + 1,  {For[j = 1, j≤nCol, j = j + 1, &# ... [i, j]] = ss ; v[[i, j]] = vv ; } ] } ] ; <br />

v = N[v/255] ;

h = h * 360 ;   (*convert to degree*)b = Table[0, {i, nRow}, {j, nCol}] ; 

Print["Dimensions of Hue =", Dimensions[h]] ;

Print["Dimensions of Saturation (purity) =", Dimensions[s]] ;

Print["Dimensions of Value (Brightness) =", Dimensions[v]] ; (*use 1 = RED, 2 = YELLOW, 3 = GREEN, 4 = CYAN, 5 = BLUE, 6 = MAGENTA*)

Print["Conversion completed, now counting colors..."] ; 

count = Table[0, {i, 6}] ; 

For[i = 1, i≤nRow, i = i + 1,  {For[j = 1, j≤nCol, j = j + 1, &# ... unt[[kMagenta]] = count[[kMagenta]] + 1}] ; } ] ; } ] ; 

Print["counting colors completed..."] ; 

Print["Color counting completed into 6 bins:"] ;

Print["Red pixels="<>ToString[count[[kRed]]] <> "\nYellow pixels="<>ToString[count[[kYellow]]] ] ;

    Print["Green pixels="<>ToString[count[[kGreen]]] <> "\nCyan pixels="<>ToString[count[[kCyan]]] ] ;

Print["Blue pixels="<>ToString[count[[kBlue]]] <> "\nMagenta pixels="<>ToString[count[[kMagenta]]] ] ;

m = Max[count] ;

If[mcount[[kRed]], ListPlay[redSound, SampleRate22050, PlayRange {-2^15, 2^15}]] ;    

    If[mcount[[kYellow]], ListPlay[yellowSound, SampleRate22050, PlayRange {-2^15, 2^15}]] ;

If[mcount[[kGreen]], ListPlay[greenSound, SampleRate22050, PlayRange {-2^15, 2^15}]] ;

    If[mcount[[kCyan]], ListPlay[cyanSound, SampleRate22050, PlayRange {-2^15, 2^15}]] ;

    If[mcount[[kBlue]], ListPlay[blueSound, SampleRate22050, PlayRange {-2^15, 2^15}]] ;

If[mcount[[kMagenta]], ListPlay[magentaSound, SampleRate22050, PlayRange {-2^15, 2^15}]] ; 

[Graphics:HTMLFiles/index_92.gif]

Number of color channels in image is 3

image size in pixels is 20 rows x 361 cols

Other image information PixelInterleave -> True ColorFunction -> RGBColor

Displaying the top right 5x5 image area...

{( {{254, 254, 254, 254, 254}, {254, 254, 254, 254, 254}, {254, 254, 254, 254, 254}, ... {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}} )}

.... Extracting the RGB channels from image...

... generating histograms for each color channel...

[Graphics:HTMLFiles/index_100.gif]

.... generating Cummulative histogram for each color channel...

[Graphics:HTMLFiles/index_102.gif]

Displaying the separate color channels

[Graphics:HTMLFiles/index_104.gif]

Finsihed Making CDF and PDF...

[Graphics:HTMLFiles/index_106.gif]

[Graphics:HTMLFiles/index_107.gif]

[Graphics:HTMLFiles/index_108.gif]

Converting to HSV color space to determine colors...

Dimensions of Hue = {20, 361}

Dimensions of Saturation (purity) = {20, 361}

Dimensions of Value (Brightness) = {20, 361}

Conversion completed, now counting colors...

counting colors completed...

Color counting completed into 6 bins:

Red pixels=7220\nYellow pixels=0

Green pixels=0\nCyan pixels=0

Blue pixels=0\nMagenta pixels=0

[Graphics:HTMLFiles/index_119.gif]

Format: Microsoft PCM WAVE RIFF

Duration: 6.40884 seconds

Channels: 1

Sampling rate: 22050

Bits per sample: 16

Data size: 282630 bytes

Number of samples: 141315

General :: spell1 : Possible spelling error: new symbol name \"vSound\" is similar to existing symbol \"Sound\".  More…

General :: spell : Possible spelling error: new symbol name \"hSound\" is similar to existing symbols  {Sound, vSound} .  More…

General :: spell1 : Possible spelling error: new symbol name \"kRed\" is similar to existing symbol \"Red\".  More…

General :: spell1 : Possible spelling error: new symbol name \"kYellow\" is similar to existing symbol \"Yellow\".  More…

General :: spell1 : Possible spelling error: new symbol name \"kGreen\" is similar to existing symbol \"Green\".  More…

General :: spell1 : Possible spelling error: new symbol name \"kCyan\" is similar to existing symbol \"Cyan\".  More…

General :: spell1 : Possible spelling error: new symbol name \"kBlue\" is similar to existing symbol \"Blue\".  More…

General :: spell1 : Possible spelling error: new symbol name \"kMagenta\" is similar to existing symbol \"Magenta\".  More…

[Graphics:HTMLFiles/index_135.gif]

Number of color channels in image is 3

General :: spell1 : Possible spelling error: new symbol name \"nRow\" is similar to existing symbol \"Row\".  More…

image size in pixels is 72 rows x 25 cols

Other image information PixelInterleave -> True ColorFunction -> RGBColor

Displaying the top right 5x5 image area...

{( {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, ... 54}, {254, 254, 254, 254, 254}, {254, 254, 254, 254, 254}, {254, 254, 254, 254, 254}} )}

.... Extracting the RGB channels from image...

General :: spell1 : Possible spelling error: new symbol name \"raw\" is similar to existing symbol \"Raw\".  More…

... generating histograms for each color channel...

[Graphics:HTMLFiles/index_145.gif]

.... generating Cummulative histogram for each color channel...

[Graphics:HTMLFiles/index_147.gif]

Displaying the separate color channels

[Graphics:HTMLFiles/index_149.gif]

Finsihed Making CDF and PDF...

[Graphics:HTMLFiles/index_151.gif]

[Graphics:HTMLFiles/index_152.gif]

[Graphics:HTMLFiles/index_153.gif]

Converting to HSV color space to determine colors...

Dimensions of Hue = {72, 25}

Dimensions of Saturation (purity) = {72, 25}

Dimensions of Value (Brightness) = {72, 25}

Conversion completed, now counting colors...

General :: spell1 : Possible spelling error: new symbol name \"count\" is similar to existing symbol \"Count\".  More…

counting colors completed...

Color counting completed into 6 bins:

Red pixels=0\nYellow pixels=0

Green pixels=0\nCyan pixels=0

Blue pixels=1800\nMagenta pixels=0

[Graphics:HTMLFiles/index_165.gif]


Created by Mathematica  (December 11, 2004) Valid XHTML 1.1!