A new pixel value is obtained by finding the difference
between the current pixel and the predicted pixel value.
Encoding:
STEPS TO BE PERFORMED:
clear all
clc
%Read the input image
% A=imread('rice.png');
% figure,imshow(A);
% A=double(A);
A=[10 2 3 4;5 6 7 8];
display(A);
e=A;
%Perform prediction error
for i = 1:size(A,1)
for j = 2:size(A,2)
e(i,j)=e(i,j)-A(i,j-1);
end
end
display(e);
%Huffman coding
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));
d=e;
for i = 1:size(A,1)
for j = 2:size(A,2)
d(i,j)=d(i,j-1)+e(i,j);
end
end
display(d);
%Decompressed Image
%figure,imshow(uint8(d));
2-D Prediction:
Consider an array [ a b
c d]
To perform prediction error using 2-D linear operator,
e(2,2)=d-(c+b) i.e subtract the pixels left and top to the current pixel
To decompress, perform inverse operation f(2,2)=d+(c+b)
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);
A is the input value, e is the encoded value, d is the decoded value |
1 comments:
Thank you for the code. But I am confused with data types in matlab. I tried to use it on an image but it works only if I convert it to double (uint8 otherwise). Have you got any idea why?
Enjoyed Reading? Share Your Views