9/13/2021 2-DCT Image compression - MATLAB Answers - MATLAB Central
MATLAB Answers
2-DCT Image compression
Follow 108 views (last 30 days) Show older comments
Amarnath R
on 3 Nov 2018 Vote 0
Commented: nor saziana ariani sazali
on 25 Jun 2021
Problem:
I tried implementing Discrete Cosine Transformation compression using matlab. Input image would a
jpg image (Lena) having a size 512 X 512.
There are two stages namely compression and decompression.
Compression and Quantization:
The input image is converted to YCbCr component. Then Y component is taken up for compression.
Further DCT will quantized.
Quantization and Decompression:
The quantized image is undergoes dequantization for decompression.
Issues:
Rectangular boxes are spotted in the decompressed version of the image. Is anything wrong with the
code? For your inference, below are the sample input and output images and followed by the matlab
code.
Input image:
https://in.mathworks.com/matlabcentral/answers/427708-2-dct-image-compression 1/9
9/13/2021 2-DCT Image compression - MATLAB Answers - MATLAB Central
MATLAB Answers
Y Component in YCbCr:
https://in.mathworks.com/matlabcentral/answers/427708-2-dct-image-compression 2/9
9/13/2021 2-DCT Image compression - MATLAB Answers - MATLAB Central
MATLAB Answers
Output image:
https://in.mathworks.com/matlabcentral/answers/427708-2-dct-image-compression 3/9
9/13/2021 2-DCT Image compression - MATLAB Answers - MATLAB Central
MATLAB Answers
Code:
clc;
clear all;
close all;
I = imread('lena512.jpg');
figure, imshow(I);
% Y = I;
YCbCr = rgb2ycbcr(I);
figure, imshow(YCbCr);
Y = YCbCr(:,:, 1);
figure, imshow(Y);
[h, w] = size(Y);
r = h/8;
c = w/8;
s = 1;
q50 = [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;
https://in.mathworks.com/matlabcentral/answers/427708-2-dct-image-compression 4/9
9/13/2021 2-DCT Image compression - MATLAB Answers - MATLAB Central
72 92 95 98 112 100 103 99];
MATLAB Answers
% COMPRESSION
for i=1:r
e = 1;
for j=1:c
block = Y(s:s+7,e:e+7);
cent = double(block) - 128;
for m=1:8
for n=1:8
if m == 1
u = 1/sqrt(8);
else
u = sqrt(2/8);
end
if n == 1
v = 1/sqrt(8);
else
v = sqrt(2/8);
end
comp = 0;
for x=1:8
for y=1:8
comp = comp + cent(x, y)*(cos((((2*(x-1))+1)*(m-1)*pi)/16))
end
end
F(m, n) = v*u*comp;
end
end
for x=1:8
for y=1:8
cq(x, y) = round(F(x, y)/q50(x, y));
end
end
Q(s:s+7,e:e+7) = cq;
e = e + 8;
end
s = s + 8;
end
% % % % % % % % % % % % % % %
% % DECOMPRESSION
% % % % % % %
s = 1;
for i=1:r
e = 1;
for j=1:c
cq = Q(s:s+7,e:e+7);
https://in.mathworks.com/matlabcentral/answers/427708-2-dct-image-compression 5/9
9/13/2021 2-DCT Image compression - MATLAB Answers - MATLAB Central
for x=1:8
for y=1:8
MATLAB Answers
DQ(x, y) = q50(x, y)*cq(x, y);
end
end
for m=1:8
for n=1:8
if m == 1
u = 1/sqrt(8);
else
u = sqrt(2/8);
end
if n == 1
v = 1/sqrt(8);
else
v = sqrt(2/8);
end
comp = 0;
for x=1:8
for y=1:8
comp = comp + u*v*DQ(x, y)*(cos((((2*(x-1))+1)*(m-1)*pi)/16
end
end
bf(m, n) = round(comp)+128;
end
end
Org(s:s+7,e:e+7) = bf;
e = e + 8;
end
s = s + 8;
end
imwrite(Y, 'F:\workouts\phd\jpeg\input.jpg');
imwrite(uint8(Org), 'F:\workouts\phd\jpeg\output.jpg');
return;
0 Comments
Sign in to comment.
Sign in to answer this question.
Answers (1)
https://in.mathworks.com/matlabcentral/answers/427708-2-dct-image-compression 6/9
9/13/2021 2-DCT Image compression - MATLAB Answers - MATLAB Central
Pham Minh Hanh
on 26 Mar 2021 Vote 0 Link
MATLAB Answers
Edited: Pham Minh Hanh
on 26 Mar 2021
Your decompression code is not right. IDCT is defined by:
so that u and v have to be inside the 2nd loop, not outside. Here is my code. The right image
is the input image, the left one is output. The decompressed image is similar to the input one,
so DCT works well as expected.
for x = 1:8
for y = 1:8
comp = 0;
for m = 1:8
for n = 1:8
if m == 1
u = 1/sqrt(2);
else
u = 1;
end
if n == 1
v = 1/sqrt(2);
https://in.mathworks.com/matlabcentral/answers/427708-2-dct-image-compression 7/9
9/13/2021 2-DCT Image compression - MATLAB Answers - MATLAB Central
else
v = 1; MATLAB Answers
end
comp = comp + u*v*DQ(m, n)*(cos((((2*(x-1))+1)*(m-1)*pi
end
end
bf(x, y) = round((1/4) *comp + 128);
end
5 Comments Show 4 older comments
nor saziana ariani sazali
on 25 Jun 2021
ohh i see..thankyou for explanation Walter
Sign in to comment.
Sign in to answer this question.
See Also
MATLAB Answers
image compression should bitmap image?
1 Answer
Why is my image not recombining properly after a DCT?
2 Answers
good evening , iam getting error as (im2jpeg(x, quality) Error using im2jpeg (line 21) The input must be a UINT8 image) at
line ...
1 Answer
Entire Website
Obfuscated MATLAB Code
Blogs
RGB2YCC(seq)
File Exchange
dct(y)
File Exchange
Categories
Image Processing and Computer Vision
> Image Processing Toolbox
> Image Filtering and Enhancement
Tags
dct discrete cosine tr... jpeg compression decompression
Products
MATLAB
https://in.mathworks.com/matlabcentral/answers/427708-2-dct-image-compression 8/9
9/13/2021 2-DCT Image compression - MATLAB Answers - MATLAB Central
Release
MATLAB Answers
R2014a
Poll
What’s your beverage of choice while programming in MATLAB?
Water
Tea
Coffee
Juice
Something harder
Nothing, I'm good
11962 votes
26 comments
2863 views
Vote
mathworks.com
© 1994-2021 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional
trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.
https://in.mathworks.com/matlabcentral/answers/427708-2-dct-image-compression 9/9