Skip to content

Instantly share code, notes, and snippets.

@semihozkoroglu
Created April 21, 2012 23:29
Show Gist options
  • Save semihozkoroglu/2440240 to your computer and use it in GitHub Desktop.
Save semihozkoroglu/2440240 to your computer and use it in GitHub Desktop.
Resim Sıkıştırma ve Çözme ~ Ayrık Kosinus Dönüşümü
function [J,N,K,D] = compress()
f = double(rgb2gray(imread('g.png')));
[R C] = size(f);
J = zeros(R,C);
N = zeros(R,C);
D = {};
K = {};
% Quantization 90
% Q = [3 2 2 3 5 8 10 12;
% 2 2 3 4 5 12 12 11;
% 3 3 4 5 8 11 14 11;
% 3 3 4 6 10 17 16 12;
% 4 4 7 11 14 22 21 15;
% 5 7 11 13 16 12 23 18;
% 10 13 16 17 21 24 24 21;
% 14 18 19 20 22 20 20 20];
% Quantization 10
% Q = [80 60 50 80 120 200 255 255;
% 55 60 70 95 130 255 255 255;
% 70 65 80 120 200 255 255 255;
% 70 85 110 145 255 255 255 255;
% 90 110 185 255 255 255 255 255;
% 120 175 255 255 255 255 255 255;
% 245 255 255 255 255 255 255 255;
% 255 255 255 255 255 255 255 255];
% Quantization 50
Q = [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];
T = zeros(8,8);
W = R/8;
H = C/8;
for a = 0:7
for b= 0:7
if a == 0
T(a+1,b+1) = 1/sqrt(8);
else
T(a+1,b+1) = sqrt(2/8)*cos((((2*b+1)*a*pi)/(2*8)));
end
end
end
for r = 1:W
for c = 1:H
D{r,c} = round(discreate_cos(f(((r-1)*8 + 1):((r-1)*8 + 8),((c-1)*8 + 1):((c-1)*8 + 8)),T));
K{r,c} = round(D{r,c}./Q);
J(((r-1)*8 + 1):((r-1)*8 + 8),((c-1)*8 + 1):((c-1)*8 + 8)) = D{r,c};
end
end
N = decompress(Q,T,K,N);
function D = discreate_cos(G,T)
[X Y] = size(G);
D = zeros(X,Y);
M = G - 128;
D = T*M*inv(T);
function N = decompress(Q,T,K,N)
[R C] = size(N);
W = R/8;
H = C/8;
for r = 1:W
for c = 1:H
R = Q.*[K{r,c}];
N(((r-1)*8 + 1):((r-1)*8 + 8),((c-1)*8 + 1):((c-1)*8 + 8)) = round(inv(T)*R*T) + 128;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment