Image Compression

Group Members: James Aviles

Assignment following 'Project A' on Timothy Sauer's Website

Original image

Contents

Code used

Discrete Cosine Transform

Dequantization function

Quantization function

Image Compression function

Code to compress with JPEG Quantization matrix

Code to compress with Linear Quantization matrix

Code to compress color image with Linear Quantization

Code to compress color image in luminace / color difference coordinates using JPEG quantization

Computer problem 3 Part A

extract 8x8 block from image

X = imread('tago.png');         % assign image to variable
X = double(X);                  % make the matrix a double
r = X(:,:,1);                   % Seperate the RGB values
g = X(:,:,2);
b = X(:,:,3);
X = 0.216*r + 0.7152*g + 0.0722*b;   %converts to grey
xb = X(81:88,81:88);                 % take a block from the image
imagesc(uint8(xb)); colormap(gray)   % display that block
                                     % note: uint8() converts back from
                                     % double to integer

Part B

apply 2D-DCT

Discrete Cosine Transform

C = dct(xb);                         % Define matrix C to the size xb
xc = C*xb*C';                        % Perform 2D-DCT
imagesc(uint8(xc)); colormap(gray)   % display new block

Part C + D + E

Part C: Quantisize using Linear Quantization with p = 1,2,4 print out each YQ

Part D: reconstruct block with inverse 2D-DCT

Part E: reconstruct image and compare

Dequantization function

Quantization function

Q = [8 16 24 32 40 48 56 64     %Linear Quantiziation matrix
    16 24 32 40 48 56 64 72
    24 32 40 48 56 64 72 80
    32 40 48 56 64 72 80 88
    40 48 56 64 72 80 88 96
    48 56 64 72 80 88 96 104
    56 64 72 80 88 96 104 112
    64 72 80 88 96 104 112 120];

Q = 1 * Q;                            % multiply by p value

xc = quant(xb,C,Q)                    %Quantisize

xc = dequant(xc,C,Q)                  %Dequantisize

imagesc(uint8(xc)); colormap(gray)    %Display block
xc =

  -105     2     0     0     0     0     0     0
     1     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     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     0     0     0     0     0


xc =

  Columns 1 through 7

   31.3222   30.4776   28.9169   26.8777   24.6705   22.6313   21.0706
   30.8999   30.0553   28.4945   26.4553   24.2482   22.2090   20.6482
   30.1195   29.2749   27.7142   25.6750   23.4678   21.4286   19.8679
   29.1000   28.2553   26.6946   24.6554   22.4482   20.4090   18.8483
   27.9964   27.1517   25.5910   23.5518   21.3446   19.3054   17.7447
   26.9768   26.1321   24.5714   22.5322   20.3250   18.2858   16.7251
   26.1964   25.3518   23.7910   21.7518   19.5447   17.5055   15.9447
   25.7741   24.9294   23.3687   21.3295   19.1223   17.0831   15.5224

  Column 8

   20.2259
   19.8036
   19.0232
   18.0036
   16.9000
   15.8805
   15.1001
   14.6778

defining C, quantisizing, and dequantisizing are condensed into the image compression function imagecomp(X,Q)

Image Compression function

imglinear('file',P) is a function that iterates through all the steps using the linear quantization matrix for Q and allows to do so with a chosen file and 'P' value. Looping through the whole file compressing block by block

Code to compress with Linear Quantization matrix

Y1 = imglinear('tago.png',1);
yb1 = Y1(81:88,81:88);
Q =

     8    16    24    32    40    48    56    64
    16    24    32    40    48    56    64    72
    24    32    40    48    56    64    72    80
    32    40    48    56    64    72    80    88
    40    48    56    64    72    80    88    96
    48    56    64    72    80    88    96   104
    56    64    72    80    88    96   104   112
    64    72    80    88    96   104   112   120

