2
$\begingroup$

I need first $25 \times 25$ coefficients of DCT to encrypt an image in Matlab.

How do I obtain them ?

I used this code to compute DCT of an image :

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)

The statement in the paper I read is :

Basic constructions: Let $V$ be a feature space of dimension $d$ (for instance, the space $R^d$ corresponding to the entries in the top left corner of the DCT of a digital image excluding the DC component) and consider an original feature $φ = (φ_1, . . . , φ_d)$.

The paper gives also some practical details like this :

We choose as a feature space the upper left $25 \times 25$ coefficients of the DCT (excluding the DC component).

$\endgroup$
  • $\begingroup$ I've found that dct2 Matlab function does all the job for me and gives me exactly what I need : dct coefficients, then I extract them as suggested below. Thank you! $\endgroup$ – user2991856 May 26 '15 at 20:52
2
$\begingroup$

In JPEG compression, DCT coefficients are generally parsed not as sub-blocks, but in an order:

corresponding to the entries in the top left corner

This corresponds to a zigzag indexing:

Zigzag ording of the JPEG

This can be obtained by the following code. Here I use the $8\times8$ JPEG quantization table, and extracted the $5\times5$ top-left coefficients. On a light note, the bijection between the $2$D coordinates and the zigzag $1$D coordinate can be obtained by Cantor pairing polynomials (Matlab implementation).

% ExtractComponent
nCoomponent = 25;
quantTable = [    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];

index = zigzag(length(quantTable));
indexCrop = index(2:26);
quantTableCrop = zeros(size(quantTable));
quantTableCrop(indexCrop) = quantTable(indexCrop);

figure(1)
image(quantTable);
figure(2)
image(quantTableCrop);

and the function

function index = zigzag(size)

% ZIGZAG(size) returns the zigzag order index
% for a size x size square matrix

    index = 1:size*size;
    [j,i] =  meshgrid(1:size);
    i = i(:);
    j = j(:);
    iplusj = i + j;
    even_ind = (rem(iplusj,2) == 0);
    odd_ind = ~even_ind;
    j(even_ind) =   j(even_ind);
    j(odd_ind) =    i(odd_ind);
    [nouse,tags] = sortrows([iplusj,j]);
    index = index(tags);
end
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.