Basic image processing: Part I

Note: This notebook assumes that all definitions in ipcv_definitions.nb have been evaluated first. It also assumes that the directory in the Set image directory points to an image directory with the following images: cat256x256.ppm, flowers256x256.ppm and bakery256x256.ppm.

Visualizing the normalized RGB color space

(r,  g)  =  ((R/(R + G + B)), 
        (G/(R + G + B)))

Show[
        Graphics[
          Table[{RGBColor[r/255. , g/255. , b/255. ], PointSize[ .04], 
              Point[{r/(r + g + b), g/(r + g + b)}]}, {r, 5, 255, 
              10}, {g, 5, 255, 10}, {b, 5, 255, 10}]],  
        AspectRatio -> Automatic,  Frame -> True];

(r,  b)  =  ((R/(R + G + B)), 
        (B/(R + G + B)))

Show[
        Graphics[
          Table[{RGBColor[r/255. , g/255. , b/255. ], PointSize[ .04], 
              Point[{r/(r + g + b), b/(r + g + b)}]}, {r, 5, 255, 
              10}, {g, 5, 255, 10}, {b, 5, 255, 10}]],  
        AspectRatio -> Automatic,  Frame -> True];

(g,  b)  =  ((G/(R + G + B)), 
        (B/(R + G + B)))

Show[
        Graphics[
          Table[{RGBColor[r/255. , g/255. , b/255. ], PointSize[ .04], 
              Point[{g/(r + g + b), b/(r + g + b)}]}, {r, 5, 255, 
              10}, {g, 5, 255, 10}, {b, 5, 255, 10}]],  
        AspectRatio -> Automatic,  Frame -> True];

Set image directory

If[StringQ[$ImagePath], $path = $ImagePath,  $path = "."]; 
    SetDirectory[$path];

Individual pixel modifications

Gray-scale examples

Load gray-scale image