imagesc(uint8(yb1)); colormap(gray)
Y2 = imglinear('tago.png',2);
yb2 = Y2(81:88,81:88);
Q =

    16    32    48    64    80    96   112   128
    32    48    64    80    96   112   128   144
    48    64    80    96   112   128   144   160
    64    80    96   112   128   144   160   176
    80    96   112   128   144   160   176   192
    96   112   128   144   160   176   192   208
   112   128   144   160   176   192   208   224
   128   144   160   176   192   208   224   240

imagesc(uint8(yb2)); colormap(gray)
Y4 = imglinear('tago.png',4);
yb4 = Y4(81:88,81:88);
Q =

    32    64    96   128   160   192   224   256
    64    96   128   160   192   224   256   288
    96   128   160   192   224   256   288   320
   128   160   192   224   256   288   320   352
   160   192   224   256   288   320   352   384
   192   224   256   288   320   352   384   416
   224   256   288   320   352   384   416   448
   256   288   320   352   384   416   448   480

imagesc(uint8(yb4)); colormap(gray)
Y1 = imglinear('tago.png',8);
yb1 = Y1(81:88,81:88);
Q =

    64   128   192   256   320   384   448   512
   128   192   256   320   384   448   512   576
   192   256   320   384   448   512   576   640
   256   320   384   448   512   576   640   704
   320   384   448   512   576   640   704   768
   384   448   512   576   640   704   768   832
   448   512   576   640   704   768   832   896
   512   576   640   704   768   832   896   960

imagesc(uint8(yb1)); colormap(gray)
Y2 = imglinear('tago.png',16);
yb2 = Y2(81:88,81:88);
Q =

  Columns 1 through 6

         128         256         384         512         640         768
         256         384         512         640         768         896
         384         512         640         768         896        1024
         512         640         768         896        1024        1152
         640         768         896        1024        1152        1280
         768         896        1024        1152        1280        1408
         896        1024        1152        1280        1408        1536
        1024        1152        1280        1408        1536        1664

  Columns 7 through 8

         896        1024
        1024        1152
        1152        1280
        1280        1408
        1408        1536
        1536        1664
        1664        1792
        1792        1920

imagesc(uint8(yb2)); colormap(gray)
Y2 = imglinear('tago.png',32);
yb2 = Y2(81:88,81:88);
Q =

  Columns 1 through 6

         256         512         768        1024        1280        1536
         512         768        1024        1280        1536        1792
         768        1024        1280        1536        1792        2048
        1024        1280        1536        1792        2048        2304
        1280        1536        1792        2048        2304        2560
        1536        1792        2048        2304        2560        2816
        1792        2048        2304        2560        2816        3072
        2048        2304        2560        2816        3072        3328

  Columns 7 through 8

        1792        2048
        2048        2304
        2304        2560
        2560        2816
        2816        3072
        3072        3328
        3328        3584
        3584        3840

imagesc(uint8(yb2)); colormap(gray)

Computer Problem 4

repeat with JPEG Quantization for p = 1

Code to compress with JPEG Quantization matrix

YJ = imgjpeg('tago.png',1);
ybJ = YJ(81:88,81:88);
Q =

    16    11    10    16    24    40    51    61
    12    12    14    19    26    58    60    55
    14    13    16    24    40    57    69    56
    14    17    22    29    51    87    80    62
    18    22    37    56    68   109   103    77
    24    35    55    64    81   104   113    92
    49    64    78    87   103   121   120   101
    72    92    95    98   112   100   103    99

imagesc(uint8(ybJ)); colormap(gray)
YJ = imgjpeg('tago.png',2);
ybJ = YJ(81:88,81:88);
Q =

    32    22    20    32    48    80   102   122
    24    24    28    38    52   116   120   110
    28    26    32    48    80   114   138   112
    28    34    44    58   102   174   160   124
    36    44    74   112   136   218   206   154
    48    70   110   128   162   208   226   184
    98   128   156   174   206   242   240   202
   144   184   190   196   224   200   206   198

