You are on page 1of 3

Huffman Compression:clear all

clc
%Input sequence
A=[10 11 12 13; 2 14 26 39];
display(A);
e=A;
A1=padarray(A,[1,0],0);
%Prediction error
for i = 2:size(A1,1)-1
for j = 2:size(A1,2)
fx=round(A1(i,j-1)+A1(i-1,j));
e(i-1,j)=e(i-1,j)-fx;
end
end
display(e);
%Huffman encoding
C=reshape(e,[],1);
[D1,x]=hist(C,min(min(e)):max(max(e)));
sym=x(D1>0);
prob=D1(D1>0)/numel(e);
[dict,avglen] = huffmandict(sym,prob);
comp = huffmanenco(C,dict);
%Huffman decoding
dsig = huffmandeco(comp,dict);
e=reshape(dsig,size(A,1),size(A,2));
%Inverse operation
d=e;
e1=padarray(e,[1,0],0);
for i = 2:size(e1,1)-1
for j = 2:size(e1,2)
fx=round(e1(i,j-1)+e1(i-1,j));
d(i-1,j)=d(i-1,j)+fx;
e1=padarray(d,[1,0],0);
end
end
display(d);

DCT compression technique:-

I=imread('C:\Users\Public\Pictures\Sample Pictures\009.jpg');
I=rgb2gray(I);
I=imresize(I, [400,400]);
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);
subplot(211)
imshow(I)
title('Original image');
subplot(212)
imshow(I2)

title('DCT Compress Image');

You might also like