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]], 
            Frame -> True, PlotJoined -> True, 
            PlotRange -> All, \ 
            PlotLabel -> {"PDF red. num pixels=", \ \ nPixels}]; 
cdfRedp = 
          ListPlot[cdfRed[[All, 2]], 
            PlotJoined -> True, 
            Frame -> 
              True, 
PlotLabel -> {"CDF RED. num pixels=", \ \ nPixels}]; 

pdfGreenp = 
          ListPlot[pdfGreen[[All, 2]], 
            PlotJoined -> True, Frame -> True, \ 
            PlotLabel -> {"PDF green. num pixels=", \ \ nPixels}]; 
cdfGreenp = 
          ListPlot[cdfGreen[[All, 2]], 
            Frame -> True, PlotJoined -> True, 
            PlotLabel -> {"CDF Green. num pixels=", \ \ nPixels}]; 

pdfBluep = 
          ListPlot[pdfBlue[[All, 2]], 
            PlotJoined -> True, \ Frame -> True, 
            PlotLabel -> {"PDF blue. num pixels=", \ \ nPixels}]; 
cdfBluep = 
          ListPlot[cdfBlue[[All, 2]], 
            Frame -> True, \ PlotJoined -> 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}];

 (*hRed = 
        Statistics`DataManipulation`Frequencies[
          N[Flatten[r]]]; 
hRed = 
        nma`flipxy[hRed]; 

hGreen = 
        Statistics`DataManipulation`Frequencies[
          N[Flatten[g]]]; 
hGreen = 
        nma`flipxy[hGreen]; 

hBlue = 
        Statistics`DataManipulation`Frequencies[
          N[Flatten[b]]]; 
hBlue = 
        nma`flipxy[hBlue]; 

cdfRed = 
        Statistics`DataManipulation`CumulativeSums[
          hRed]; 
cdfGreen = 
        Statistics`DataManipulation`CumulativeSums[
          hGreen]; 
cdfBlue = 
        Statistics`DataManipulation`CumulativeSums[
          hBlue];*) 

 (*Graphics`Graphics`BarChart[
        hRed]; 
Graphics`Graphics`BarChart[
        hGreen]; 
Graphics`Graphics`BarChart[
        hBlue]; 
Graphics`Graphics`BarChart[
        cdfRed]; 
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, 
{
{hh, ss, vv} = 
                RGBtoHSV[r[\([i, j]\)], g[\([i, j]\)], 
                  b[\([i, j]\)]]; 
h[\([i, j]\)] = 
                hh; 
s[\([i, j]\)] = 
                ss; 
v[\([i, j]\)] = 
                vv;
}
]
}
];\)

\)
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]];

  

\(\(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, 
{

If[ (  ( 
                          h[[i, j]] ≥  0  &&  
                            h[[i, j]] ≤  
                              25) || (h[[i, j]] ≥ 
                            340))  &&  (s[[i, 
                              j]] ≥   .6   &&  
                        v[[i, j]] ≥   .6) , {b[[i, 
                          j]] = 1, 
                    count[[kRed]] = 
                      count[[kRed]] + 
                        1}]; 

If[ (h[[i, j]] > 25 &&  h[[i, j]] ≤  70  && 
                      s[[i, j]] ≥   .6  &&  
                      v[[i, j]] ≥   .6), {b[[i, 
                          j]] = 2, 
                    count[[kYellow]] = 
                      count[[kYellow]] + 
                        1}]; 

If[ (h[[i, j]] >  70  &&  h[[i, j]] ≤  170   && 
                      s[[i, j]] ≥   .6  &&  
                      v[[i, j]] ≥   .6), {b[[i, 
                          j]] = 3, 
                    count[[kGreen]] = 
                      count[[kGreen]] + 
                        1}]; 

If[ (h[[i, j]] >  170  &&  h[[i, j]] ≤  200  &&  
                      s[[i, j]] ≥   .6  &&  
                      v[[i, j]] ≥   .6), {b[[i, 
                          j]] = 4, 
                    count[[kCyan]] = 
                      count[[kCyan]] + 
                        1}]; 

If[ (h[[i, j]] >  200  &&  h[[i, j]] ≤  280  &&  
                      s[[i, j]] ≥   .6  &&  
                      v[[i, j]] ≥   .6), {b[[i, 
                          j]] = 5, 
                    count[[kBlue]] = 
                      count[[kBlue]] + 
                        1}]; 

If[ (h[[i, j]] >  280  &&  h[[i, j]] <  340  &&  
                      s[[i, j]] ≥   .6  &&  
                      v[[i, j]] ≥   .6), {b[[i, 
                          j]] = 6, 
                    count[[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}]];