imagesc(uint8(ybJ)); colormap(gray)
YJ = imgjpeg('tago.png',4);
ybJ = YJ(81:88,81:88);
Q =

    64    44    40    64    96   160   204   244
    48    48    56    76   104   232   240   220
    56    52    64    96   160   228   276   224
    56    68    88   116   204   348   320   248
    72    88   148   224   272   436   412   308
    96   140   220   256   324   416   452   368
   196   256   312   348   412   484   480   404
   288   368   380   392   448   400   412   396

imagesc(uint8(ybJ)); colormap(gray)
YJ = imgjpeg('tago.png',8);
ybJ = YJ(81:88,81:88);
Q =

   128    88    80   128   192   320   408   488
    96    96   112   152   208   464   480   440
   112   104   128   192   320   456   552   448
   112   136   176   232   408   696   640   496
   144   176   296   448   544   872   824   616
   192   280   440   512   648   832   904   736
   392   512   624   696   824   968   960   808
   576   736   760   784   896   800   824   792

imagesc(uint8(ybJ)); colormap(gray)
YJ = imgjpeg('tago.png',16);
ybJ = YJ(81:88,81:88);
Q =

  Columns 1 through 6

         256         176         160         256         384         640
         192         192         224         304         416         928
         224         208         256         384         640         912
         224         272         352         464         816        1392
         288         352         592         896        1088        1744
         384         560         880        1024        1296        1664
         784        1024        1248        1392        1648        1936
        1152        1472        1520        1568        1792        1600

  Columns 7 through 8

         816         976
         960         880
        1104         896
        1280         992
        1648        1232
        1808        1472
        1920        1616
        1648        1584

imagesc(uint8(ybJ)); colormap(gray)
YJ = imgjpeg('tago.png',32);
ybJ = YJ(81:88,81:88);
Q =

  Columns 1 through 6

         512         352         320         512         768        1280
         384         384         448         608         832        1856
         448         416         512         768        1280        1824
         448         544         704         928        1632        2784
         576         704        1184        1792        2176        3488
         768        1120        1760        2048        2592        3328
        1568        2048        2496        2784        3296        3872
        2304        2944        3040        3136        3584        3200

  Columns 7 through 8

        1632        1952
        1920        1760
        2208        1792
        2560        1984
        3296        2464
        3616        2944
        3840        3232
        3296        3168

imagesc(uint8(ybJ)); colormap(gray)

Computer Problem 5

Carry out computer problem 3 but with a color image by using linear quantization on R,G,B colors seperatley

Code to compress color image with Linear Quantization

Z = clrlinear('tago.png',1);
yc1 = Z(81:88,81:88);
Q =

     8    16    24    32    40    48    56    64
    16    24    32    40    48    56    64    72
    24    32    40    48    56    64    72    80
    32    40    48    56    64    72    80    88
    40    48    56    64    72    80    88    96
    48    56    64    72    80    88    96   104
    56    64    72    80    88    96   104   112
    64    72    80    88    96   104   112   120

imagesc(uint8(yc1));
Z = clrlinear('tago.png',2);
yc1 = Z(81:88,81:88);
Q =

    16    32    48    64    80    96   112   128
    32    48    64    80    96   112   128   144
    48    64    80    96   112   128   144   160
    64    80    96   112   128   144   160   176
    80    96   112   128   144   160   176   192
    96   112   128   144   160   176   192   208
   112   128   144   160   176   192   208   224
   128   144   160   176   192   208   224   240

imagesc(uint8(yc1));
Z = clrlinear('tago.png',4);
yc1 = Z(81:88,81:88);
Q =

    32    64    96   128   160   192   224   256
    64    96   128   160   192   224   256   288
    96   128   160   192   224   256   288   320
   128   160   192   224   256   288   320   352
   160   192   224   256   288   320   352   384
   192   224   256   288   320   352   384   416
   224   256   288   320   352   384   416   448
   256   288   320   352   384   416   448   480

