Quantifying dct coefficients in matlab

hi i need to do quantization by dct coefficients for an image, for a block size of 8 * 8 pixels in a matrix. you can help me with the syntax, thanks.

+2


a source to share


3 answers


MATLAB has a built-in function for DCT.

You need a signal processing toolbar. Enter 'ver' (without quotes) in the MATLAB command to see if you have one.



The code:

image = image; % define your image

[m,n] = size(image); % get size of your image

imvector = reshape(image, m*n, 1); % reshape your image to a vector to compute DCT

imdct = dct(imvector); % compute DCT 

imagedct = reshape(imdct,m,n); \ reshape result back to original form of your image

      

+1


a source


There is also an example in the help file which is very nice:



I = imread('cameraman.tif');
I = im2double(I);
T = dctmtx(8);
dct = @(block_struct) T * block_struct.data * T';
B = blockproc(I,[8 8],dct);
mask = [1   1   1   1   0   0   0   0
        1   1   1   0   0   0   0   0
        1   1   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];
B2 = blockproc(B,[8 8],@(block_struct) mask .* block_struct.data);
invdct = @(block_struct) T' * block_struct.data * T;
I2 = blockproc(B2,[8 8],invdct);
imshow(I), figure, imshow(I2)

      

+1


a source


To quantize DCT coefficients, you simply divide each coefficient by the quantum term and round to whole numbers. Quantization terms are often unique to each coefficient and are stored in the quantization matrix.

There is a good example on Wikipedia. Here's how to implement this example in Matlab.

coef = [
 -415  -33  -58   35   58  -51  -15  -12;
    5  -34   49   18   27    1   -5    3;
  -46   14   80  -35  -50   19    7  -18;
  -53   21   34  -20    2   34   36   12;
    9   -2    9   -5  -32  -15   45   37;
   -8   15  -16    7   -8   11    4    7;
   19  -28   -2  -26   -2    7  -44  -21;
   18   25  -12  -44   35   48  -37  -3
   ];

quant = [
 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
    ];

quantCoef = round(coef ./ quant)

quantCoef =
   -26    -3    -6     2     2    -1     0     0
     0    -3     4     1     1     0     0     0
    -3     1     5    -1    -1     0     0     0
    -4     1     2    -1     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


a source







All Articles