# Need help on my DCT and Quantization code for Image Compression.

138 views (last 30 days)
Hiroshi Nakamura on 16 May 2015
Edited: Prathyusha K on 20 Nov 2017
Hello guys, I would really appreciate it if anyone could point out the mistakes in my code. I am trying to encode and decode an image by reading it in, performing DCT, Quantization then dequantizing it and performing inverse DCT. After running this code, the output Image, I2 is kind of pixellated. I have no idea how to fix it. The output should be somewhat similar to the original image but slightly blurred as it has undergone compression. Please help! My code is as follows :-
I = imread('cameraman.tif');
I = im2double(I);
T = dctmtx(8); % dct matrix
%Performing DCT on blocks of 8 by 8
dct = @(block_struct) T * block_struct.data * T';
B = blockproc(I,[8 8],dct);
B = ceil(B);
% A Standard Quantization Matrix
q_mtx = [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];
%PErforming Quantization by Dividing with q_mtx on blocks of 8 by 8
c = @(block_struct) (block_struct.data) ./ q_mtx;
B2 = blockproc(B,[8 8],c);
% B2 = ceil(B2)
%Performing Inverse Quantization By Multiplying with q_mtx on Blocks of 8
%by 8
B3 = blockproc(B2,[8 8],@(block_struct) q_mtx .* block_struct.data);
%Performing Inverse DCT on Blocks of 8 by 8
invdct = @(block_struct) T' * block_struct.data * T;
% B3 = ceil(B3);
I2 = blockproc(B3,[8 8],invdct);
imshow(I), figure, imshow(I2)
##### 1 CommentShowHide None
matlabgeek on 11 Jan 2016
Have you fixed this problem?

Sign in to comment.

### Answers (3)

Hiroshi Nakamura on 16 May 2015
Just in case you're wondering after running the code, I2 looks like this :-
Please help!
##### 1 CommentShowHide None
B.k Sumedha on 16 May 2015

Sign in to comment.

B.k Sumedha on 16 May 2015
Edited: B.k Sumedha on 11 Mar 2016
I = imread('cameraman.tif');
I = im2double(I);
T = dctmtx(8);
B = blkproc(I,[8 8],'P1*x*P2',T,T');
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 = blkproc(B,[8 8],'P1.*x',mask);
I2 = blkproc(B2,[8 8],'P1*x*P2',T',T);
imshow(I), figure, imshow(I2)
##### 2 CommentsShowHide 1 older comment
B.k Sumedha on 16 May 2015
If the answer is accepted then pleae click on accepted answer:)

Sign in to comment.

Prathyusha K on 20 Nov 2017
Edited: Prathyusha K on 20 Nov 2017
I = imread('cameraman.tif');
I = double(I);
T = dctmtx(8); % dct matrix
dct = @(block_struct) T * block_struct.data * T';
B = blockproc(I,[8 8],dct);
q_mtx = [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];
c = @(block_struct)(block_struct.data) ./ q_mtx;
B2 = blockproc(B,[8 8],c);
B2 = round(B2);
B3 = blockproc(B2,[8 8],@(block_struct) q_mtx .* block_struct.data);
invdct = @(block_struct) round(T' * block_struct.data * T);
I2 = blockproc(B3,[8 8],invdct);
imagesc(I); colormap gray; figure, imagesc(I2), colormap gray;
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Computer Vision With Simulink in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!