imagesc(uint8(yc1));
Z = clrlinear('tago.png',8);
yc1 = Z(81:88,81:88);
Q =

    64   128   192   256   320   384   448   512
   128   192   256   320   384   448   512   576
   192   256   320   384   448   512   576   640
   256   320   384   448   512   576   640   704
   320   384   448   512   576   640   704   768
   384   448   512   576   640   704   768   832
   448   512   576   640   704   768   832   896
   512   576   640   704   768   832   896   960

imagesc(uint8(yc1));
Z = clrlinear('tago.png',16);
yc1 = Z(81:88,81:88);
Q =

  Columns 1 through 6

         128         256         384         512         640         768
         256         384         512         640         768         896
         384         512         640         768         896        1024
         512         640         768         896        1024        1152
         640         768         896        1024        1152        1280
         768         896        1024        1152        1280        1408
         896        1024        1152        1280        1408        1536
        1024        1152        1280        1408        1536        1664

  Columns 7 through 8

         896        1024
        1024        1152
        1152        1280
        1280        1408
        1408        1536
        1536        1664
        1664        1792
        1792        1920

imagesc(uint8(yc1));
Z = clrlinear('tago.png',32);
yc1 = Z(81:88,81:88);
Q =

  Columns 1 through 6

         256         512         768        1024        1280        1536
         512         768        1024        1280        1536        1792
         768        1024        1280        1536        1792        2048
        1024        1280        1536        1792        2048        2304
        1280        1536        1792        2048        2304        2560
        1536        1792        2048        2304        2560        2816
        1792        2048        2304        2560        2816        3072
        2048        2304        2560        2816        3072        3328

  Columns 7 through 8

        1792        2048
        2048        2304
        2304        2560
        2560        2816
        2816        3072
        3072        3328
        3328        3584
        3584        3840

imagesc(uint8(yc1));

Computer Problem 6

Carry out computer problem 3 but transform RGB to luminance / color difference coordinates and using JPEG quantization

Code to compress color image in luminace / color difference coordinates using JPEG quantization

U1 = yuvjpeg('tago.png',1);
yu1 = U1(81:88,81:88);
Q =

    16    11    10    16    24    40    51    61
    12    12    14    19    26    58    60    55
    14    13    16    24    40    57    69    56
    14    17    22    29    51    87    80    62
    18    22    37    56    68   109   103    77
    24    35    55    64    81   104   113    92
    49    64    78    87   103   121   120   101
    72    92    95    98   112   100   103    99


Q2 =

    17    18    24    47    99    99    99    99
    18    21    26    66    99    99    99    99
    24    26    56    99    99    99    99    99
    47    66    99    99    99    99    99    99
    99    99    99    99    99    99    99    99
    99    99    99    99    99    99    99    99
    99    99    99    99    99    99    99    99
    99    99    99    99    99    99    99    99

imagesc(uint8(yu1));
U1 = yuvjpeg('tago.png',2);
yu1 = U1(81:88,81:88);
Q =

    32    22    20    32    48    80   102   122
    24    24    28    38    52   116   120   110
    28    26    32    48    80   114   138   112
    28    34    44    58   102   174   160   124
    36    44    74   112   136   218   206   154
    48    70   110   128   162   208   226   184
    98   128   156   174   206   242   240   202
   144   184   190   196   224   200   206   198


Q2 =

    34    36    48    94   198   198   198   198
    36    42    52   132   198   198   198   198
    48    52   112   198   198   198   198   198
    94   132   198   198   198   198   198   198
   198   198   198   198   198   198   198   198
   198   198   198   198   198   198   198   198
   198   198   198   198   198   198   198   198
   198   198   198   198   198   198   198   198

