2-DCT Image Compression - MATLAB Answers - MATLAB Central
2-DCT Image Compression - MATLAB Answers - MATLAB Central
MATLAB Answers
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.
The input image is converted to YCbCr component. Then Y component is taken up for compression.
Further DCT will quantized.
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
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.
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
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
Sign in to comment.
See Also
MATLAB Answers
image compression should bitmap image?
1 Answer
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
Water
Tea
Coffee
Juice
Something harder
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