Two soda cans classification examples

This notebook  assumes that the definitions in the notebook "statistical_modeling.nb" have already been evaluated.

Global variables

File information

\(\(dir = "/Users/nechyba/www/eel6825/course_materials/t5.em_examples2/soda_cans_data";\)\)

segmentedFile1 = "sprite_eng_sml.ppm";
segmentedFile2 = "light_eng_sml.ppm";
segmentedFile1T  =  "sprite_chn_sml.ppm";
segmentedFile2T  =  "light_chn_sml.ppm";

SetDirectory[dir];

Replacement color

replacementColor = {0, 255, 0};

Plotting options

pointStyle1  =  {Orange,  
          PointSize[ .01]};
pointStyle2  =  {Purple,  PointSize[ .01]};

Classification region colors

classColor1  =  RGBColor[ .5, 1, 1];
classColor2  =  RGBColor[1, 1,  .5]; 
    classColors  =  {classColor1,  classColor2};

pr = {{0, 255}, {0, 255}};

Local definitions

Segment loaded image

SegmentObject[img_, thresh_:  245,  
          color_:  
            replacementColor] := 
Module[{img2}, 
img2  =  
            Map[If[#[[1]]  >  thresh  &&  #[[2]]  >  
                      thresh  &&  #[[3]]  >  thresh,  
                  replacementColor, #] & , 
              img[[1, 1]], {2}]; 
ReplacePart[img, 
            img2, {1, 1}]
];

Segment file image

\(\(ConvertImage[outFile_, inFile_, \ type_, \ thresh_:  245, 
          color_:  
            replacementColor] := 
Module[{img}, 
img = Import[inFile, type]; 
img2 = 
            SegmentObject[img, thresh, color]; 
Export[
            outFile, img2, "PPM"];
];\)\)

Extract RGB data from segmented images

ExtractRGB[img_, 
          color_:  
            replacementColor] := 
Module[{rgb}, 
rgb = 
            Partition[Flatten[img[[1, 1]]], 
              3]; 
DeleteCases[rgb, 
            color]
];

Get representative sample of data

RepresentativeSample[dat_,  
          n_] := 
Module[{d2, 
            m}, 
m  =  
            Ceiling[Length[dat]/n]; 
d2  =  
            First  /@  Partition[dat,  m]];

Data plotting routines

Plot representative sample data (columns a and b from dat ) from a larger data set in 2d

\(\(PlotRepresentativeSample2D[dat_, \ n_, {a_, b_}, \ 
          opt___] := 
Module[{d2, m, 
            rgblabel}, 
rgblabel\  = \ {"Red", \ "Green", \ "Blue"}; 
m\  = \ 
            Ceiling[Length[dat]/n]; 
d2\  = \ 
            First\  /@ \ Partition[dat, \ m]; 
Show[
            Graphics[
              Join[opt, \(Point[{#[\([a]\)], #[\([b]\)]}] &\) /@ \ d2]], \ 
            Frame -> True, AspectRatio -> Automatic, \ 
            FrameLabel -> {rgblabel[\([a]\)], \ rgblabel[\([b]\)]}, \ 
            DisplayFunction -> Identity, \ 
            PlotRange -> {{0, 255}, {0, 255}}]];\)\)

Plot representative sample data  from a larger data set in 3d

PlotRepresentativeSample3D[dat_,  n_,  
          opt___] := 
Module[{d2, 
            m}, 
m  =  
            Ceiling[Length[dat]/n]; 
d2  =  
            First  /@  Partition[dat,  m]; 
Show[
            Graphics3D[Join[opt, Point /@  d2]],  
            DisplayFunction -> Identity, 
            PlotRange -> {{0, 255}, {0, 255}, {0, 255}}]];

data2pixel[{r_, g_, b_, x_, y_}] := {RGBColor[r/255. , g/255. , 
          b/255. ],  Rectangle[{x, -y - 1}, {x + 1, -y}]}

Plot multiple results for mixture model

EMAllPlot[{μ_, Σ_, priors_},  
          gin_] := 
Module[{pr, g2, g4, 
            g5}, 
pr  =  {{0, 1}, {0, 
                1}}; 
g2  =  
            PlotMeans[{μ, Σ, priors},  pr,  Blue,  nS,  
              FrameTicks -> None]; 
g4  =  
            PlotContourPDF[{μ, Σ, priors},  pr, 50, nS,  
              FrameTicks -> None]; 
g5  =  
            PlotPDF[{μ, Σ, priors},  pr, 50, nS,  
              Axes -> None]; 
Show[
            GraphicsArray[{{g2, gin}, {g4, g5}}], yS,  
            imSize]
];

Classify image (2d)

ClassifyImage2DAugmented[im_, pdf1_, pdf2_,  
          which_: {Red,  Green},  
          scale_:  1] := 
Module[{class, colors, pos1, 
            pos2, lpdf, gr, 
            bl}, 
gr = (List @@ (pointStyle1  //  
                      First))*255; 
bl = (List @@ (pointStyle2 // First))*255; 
colors = {Red, Green, 
              Blue}; 
pos1 = Position[colors, 
                  which[[1]]] // Flatten  // 
              First; 
pos2 = Position[colors, 
                  which[[2]]] // Flatten  // 
              First; 
lpdf  =  (Log[pdf1]  // 
                  PowerExpand)  -  (Log[pdf2]  //  
                  PowerExpand); 
class  =  
            Map[If[# == replacementColor, replacementColor, 
                  If[(lpdf  /.  {x -> #[[pos1]]/scale  //  N, 
                            y -> #[[pos2]]/scale  //  N})  >  
                      0,  gr,  bl]] &,  
              im[[1, 1]], {2}]; 
ReplacePart[im, 
            class, {1, 1}]
];

Classify image (3d)

ClassifyImage3DAugmented[im_, lpdf1_, lpdf2_,  
          scale_:  1] := 
Module[{class, colors, lpdf, gr, 
            bl}, 
gr = (List @@ (pointStyle1  //  
                      First))*255; 
bl = (List @@ (pointStyle2 // First))*255; 
colors = {Red, Green, 
              Blue}; 
lpdf  =  (lpdf1  -  
                lpdf2); 
class  =  
            Map[If[# == replacementColor, replacementColor, 
                  If[(lpdf  /.  {x -> #[[1]]/scale  //  N, 
                            y -> #[[2]]/scale  //  N,  
                            z -> #[[3]]/scale  //  N})  >  
                      0,  gr,  bl]] &,  
              im[[1, 1]], {2}]; 
ReplacePart[im, 
            class, {1, 1}]
];

Count pixels in classified images

CountPixels[classim_, 
          color1_: (List  @@  (pointStyle1  //  First))*255,  
          color2_: (List  @@  (pointStyle2  //  First))*255, 
          backgroundColor_:  
            replacementColor] := 
Module[{cnts, n1, n2, 
            n3}, 
cnts = 
            Frequencies[
              Partition[Flatten[classim[[1, 1]]], 
                3]]; 
n1 = 
            cnts[[Position[cnts, color1]  //  Flatten  //  First, 
                1]]; 
n2 = 
            cnts[[Position[cnts, color2]  //  Flatten  //  First, 
                1]]; 
n3 = 
            cnts[[Position[cnts, backgroundColor]  //  Flatten  //  
                  First, 1]]; 
{100*n1/(n1 + n2),  
              100*n2/(n1 + n2)}  //  
            N
];

Normalized log-likelihood for each object (2d)

AggregateLogLikelihood2D[data1_,  data2_,  
          which_] := 
Module[{d1, 
            d2, μ1,  Σ1,  
            gauss1,  μ2,  Σ2,  gauss2,  a11, a12, a21, 
            a22}, 
d1 = {#[[which[[1]]]], #[[which[[2]]]]} &  /@  (data1/255)  //  
              N; 
d2 = {#[[which[[1]]]], #[[which[[2]]]]} &  /@  (data2/255)  //  
              N; 
μ1 = Mean[d1]; Σ1 = 
            CovarianceMatrix[d1]; 
          gauss1 = {{μ1}, {Σ1}, {1. }}; 
μ2 = Mean[d2]; Σ2 = CovarianceMatrix[d2]; 
          gauss2 = {{μ2}, {Σ2}, {1. }}; 
a11 = 
            LogLikelihoodMixtureDataSet[d1, gauss1]/
              Length[d1]; 
a12 = 
            LogLikelihoodMixtureDataSet[d1, gauss2]/
              Length[d1]; 
a21 = 
            LogLikelihoodMixtureDataSet[d2, gauss1]/
              Length[d2]; 
a22 = 
            LogLikelihoodMixtureDataSet[d2, gauss2]/
              Length[d2]; 
{{a11, a12}, {a21, 
              a22}}
];

Normalized log-likelihood for each object (2d) (mixture model)

AggregateLogLikelihood2DEM[data1_,  data2_,  model1_,  model2_, 
          which_] := 
Module[{d1, d2,  a11, a12, a21, 
            a22}, 
d1 = {#[[which[[1]]]], #[[which[[2]]]]} &  /@  (data1/255)  //  
              N; 
d2 = {#[[which[[1]]]], #[[which[[2]]]]} &  /@  (data2/255)  //  
              N; 
a11 = 
            LogLikelihoodMixtureDataSet[d1, model1]/
              Length[d1]; 
a12 = 
            LogLikelihoodMixtureDataSet[d1, model2]/
              Length[d1]; 
a21 = 
            LogLikelihoodMixtureDataSet[d2, model1]/
              Length[d2]; 
a22 = 
            LogLikelihoodMixtureDataSet[d2, model2]/
              Length[d2]; 
{{a11, a12}, {a21, 
              a22}}
];

Normalized log-likelihood for each object (3d)

AggregateLogLikelihood3D[data1_,  
          data2_] := 
Module[{d1, 
            d2, μ1,  Σ1,  
            gauss1,  μ2,  Σ2,  gauss2,  a11, a12, a21, 
            a22}, 
d1 = (data1/255)  //  
              N; 
d2 = (data2/255)  //  
              N; 
μ1 = Mean[d1]; Σ1 = 
            CovarianceMatrix[d1]; 
          gauss1 = {{μ1}, {Σ1}, {1. }}; 
μ2 = Mean[d2]; Σ2 = CovarianceMatrix[d2]; 
          gauss2 = {{μ2}, {Σ2}, {1. }}; 
a11 = 
            LogLikelihoodMixtureDataSet[d1, gauss1]/
              Length[d1]; 
a12 = 
            LogLikelihoodMixtureDataSet[d1, gauss2]/
              Length[d1]; 
a21 = 
            LogLikelihoodMixtureDataSet[d2, gauss1]/
              Length[d2]; 
a22 = 
            LogLikelihoodMixtureDataSet[d2, gauss2]/
              Length[d2]; 
{{a11, a12}, {a21, 
              a22}}
];

Normalized log-likelihood for each object (3d) (mixture model)

AggregateLogLikelihood3DEM[data1_,  data2_, model1_, 
          model2_] := 
Module[{d1, d2, a11, a12, a21, 
            a22}, 
d1 = (data1/255)  //  
              N; 
d2 = (data2/255)  //  
              N; 
a11 = 
            LogLikelihoodMixtureDataSet[d1, model1]/
              Length[d1]; 
a12 = 
            LogLikelihoodMixtureDataSet[d1, model2]/
              Length[d1]; 
a21 = 
            LogLikelihoodMixtureDataSet[d2, model1]/
              Length[d2]; 
a22 = 
            LogLikelihoodMixtureDataSet[d2, model2]/
              Length[d2]; 
{{a11, a12}, {a21, 
              a22}}
];

3d EM initialization function

Scalar covariance initialization 3d

\(\(EMInitializationScalar3D[x_, \ k_] := 
        Module[{n, \ rl, \ μ, \ σ, \ priors, \ sm, \ del2}, 
	
          n\  = \ Length[x]; 
	
          rl\  = \ Table[{Random[Integer, {1, n}]}, \ {k}]; 
	μ\  = \ 
            Partition[Flatten[\(Take[x, \ #] &\)\  /@ \ rl], \ 3]; 
	
          sm\  = \ 
            Table[\(dist[#, \ μ[\([i]\)]] &\)\  /@ \ μ, \ {i, \ 1, \ 
                k}]; 
	
          del2\  = \ 
            Table[{i, i}\ \ , \ {i, \ 1, 
                k}]; 
	σ\  = \ \(\((#*#)\) &\)\  /@ \ \((Min\  /@ \ 
                  Delete[sm, \ del2])\); 
	
          priors\  = \ Table[1/k, \ {k}]\  // \ N; 
	{μ, \ σ, \ 
            priors}
];\)\)

Full covariance initialization 3d

EMInitializationFull3D[x_, 
          k_] := 
Module[{μ, σ, priors, 
            dim}, 
{μ, σ, priors}  =  
            EMInitializationScalar3D[x,  k]; 
dim  =  
            Length[First[μ]]; 
{μ, (# IdentityMatrix[dim]) &  /@  σ, 
            priors}
];

Load and visualize data

Load segmented images

obj1 = Import[segmentedFile1, "PPM"];
obj2 = Import[segmentedFile2, "PPM"];
obj1T = 
        Import[segmentedFile1T, "PPM"];
obj2T = 
        Import[segmentedFile2T, "PPM"];
Show[GraphicsArray[{{obj1, obj2, obj1T, obj2T}}],  
        ImageSize -> 400];

Extract RGB data

data1 = ExtractRGB[obj1];
data2 = ExtractRGB[obj2];

Visualize RGB data

3d distribution in RGB cube

gobj1  =  
        PlotRepresentativeSample3D[data1,  2000, pointStyle1];
gobj2  =  
        PlotRepresentativeSample3D[data2,  2000, pointStyle2];
gdist1 = 
        Show[{gobj1, gobj2}, nS,  ImageSize -> 288];

RG plane

which = {1, 2};
gobj1  =  
        PlotRepresentativeSample2D[data1,  2000, which, 
          pointStyle1];
gobj2  =  
        PlotRepresentativeSample2D[data2,  2000, which, 
          pointStyle2];
gdist2 = 
        Show[{gobj1, gobj2}, nS, FrameTicks -> False,  
          FrameLabel -> None, ImageSize -> 288];

RB plane

which = {1, 3};
gobj1  =  
        PlotRepresentativeSample2D[data1,  2000, which, 
          pointStyle1];
gobj2  =  
        PlotRepresentativeSample2D[data2,  2000, which, 
          pointStyle2];
gdist3 = 
        Show[{gobj1, gobj2}, nS, FrameTicks -> False,  
          FrameLabel -> None, ImageSize -> 288];

GB plane

which = {2, 3};
gobj1  =  
        PlotRepresentativeSample2D[data1,  2000, which, 
          pointStyle1];
gobj2  =  
        PlotRepresentativeSample2D[data2,  2000, which, 
          pointStyle2];
gdist4 = 
        Show[{gobj1, gobj2}, nS, FrameTicks -> False,  
          FrameLabel -> None, ImageSize -> 288];

Show all

Show[GraphicsArray[{{gdist1, gdist2}, {gdist3, gdist4}}],  
        imSize];

2d Gaussian classification (RG plane)

Visualize distributions

which = {1, 2};
gobj1  =  
        PlotRepresentativeSample2D[data1,  2000, which, 
          pointStyle1];
gobj2  =  
        PlotRepresentativeSample2D[data2,  2000, which, 
          pointStyle2];
g1 = 
        Show[gobj1, FrameTicks -> False, FrameLabel -> None, 
          nS];
g2 = 
        Show[gobj2, FrameTicks -> False, FrameLabel -> None, 
          nS];
gbase = Show[g1, g2, nS];
Show[GraphicsArray[{g1, g2}],  imSize,  yS];

Gaussian models

Extract 2d data

d2d1 = ({#[[which[[1]]]], #[[which[[2]]]]} & /@  data1);
d2d2 = ({#[[which[[1]]]], #[[which[[2]]]]} & /@  data2);

Compute pdfs

pdf1  =  gaussian[d2d1, {x, y}];
pdf2  =  gaussian[d2d2, {x, y}];

Visualize pdfs

ShowTwoGaussianPDFs[pdf1, pdf2, pr, x, y, yS, imSize];

Classification in 2d

ShowTwoClassificationRegions[pdf1, pdf2, pr, x, y, classColors, 
        gbase, Axes -> False, Frame -> True,  
        FrameTicks -> False, yS];

Image classification

class1 = 
        ClassifyImage2DAugmented[obj1, pdf1, pdf2, {Red, Green}];
class2 = 
        ClassifyImage2DAugmented[obj2, pdf1, pdf2, {Red, Green}];
Show[GraphicsArray[{{obj1, class1, obj2, class2}}],  
        ImageSize -> 400];

class1 = 
        ClassifyImage2DAugmented[obj1T, pdf1, 
          pdf2, {Red, Green}];
class2 = 
        ClassifyImage2DAugmented[obj2T, pdf1, 
          pdf2, {Red, Green}];
Show[GraphicsArray[{{obj1T, class1, obj2T, class2}}],  
        ImageSize -> 400];

Error plot

cnt1 = CountPixels[class1];
cnt2 = CountPixels[class2];

bar  =  {cnt1,  cnt2}  //  
          Transpose;
BarChart[bar[[1]],  bar[[2]],  
        BarStyle -> {pointStyle1[[1]], 
            pointStyle2[[1]]},  
Frame -> True, 
        BarLabels -> {"Kellogs", "Tide"},  
        imSize];

2d Gaussian classification (RB plane)

Visualize distributions

which = {1, 3};
gobj1  =  
        PlotRepresentativeSample2D[data1,  2000, which, 
          pointStyle1];
gobj2  =  
        PlotRepresentativeSample2D[data2,  2000, which, 
          pointStyle2];
g1 = 
        Show[gobj1, FrameTicks -> False, FrameLabel -> None, 
          nS];
g2 = 
        Show[gobj2, FrameTicks -> False, FrameLabel -> None, 
          nS];
gbase = Show[g1, g2, nS];
Show[GraphicsArray[{g1, g2}],  imSize,  yS];

Gaussian models

Extract 2d data

d2d1 = ({#[[which[[1]]]], #[[which[[2]]]]} & /@  data1);
d2d2 = ({#[[which[[1]]]], #[[which[[2]]]]} & /@  data2);

Compute pdfs

pdf1  =  gaussian[d2d1, {x, y}];
pdf2  =  gaussian[d2d2, {x, y}];

Visualize pdfs

ShowTwoGaussianPDFs[pdf1, pdf2, pr, x, y, yS, imSize];

Classification in 2d

ShowTwoClassificationRegions[pdf1, pdf2, pr, x, y, classColors, 
        gbase, Axes -> False, Frame -> True,  
        FrameTicks -> False, yS];

Image classification

class1 = 
        ClassifyImage2DAugmented[obj1, pdf1, pdf2, {Red, Blue}];
class2 = 
        ClassifyImage2DAugmented[obj2, pdf1, pdf2, {Red, Blue}];
Show[GraphicsArray[{{obj1, class1, obj2, class2}}],  
        ImageSize -> 400];

class1 = 
        ClassifyImage2DAugmented[obj1T, pdf1, pdf2, {Red, Blue}];
class2 = 
        ClassifyImage2DAugmented[obj2T, pdf1, pdf2, {Red, Blue}];
Show[GraphicsArray[{{obj1T, class1, obj2T, class2}}],  
        ImageSize -> 400];

Error plot

cnt1 = CountPixels[class1];
cnt2 = CountPixels[class2];

bar  =  {cnt1,  cnt2}  //  
          Transpose;
BarChart[bar[[1]],  bar[[2]],  
        BarStyle -> {pointStyle1[[1]], 
            pointStyle2[[1]]},  
Frame -> True, 
        BarLabels -> {"Kellogs", "Tide"},  
        imSize];

2d Gaussian classification (GB plane)

Visualize distributions

which = {2, 3};
gobj1  =  
        PlotRepresentativeSample2D[data1,  2000, which, 
          pointStyle1];
gobj2  =  
        PlotRepresentativeSample2D[data2,  2000, which, 
          pointStyle2];
g1 = 
        Show[gobj1, FrameTicks -> False, FrameLabel -> None, 
          nS];
g2 = 
        Show[gobj2, FrameTicks -> False, FrameLabel -> None, 
          nS];
gbase = Show[g1, g2, nS];
Show[GraphicsArray[{g1, g2}],  imSize,  yS];

Gaussian models

Extract 2d data

d2d1 = ({#[[which[[1]]]], #[[which[[2]]]]} & /@  data1);
d2d2 = ({#[[which[[1]]]], #[[which[[2]]]]} & /@  data2);

Compute pdfs

pdf1  =  gaussian[d2d1, {x, y}];
pdf2  =  gaussian[d2d2, {x, y}];

Visualize pdfs

ShowTwoGaussianPDFs[pdf1, pdf2, pr, x, y, yS, imSize];

Classification in 2d

ShowTwoClassificationRegions[pdf1, pdf2, pr, x, y, classColors, 
        gbase, Axes -> False, Frame -> True,  
        FrameTicks -> False, yS];

Image classification

class1 = 
        ClassifyImage2DAugmented[obj1, pdf1, 
          pdf2, {Green, Blue}];
class2 = 
        ClassifyImage2DAugmented[obj2, pdf1, 
          pdf2, {Green, Blue}];
Show[GraphicsArray[{{obj1, class1, obj2, class2}}],  
        ImageSize -> 400];

class1 = 
        ClassifyImage2DAugmented[obj1T, pdf1, 
          pdf2, {Green, Blue}];
class2 = 
        ClassifyImage2DAugmented[obj2T, pdf1, 
          pdf2, {Green, Blue}];
Show[GraphicsArray[{{obj1T, class1, obj2T, class2}}],  
        ImageSize -> 400];

Error plot

cnt1 = CountPixels[class1];
cnt2 = CountPixels[class2];

bar  =  {cnt1,  cnt2}  //  
          Transpose;
BarChart[bar[[1]],  bar[[2]],  
        BarStyle -> {pointStyle1[[1]], 
            pointStyle2[[1]]},  
Frame -> True, 
        BarLabels -> {"Kellogs", "Tide"},  
        imSize];

2d mixture-of-Gaussians classification (GB plane)

Visualize distributions

which = {2, 3};
gobj1  =  
        PlotRepresentativeSample2D[data1,  2000, which, 
          pointStyle1];
gobj2  =  
        PlotRepresentativeSample2D[data2,  2000, which, 
          pointStyle2];
g1 = 
        Show[gobj1, FrameTicks -> False, FrameLabel -> None, 
          nS];
g2 = 
        Show[gobj2, FrameTicks -> False, FrameLabel -> None, 
          nS];
gbase = Show[g1, g2, nS];
Show[GraphicsArray[{g1, g2}],  imSize,  yS];

EM experiment #1: object 1 (Sprite can)

Extract 2d data

d2d1 = ({#[[which[[1]]]], #[[which[[2]]]]} & /@  (data1/255. ));

Choose number of Gaussians in mixture model

k = 4;

Assumes full covariance matrices

initFull  =  EMInitializationFull[d2d1, k];

Compute EM algorithm

Timing[em  =  EM[d2d1, initFull, 0.001];]

Save solution

bad = Position[em, Indeterminate];
If[Length[bad] > 0, 
        bad = bad // First // First]; 
em  =  
        If[NumberQ[bad] == True, Take[em, {1, bad - 1}], 
          em];
model1 = em // Last;

Length[em]

Final model

EMAllPlot[model1, g1];

EM experiment #2: object 2 (Coke Light can)

Extract 2d data

d2d2 = ({#[[which[[1]]]], #[[which[[2]]]]} & /@  (data2/255. ));

Choose number of Gaussians in mixture model

k = 4;

Assumes full covariance matrices

initFull  =  EMInitializationFull[d2d2, k];

Compute EM algorithm

Timing[em  =  EM[d2d2, initFull, 0.001];]

Save solution

bad = Position[em, Indeterminate];
If[Length[bad] > 0, 
        bad = bad // First // First]; 
em  =  
        If[NumberQ[bad] == True, Take[em, {1, bad - 1}], 
          em];
model2 = em // Last;

Final model

EMAllPlot[model2, g2];

Resulting classification of 2d space

pts  =  150;
reg1  = 
        MixtureLikelihoodMatrix[{0. , 1. , 0. , 1. }, pts, 
          model1];
reg2  = 
        MixtureLikelihoodMatrix[{0. , 1. , 0. , 1. }, pts, 
          model2];
class  = 
        Partition[If[#[[1]]  <  #[[2]],  1, 
                0] &  /@  ({Flatten[reg1],  Flatten[reg2]}  //  
                Transpose), pts + 1];

\(\(\(
\)\(classPlot1\  = 
      ListDensityPlot[class, \ 
        ColorFunction -> \((If[# == 0, pointStyle1[\([1]\)], \ 
                White] &)\), \ Mesh -> False, \ 
        MeshRange -> {{0, 1}, {0, 1}}, FrameTicks -> None, nS]; 
    classPlot2\  = 
      ListDensityPlot[class, \ 
        ColorFunction -> \((If[# == 1, pointStyle2[\([1]\)], White] &)\), \ 
        Mesh -> False, \ MeshRange -> {{0, 1}, {0, 1}}, 
        FrameTicks -> None, nS];

    \(Show[GraphicsArray[{{g1, g2}, {classPlot1, classPlot2}}], imSize, 
        yS];\)\)\)\)

Image classification

pdf1 = 
        MixtureLikelihoodNative[{x, y}, model1];
pdf2 = MixtureLikelihoodNative[{x, y}, model2];

class1 = 
        ClassifyImage2DAugmented[obj1, pdf1, pdf2, {Green, Blue}, 
          255. ];
class2 = 
        ClassifyImage2DAugmented[obj2, pdf1, pdf2, {Green, Blue}, 
          255. ];
Show[GraphicsArray[{{obj1, class1, obj2, class2}}],  
        ImageSize -> 400];

class1 = 
        ClassifyImage2DAugmented[obj1T, pdf1, pdf2, {Green, Blue}, 
          255. ];
class2 = 
        ClassifyImage2DAugmented[obj2T, pdf1, pdf2, {Green, Blue}, 
          255. ];
Show[GraphicsArray[{{obj1T, class1, obj2T, class2}}],  
        ImageSize -> 400];

Error plot

cnt1 = CountPixels[class1];
cnt2 = CountPixels[class2];

bar  =  {cnt1,  cnt2}  //  
          Transpose;
BarChart[bar[[1]],  bar[[2]],  
        BarStyle -> {pointStyle1[[1]], 
            pointStyle2[[1]]},  
Frame -> True, 
        BarLabels -> {"Kellogs", "Tide"},  
        imSize];