imagesc(uint8(yu1));
U1 = yuvjpeg('tago.png',4);
yu1 = U1(81:88,81:88);
Q =

    64    44    40    64    96   160   204   244
    48    48    56    76   104   232   240   220
    56    52    64    96   160   228   276   224
    56    68    88   116   204   348   320   248
    72    88   148   224   272   436   412   308
    96   140   220   256   324   416   452   368
   196   256   312   348   412   484   480   404
   288   368   380   392   448   400   412   396


Q2 =

    68    72    96   188   396   396   396   396
    72    84   104   264   396   396   396   396
    96   104   224   396   396   396   396   396
   188   264   396   396   396   396   396   396
   396   396   396   396   396   396   396   396
   396   396   396   396   396   396   396   396
   396   396   396   396   396   396   396   396
   396   396   396   396   396   396   396   396

imagesc(uint8(yu1));
U1 = yuvjpeg('tago.png',8);
yu1 = U1(81:88,81:88);
Q =

   128    88    80   128   192   320   408   488
    96    96   112   152   208   464   480   440
   112   104   128   192   320   456   552   448
   112   136   176   232   408   696   640   496
   144   176   296   448   544   872   824   616
   192   280   440   512   648   832   904   736
   392   512   624   696   824   968   960   808
   576   736   760   784   896   800   824   792


Q2 =

   136   144   192   376   792   792   792   792
   144   168   208   528   792   792   792   792
   192   208   448   792   792   792   792   792
   376   528   792   792   792   792   792   792
   792   792   792   792   792   792   792   792
   792   792   792   792   792   792   792   792
   792   792   792   792   792   792   792   792
   792   792   792   792   792   792   792   792

imagesc(uint8(yu1));
U1 = yuvjpeg('tago.png',16);
yu1 = U1(81:88,81:88);
Q =

  Columns 1 through 6

         256         176         160         256         384         640
         192         192         224         304         416         928
         224         208         256         384         640         912
         224         272         352         464         816        1392
         288         352         592         896        1088        1744
         384         560         880        1024        1296        1664
         784        1024        1248        1392        1648        1936
        1152        1472        1520        1568        1792        1600

  Columns 7 through 8

         816         976
         960         880
        1104         896
        1280         992
        1648        1232
        1808        1472
        1920        1616
        1648        1584


Q2 =

  Columns 1 through 6

         272         288         384         752        1584        1584
         288         336         416        1056        1584        1584
         384         416         896        1584        1584        1584
         752        1056        1584        1584        1584        1584
        1584        1584        1584        1584        1584        1584
        1584        1584        1584        1584        1584        1584
        1584        1584        1584        1584        1584        1584
        1584        1584        1584        1584        1584        1584

  Columns 7 through 8

        1584        1584
        1584        1584
        1584        1584
        1584        1584
        1584        1584
        1584        1584
        1584        1584
        1584        1584

imagesc(uint8(yu1));
U1 = yuvjpeg('tago.png',32);
yu1 = U1(81:88,81:88);
Q =

  Columns 1 through 6

         512         352         320         512         768        1280
         384         384         448         608         832        1856
         448         416         512         768        1280        1824
         448         544         704         928        1632        2784
         576         704        1184        1792        2176        3488
         768        1120        1760        2048        2592        3328
        1568        2048        2496        2784        3296        3872
        2304        2944        3040        3136        3584        3200

  Columns 7 through 8

        1632        1952
        1920        1760
        2208        1792
        2560        1984
        3296        2464
        3616        2944
        3840        3232
        3296        3168


Q2 =

  Columns 1 through 6

         544         576         768        1504        3168        3168
         576         672         832        2112        3168        3168
         768         832        1792        3168        3168        3168
        1504        2112        3168        3168        3168        3168
        3168        3168        3168        3168        3168        3168
        3168        3168        3168        3168        3168        3168
        3168        3168        3168        3168        3168        3168
        3168        3168        3168        3168        3168        3168

  Columns 7 through 8

        3168        3168
        3168        3168
        3168        3168
        3168        3168
        3168        3168
        3168        3168
        3168        3168
        3168        3168

imagesc(uint8(yu1));