img = ReadImage["cat256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Define some \(TraditionalForm\`f : ℛ\^1 -> ℛ\^1\)such that \(TraditionalForm\`\(i\^*\) = f(i)\)

f1[v_, exp_] := 255 (v/255)^exp;
f1a[v_] := f1[v,  .25];
f1b[v_] := f1[v, 4];
f2[v_] := 255 - v;
f3[v_, a_] := 
        Abs[255 Sin[a π (v/255)]]  //  
          N;
f3a[v_] := f3[v, 1. ];
f3b[v_] := f3[v, 2. ];

Plot and apply scalar function (1)

ftst = f1a;

Plot[ftst[v], {v, 0, 255}, 
        PlotStyle -> {Red, Thickness[ .01]},  AspectRatio -> 1, 
        PlotRange -> All];

tmp = PixelModification[img,  ftst];
ShowImage[tmp, scale];

Plot and apply scalar function (2)

ftst = f1b;

Plot[ftst[v], {v, 0, 255}, 
        PlotStyle -> {Red, Thickness[ .01]},  AspectRatio -> 1, 
        PlotRange -> All];

tmp = PixelModification[img,  ftst];
ShowImage[tmp, scale];

Plot and apply scalar function (3)

ftst = f2;

Plot[ftst[v], {v, 0, 255}, 
        PlotStyle -> {Red, Thickness[ .01]},  AspectRatio -> 1, 
        PlotRange -> All];

tmp = PixelModification[img,  ftst];
ShowImage[tmp, scale];

Plot and apply scalar function (4)

ftst = f3a;

Plot[ftst[v], {v, 0, 255}, 
        PlotStyle -> {Red, Thickness[ .01]},  AspectRatio -> 1, 
        PlotRange -> All];

tmp = PixelModification[img,  ftst];
ShowImage[tmp, scale];

Plot and apply scalar function (5)

ftst = f3b;

Plot[ftst[v], {v, 0, 255}, 
        PlotStyle -> {Red, Thickness[ .01]},  AspectRatio -> 1, 
        PlotRange -> All];

tmp = PixelModification[img,  ftst];
ShowImage[tmp, scale];

Color examples

Load color image

img = 
        ReadImage["flowers256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Define some \(TraditionalForm\`f : ℛ\^3 -> ℛ\^3\)such that \(TraditionalForm\`\((\(r\^*\), \(g\^*\), \(b\^*\))\) = f(r, g, b)\)

Single-channel functions

f1[v_, exp_] := 255 (v/255)^exp;
f1a[v_] := f1[v,  .25];
f1b[v_] := f1[v, 4];
f2[v_] := 255 - v;
f3[v_, a_] := 
        Abs[255 Sin[a π (v/255)]]  //  
          N;
f3a[v_] := f3[v, 1. ];
f3b[v_] := f3[v, 2. ];

Color functions

g1a[v_] := f1a /@  v;
g1b[v_] := f1b /@ v;
g2[v_] := f2  /@  v;
g3[v_] := f3a  /@  v;

Apply  function (1)

ftst = g1a;

tmp = PixelModification[img,  ftst];
ShowImage[tmp, scale];

Apply  function (2)

ftst = g1b;

tmp = PixelModification[img,  ftst];
ShowImage[tmp, scale];

Apply  function (3)

ftst = g2;

tmp = PixelModification[img,  ftst];
ShowImage[tmp, scale];

Apply  function (4)

ftst = g3;

tmp = PixelModification[img,  ftst];
ShowImage[tmp, scale];

Image histogram modifications (gray-scale images)

Example #1

Load gray-scale image

img = ReadImage["cat256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Plot image histogram

ShowHistogram[img];

Histogram equalization of image

tmp = HistogramEqualization[img];
ShowHistogram[tmp];
ShowImage[tmp, scale];

Histogram squeeze image

tst = HistogramStretch[img, 0, 50];
ShowHistogram[tst];
ShowImage[tst, scale];

Histogram stretch of histogram-squeezed image

tmp = HistogramStretch[tst, 0, 255];
ShowHistogram[tmp];
ShowImage[tmp, scale];

Histogram equalization of histogram-squeezed image

tmp = HistogramEqualization[tst];
ShowHistogram[tmp];
ShowImage[tmp, scale];

Example #2

Load gray-scale image

img = 
        ReadImage["bakery256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Plot image histogram

ShowHistogram[img];

Histogram equalization of image

tmp = HistogramEqualization[img];
ShowHistogram[tmp];
ShowImage[tmp, scale];

Histogram squeeze image

tst = HistogramStretch[img, 0, 50];
ShowHistogram[tst];
ShowImage[tst, scale];

Histogram stretch of histogram-squeezed image

tmp = HistogramStretch[tst, 0, 255];
ShowHistogram[tmp];
ShowImage[tmp, scale];

Histogram equalization of histogram-squeezed image

tmp = HistogramEqualization[tst];
ShowHistogram[tmp];
ShowImage[tmp, scale];

2D convolution: image smoothing

Mean-filtering: gray-scale image example

Load gray-scale image

img = ReadImage["cat256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Mean filtering

5×5mean filter

n = 5;
tmp = Mask[img, MeanFilter[n]];
ShowImage[tmp, scale];

11×11mean filter

n = 11;
tmp = Mask[img, MeanFilter[n]];
ShowImage[tmp, scale];

21×21mean filter

n = 21;
tmp = Mask[img, MeanFilter[n]];
ShowImage[tmp, scale];

Mean-filtering: color image example

Load color image

img = 
        ReadImage["flowers256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Mean filtering

5×5mean filter

n = 5;
tmp = Mask[img, MeanFilter[n]];
ShowImage[tmp, scale];

11×11mean filter

n = 11;
tmp = Mask[img, MeanFilter[n]];
ShowImage[tmp, scale];

21×21mean filter

n = 21;
tmp = Mask[img, MeanFilter[n]];
ShowImage[tmp, scale];

Gaussian filtering: gray-scale image example

Load gray-scale image

img = ReadImage["cat256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Gaussian filtering

5×5Gaussian filter

n = 5;
gf = GaussianFilter[n, (n - 1)/4. ];
ListDensityPlot[gf, Mesh -> False, Frame -> False, 
        ImageSize -> 5*n,  
        PlotRange -> All];
tmp = Mask[img, gf];
ShowImage[tmp, scale];

11×11Gaussian filter

n = 11;
gf = GaussianFilter[n, (n - 1)/4. ];
ListDensityPlot[gf, Mesh -> False, Frame -> False, 
        ImageSize -> 5*n,  
        PlotRange -> All];
tmp = Mask[img, gf];
ShowImage[tmp, scale];

21×21Gaussian filter

n = 21;
gf = GaussianFilter[n, (n - 1)/4. ];
ListDensityPlot[gf, Mesh -> False, Frame -> False, 
        ImageSize -> 5*n,  
        PlotRange -> All];
tmp = Mask[img, gf];
ShowImage[tmp, scale];

Gaussian filtering: color image example

Load color image

img = 
        ReadImage["flowers256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Gaussian filtering

5×5Gaussian filter

n = 5;
gf = GaussianFilter[n, (n - 1)/4. ];
ListDensityPlot[gf, Mesh -> False, Frame -> False, 
        ImageSize -> 5*n,  
        PlotRange -> All];
tmp = Mask[img, gf];
ShowImage[tmp, scale];

11×11Gaussian filter

n = 11;
gf = GaussianFilter[n, (n - 1)/4. ];
ListDensityPlot[gf, Mesh -> False, Frame -> False, 
        ImageSize -> 5*n,  
        PlotRange -> All];
tmp = Mask[img, gf];
ShowImage[tmp, scale];

21×21Gaussian filter

n = 21;
gf = GaussianFilter[n, (n - 1)/4. ];
ListDensityPlot[gf, Mesh -> False, Frame -> False, 
        ImageSize -> 5*n,  
        PlotRange -> All];
tmp = Mask[img, gf];
ShowImage[tmp, scale];

2D convolution: gradient images

Sobel gradient image: gray-scale image example

Load gray-scale image

img = ReadImage["cat256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Sobel gradient image

Sobel row mask

SobelRow[]  //  TraditionalForm

Sobel row gradient image (magnitude)

sr = Mask[img, SobelRow[]]  //  N;
ShowImage[sr, scale,  .8  Max[sr]];

Sobel column mask

SobelColumn[]  //  TraditionalForm

Sobel column gradient image (magnitude)

sc = Mask[img, SobelColumn[]]  //  N;
ShowImage[sc, scale,  .8  Max[sc]];

Sobel gradient image

tmp = Sqrt[sc^2 +  sr^2];
ShowImage[tmp, scale,  .8  Max[tmp]];

Sobel gradient image: color image example

Load color image

img = 
        ReadImage["flowers256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Sobel gradient image

Sobel row mask

SobelRow[]  //  TraditionalForm

Sobel row gradient image (magnitude)

sr = Mask[img, SobelRow[]]  //  N;
ShowImage[sr, scale,  .8  Max[sr]];

Sobel column mask

SobelColumn[]  //  TraditionalForm

Sobel column gradient image (magnitude)

sc = Mask[img, SobelColumn[]]  //  N;
ShowImage[sc, scale,  .8  Max[sc]];

Sobel gradient image

tmp = Sqrt[sc^2 +  sr^2];
ShowImage[tmp, scale,  .8  Max[tmp]];

Prewitt gradient image: gray-scale image example

Load gray-scale image

img = ReadImage["cat256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Prewitt gradient image

Prewitt row mask

PrewittRow[]  //  TraditionalForm

Prewitt row gradient image (magnitude)

pr = Mask[img, PrewittRow[]]  //  N;
ShowImage[pr, scale,  .8  Max[pr]];

Prewitt column mask

PrewittColumn[]  //  TraditionalForm

Prewitt column gradient image (magnitude)

pc = 
        Mask[img, PrewittColumn[]]  //  N;
ShowImage[pc, scale,  .8  Max[pc]];

Prewitt gradient image

tmp = Sqrt[pc^2 +  pr^2];
ShowImage[tmp, scale,  .8  Max[tmp]];

Prewitt gradient image: color image example

Load color image

img = 
        ReadImage["flowers256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Prewitt gradient image

Prewitt row mask

PrewittRow[]  //  TraditionalForm

Prewitt row gradient image (magnitude)

pr = Mask[img, PrewittRow[]]  //  N;
ShowImage[pr, scale,  .8  Max[pr]];

Prewitt column mask

PrewittColumn[]  //  TraditionalForm

Prewitt column gradient image (magnitude)

pc = 
        Mask[img, PrewittColumn[]]  //  N;
ShowImage[pc, scale,  .8  Max[pc]];

Prewitt gradient image

tmp = Sqrt[pc^2 +  pr^2];
ShowImage[tmp, scale,  .8  Max[tmp]];