-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 313bcb0
Showing
7 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function [D] = dis_cos(Y) | ||
% DIS_COS input : 8×8 matrix, output : 8×8 matrix | ||
% Calculate DCT of the input and return it's DCT matrix | ||
D = zeros(8,8); | ||
for i = 1:8 | ||
for j = 1:8 | ||
for x = 1:8 | ||
for y = 1:8 | ||
D(i,j) = D(i,j)+Y(x,y)*cos((2*(y-1)+1)*(j-1)*pi/16)*cos((2*(x-1)+1)*(i-1)*pi/16); | ||
end | ||
end | ||
if ((i-1)==0) | ||
Ci = 1/sqrt(8); | ||
else | ||
Ci = sqrt(2/8); | ||
end | ||
if ((j-1)==0) | ||
Cj = 1/sqrt(8); | ||
else | ||
Cj = sqrt(2/8); | ||
end | ||
D(i,j) = D(i,j)*Ci*Cj; | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function [D] = idis_cos(Y) | ||
% IDIS_COS input : 8×8 matrix, output : 8×8 matrix | ||
% Calculate IDCT of the input and return it's matrix | ||
D=zeros(8,8); | ||
for i=1:8 | ||
for j=1:8 | ||
for x=1:8 | ||
for y=1:8 | ||
if ((x-1)==0) | ||
Ci=1/sqrt(8); | ||
else | ||
Ci=sqrt(2/8); | ||
end | ||
if ((y-1)==0) | ||
Cj=1/sqrt(8); | ||
else | ||
Cj=sqrt(2/8); | ||
end | ||
D(i,j)=D(i,j)+Y(x,y)*Ci*Cj*cos((2*(j-1)+1)*(y-1)*pi/16)*cos((2*(i-1)+1)*(x-1)*pi/16); | ||
end | ||
end | ||
D(i,j)=D(i,j); | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function [iZ] = irle(E) | ||
% IRLE input : 1D array output : 2D matrix | ||
% Run length decode every zeros and convert into a 2D matrix | ||
n = length(E); | ||
arr = []; | ||
i = 1; | ||
while i<=n | ||
if E(i)==0 | ||
for j = 1:E(i+1) | ||
arr = [arr,0]; | ||
end | ||
i = i+1; | ||
else | ||
arr = [arr,E(i)]; | ||
end | ||
i = i+1; | ||
end | ||
iZ = arr; | ||
iZ = reshape(iZ,64,[]); | ||
iZ = iZ'; | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function [z] = izigzag(iZ,w,h) | ||
% IZIGZAG input : 2D matrix output : 2D matrix | ||
% reverse zigzag sequence each row of input matrix | ||
k = 1; | ||
z = zeros(w,h); | ||
for i=1:8:w | ||
for j=1:8:h | ||
P2 = iZ(k,:); | ||
P = [P2(1),P2(2),P2(6),P2(7),P2(15),P2(16),P2(28),P2(29); | ||
P2(3),P2(5),P2(8),P2(14),P2(17),P2(27),P2(30),P2(43); | ||
P2(4),P2(9),P2(13),P2(18),P2(26),P2(31),P2(42),P2(44); | ||
P2(10),P2(12),P2(19),P2(25),P2(32),P2(41),P2(45),P2(54); | ||
P2(11),P2(20),P2(24),P2(33),P2(40),P2(46),P2(53),P2(55); | ||
P2(21),P2(23),P2(34),P2(39),P2(47),P2(52),P2(56),P2(61); | ||
P2(22),P2(35),P2(38),P2(48),P2(51),P2(57),P2(60),P2(62); | ||
P2(36),P2(37),P2(49),P2(50),P2(58),P2(59),P2(63),P2(64)]; | ||
z(i:i+7,j:j+7)=P; | ||
k=k+1; | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
clc; | ||
close all; | ||
clear all; | ||
|
||
% Read Image | ||
img = imread('peppers.png'); | ||
img_size = numel(img); % Number of bytes to store the image | ||
str1 = ['Initial image size = ',num2str(img_size),' bytes']; | ||
disp(str1); | ||
w = size(img,1); | ||
h = size(img,2); | ||
|
||
% Make number of rows and columns multiple of 8 | ||
if(mod(w,8)==0) | ||
w=w-8; | ||
else | ||
w=w-(8+mod(w,8)); | ||
end | ||
if(mod(h,8)==0) | ||
h=h-8; | ||
else | ||
h=h-(8+mod(h,8)); | ||
end | ||
|
||
% Convert RGB to YCbCr | ||
img_ycbcr = rgb2ycbcr(img); | ||
|
||
% Downsample | ||
I_d = img_ycbcr; | ||
I_d(:,2:2:end,2) =I_d(:,1:2:end-1,2); | ||
I_d(2:2:end,:,2) =I_d(1:2:end-1,:,2); | ||
I_d(:,2:2:end,3) =I_d(:,1:2:end-1,3); | ||
I_d(2:2:end,:,3) =I_d(1:2:end-1,:,3); | ||
|
||
% DCT compression and Quantization | ||
I_d = double(I_d); | ||
% Quatization Matrix | ||
Q = [16 11 10 16 24 40 51 61 ; | ||
12 12 14 19 26 28 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]; | ||
quantn_out = zeros(w,h,3); | ||
iquantn_out = quantn_out; | ||
idct_out = quantn_out; | ||
|
||
for channel = 1:3 | ||
for j = 1:8:w | ||
for k = 1:8:h | ||
block = I_d(j:j+7,k:k+7,channel); | ||
dct_out = dis_cos(block); % DCT | ||
block_quantn = round(dct_out./Q); % Quantization | ||
quantn_out(j:j+7,k:k+7,channel) = block_quantn; | ||
end | ||
end | ||
end | ||
|
||
% Encoding | ||
% Zig-Zag and Run length encoding | ||
Z1 = zigzag(quantn_out(:,:,1),w,h); | ||
E1 = rle(Z1); | ||
Z2 = zigzag(quantn_out(:,:,2),w,h); | ||
E2 = rle(Z2); | ||
Z3 = zigzag(quantn_out(:,:,3),w,h); | ||
E3 = rle(Z3); | ||
|
||
% Size after Compression | ||
OpBytes = 8*numel(E1)+8*numel(E2)+8*numel(E3); | ||
space_saved = img_size-OpBytes; | ||
str1 = ['Initial size-Final size = ',num2str(space_saved),' bytes']; | ||
disp(str1); | ||
|
||
% Decompression | ||
% Decoding | ||
iZ1 = irle(E1); | ||
iquantn_out(:,:,1) = izigzag(iZ1,w,h); | ||
iZ2 = irle(E2); | ||
iquantn_out(:,:,2) = izigzag(iZ2,w,h); | ||
iZ3 = irle(E3); | ||
iquantn_out(:,:,3) = izigzag(iZ3,w,h); | ||
|
||
% Dequantization and Inverse DCT | ||
for channel = 1:3 | ||
for j = 1:8:w | ||
for k = 1:8:h | ||
block = iquantn_out(j:j+7,k:k+7,channel); | ||
block_dquantn = block.*Q; % Dequantization | ||
idct_out(j:j+7,k:k+7,channel) = idis_cos(block_dquantn); % IDCT | ||
end | ||
end | ||
end | ||
|
||
% Results | ||
subplot(1,2,1) | ||
imshow(img) | ||
title('Original Image') | ||
subplot(1,2,2) | ||
imshow(ycbcr2rgb(uint8(idct_out))); | ||
title('Reconstructed Image') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function [out] = rle(array) | ||
% RLE input : 2D matrix output : 1D array | ||
% Convert the matrix in to a 1D array and then run length encode every zeros in the array | ||
out = []; | ||
array = array'; | ||
array = array(:); | ||
oarr = array'; | ||
n = length(oarr); | ||
i =1; | ||
while i<=n | ||
count = 1; | ||
if oarr(i)==0 | ||
while (i<n-1)&&(oarr(i) == oarr(i+1)) | ||
count = count+1; | ||
i = i+1; | ||
end | ||
out = [out,oarr(i)]; | ||
out = [out,count]; | ||
else | ||
out = [out,oarr(i)]; | ||
end | ||
i = i+1; | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function [Z] = zigzag(z,w,h) | ||
% ZIGZAG input : a matrix(w×h),number of rows,number of columns output : matrix of size (w×h/64)×64 | ||
% zigzag sequence each 8×8 block of input matrix | ||
Z=zeros(w*h/64,64); | ||
x=0; | ||
for i=1:8:w | ||
for j=1:8:h | ||
x=x+1; | ||
A=z(i:i+7,j:j+7); | ||
Z(x,:)=[A(1,1),A(1,2),A(2,1),A(3,1),A(2,2),A(1,3),A(1,4),A(2,3),A(3,2),A(4,1),A(5,1),A(4,2),A(3,3),A(2,4),A(1,5),A(1,6),A(2,5),A(3,4),A(4,3),A(5,2),A(6,1),A(7,1),A(6,2),A(5,3),A(4,4),A(3,5),A(2,6),A(1,7),A(1,8),A(2,7),A(3,6),A(4,5),A(5,4),A(6,3),A(7,2),A(8,1),A(8,2),A(7,3),A(6,4),A(5,5),A(4,6),A(3,7),A(2,8),A(3,8),A(4,7),A(5,6),A(6,5),A(7,4),A(8,3),A(8,4),A(7,5),A(6,6),A(5,7),A(4,8),A(5,8),A(6,7),A(7,6),A(8,5),A(8,6),A(7,7),A(6,8),A(7,8),A(8,7),A(8,8)]; | ||
end | ||
end | ||
end | ||
|