IMAGE COMPRESSION & RECONSTRUCTION OF IMAGES

by

 

10 Oct 2006 (Updated )

PROGRAM DECOMPOSE IMAGE,COMPRESS IT, RECONSTRUCTION IS PERFORMED.

vi2.m
clear
clc
load wbarb; 
file=uigetfile({'*.tif';'*.png';'*.jpg';'*.gif';'*.*'},'Select the image','s');
X=imread(file);

load X
%imshow(X)
%figure
if size(X,1)~=size(X,2)
    X=X(1:256,1:256);
end
whos
image(X);colormap(map); title('Original image');colorbar;
pause
home
c=1
while c==1
wavelet=input('Select the wavelet ','s'); 
level=input('Select the level of decomposition ');
switch level
case 1,
%To perform a level 1 decomposition of the image
        % Reconstruction of the image from the approximate and details
[C,S] = wavedec2(X,1,wavelet);
A1 = wrcoef2('a',C,S,wavelet,1);
H1 = wrcoef2('h',C,S,wavelet,1); 
V1 = wrcoef2('v',C,S,wavelet,1); 
D1 = wrcoef2('d',C,S,wavelet,1); 
%Display the results of a first level decomposition.
colormap(map);
subplot(2,2,1); image(wcodemat(A1,192));
title('Approximation A1')
subplot(2,2,2); image(wcodemat(H1,192));
title('Horizontal Detail H1')
subplot(2,2,3); image(wcodemat(V1,192));
title('Vertical Detail V1')
subplot(2,2,4); image(wcodemat(D1,192));
title('Diagonal Detail D1')
pause
subplot(1,1,1,'replace')
colors = size(unique(X));
grayLevels = colors(1);
a1_cod = wcodemat(A1,grayLevels);
d1_hcod = wcodemat(H1,grayLevels);
d1_vcod = wcodemat(V1,grayLevels);
d1_dcod = wcodemat(D1,grayLevels);
L1 = [a1_cod,d1_hcod;d1_vcod,d1_dcod];
image(uint8(L1));
axis image;
title('Level 1 decomposition');
pause
% Compress the image and display it. 
% To compress the original image X, use the ddencmp command to calculate the default parameters
% and the wdencmp command to perform the actual compression. Type
[thr,sorh,keepapp] = ddencmp('cmp','wv',X);
thr
[Xcomp,CXC,LXC,PERF0,PERFL2] = wdencmp('gbl',C,S,wavelet,1,thr,sorh,keepapp);
PERF0
PERFL2
pause
NC = wthcoef2('t',C,S,1,thr,'s');
        % Reconstruction of the image from the approximate and details
        X0 = waverec2(NC,S,wavelet);
% Reconstruct the original image from the I level decomposition. 
%To reconstruct the original image from the wavelet decomposition structure, type
X=double(X)
mse=sum(sum((X-X0).*(X-X0)))/65536
PSNR=20*log10(255/sqrt(mse))
pq=pqs(X,Xcomp)
br=(524288*(1-(PERF0/100)))/65536
pause
%To view the compressed image side by side with the original, type
colormap(map);
subplot(1,2,1); image(X); title('Original Image');
axis square;
subplot(1,2,2); 
%image(Xcomp); title('Denoised Image');
%axis square
%subplot(1,3,3);
image(X0); title('Reconstructed Image');
axis square
while thr > 0
home
thr=input('give your required threshold value ');
%[thr,sorh,keepapp] = ddencmp('cmp','wv',X);
[Xcomp,CXC,LXC,PERF0,PERFL2] = wdencmp('gbl',C,S,wavelet,2,thr,sorh,keepapp);
PERF0
PERFL2
pause
NC = wthcoef2('t',C,S,1,thr,'s');
        % Reconstruction of the image from the approximate and details
        X0 = waverec2(NC,S,wavelet); 
X=double(X)
mse=sum(sum((X-X0).*(X-X0)))/65536
PSNR=20*log10(255/sqrt(mse))
pq=pqs(X,Xcomp)
br=(524288*(1-(PERF0/100)))/65536

pause
%To view the compressed image side by side with the original, type
colormap(map);
subplot(1,2,1); image(X); title('Original Image');
axis square
subplot(1,2,2);% image(Xcomp); title('Denoised Image');
%axis square
%subplot(1,3,3); 
image(X0); title('Reconstructed Image');
axis square
end
case 2,
%To perform a level 2 decomposition of the image
[C,S] = wavedec2(X,2,wavelet);
%Reconstruct the Level 2 approximation and the Level 1 and 2 details. 
%To reconstruct the level 2 approximation from C, type
A2 = wrcoef2('a',C,S,wavelet,2);
A1 = wrcoef2('a',C,S,wavelet,1);
H1 = wrcoef2('h',C,S,wavelet,1); 
V1 = wrcoef2('v',C,S,wavelet,1); 
D1 = wrcoef2('d',C,S,wavelet,1); 
H2 = wrcoef2('h',C,S,wavelet,2);
V2 = wrcoef2('v',C,S,wavelet,2); 
D2 = wrcoef2('d',C,S,wavelet,2);
colormap(map);
subplot(2,4,1);image(wcodemat(A1,192));
title('Approximation A1')
subplot(2,4,2);image(wcodemat(H1,192));
title('Horizontal Detail H1')
subplot(2,4,3);image(wcodemat(V1,192));
title('Vertical Detail V1')
subplot(2,4,4);image(wcodemat(D1,192));
title('Diagonal Detail D1')
subplot(2,4,5);image(wcodemat(A2,192));
title('Approximation A2')
subplot(2,4,6);image(wcodemat(H2,192));
title('Horizontal Detail H2')
subplot(2,4,7);image(wcodemat(V2,192));
title('Vertical Detail V2')
subplot(2,4,8);image(wcodemat(D2,192));
title('Diagonal Detail D2')
pause
subplot(1,1,1,'replace')
colors = size(unique(X));
grayLevels = colors(1);
a1_cod = wcodemat(A1,grayLevels);
d1_hcod = wcodemat(H1,grayLevels);
d1_vcod = wcodemat(V1,grayLevels);
d1_dcod = wcodemat(D1,grayLevels);
L1 = [a1_cod,d1_hcod;d1_vcod,d1_dcod];
image(uint8(L1));
axis image;
title('Level 1 decomposition');
pause
% 2nd level coefficients coding
a2_cod = wcodemat(A2,grayLevels);
d2_hcod = wcodemat(H2,grayLevels);
d2_vcod = wcodemat(V2,grayLevels);
d2_dcod = wcodemat(D2,grayLevels);
% Displaying level 2 structure
image(uint8([imresize([a2_cod,d2_hcod;d2_vcod,d2_dcod],size(d1_hcod),'bilinear'),d1_hcod;d1_vcod,d1_dcod]));
axis image;
title('Level 2 decomposition');
pause

% Compress the image and display

% Compress the image and display it. 
% To compress the original image X, use the ddencmp command to calculate the default parameters
% and the wdencmp command to perform the actual compression. Type
[thr,sorh,keepapp] = ddencmp('cmp','wv',X);
thr
[Xcomp,CXC,LXC,PERF0,PERFL2] = wdencmp('gbl',C,S,wavelet,2,thr,sorh,keepapp);
PERF0
PERFL2
pause
NC = wthcoef2('t',C,S,2,thr,'s');
        % Reconstruction of the image from the approximate and details
        X0 = waverec2(NC,S,wavelet);
        pause
% Reconstruct the original image from the I level decomposition. 
%To reconstruct the original image from the wavelet decomposition structure, type
X=double(X)
mse=sum(sum((X-X0).*(X-X0)))/65536
PSNR=20*log10(255/sqrt(mse))
pq=pqs(X,Xcomp)
br=(524288*(1-(PERF0/100)))/65536
pause
%To view the compressed image side by side with the original, type
colormap(map);
subplot(1,2,1); image(X); title('Original Image');
axis square
subplot(1,2,2); image(X0); title('Reconstructed image');
axis square
while thr > 0
home
thr=input('give your required threshold value ');
%[thr,sorh,keepapp] = ddencmp('cmp','wv',X);
[Xcomp,CXC,LXC,PERF0,PERFL2] = wdencmp('gbl',C,S,wavelet,2,thr,sorh,keepapp);
PERF0
PERFL2
pause
NC = wthcoef2('t',C,S,2,thr,'s');
        % Reconstruction of the image from the approximate and details
        X0 = waverec2(NC,S,wavelet);
        pause
X=double(X)
mse=sum(sum((X-X0).*(X-X0)))/65536
PSNR=20*log10(255/sqrt(mse))
pq=pqs(X,Xcomp)
br=(524288*(1-(PERF0/100)))/65536
pause
%To view the compressed image side by side with the original, type
colormap(map);
subplot(1,2,1); image(X); title('Original Image');
axis square
subplot(1,2,2); 
%image(Xcomp); title('Compressed Image');
%axis square
%subplot(1,3,3); 
image(X0); title('Reconstructed Image');
axis square
pause
end
case 3,
%To perform a level 3 decomposition of the image
[C,S] = wavedec2(X,3,wavelet);
%To reconstruct the level 1,2,3 approximation from C, type
A3 = wrcoef2('a',C,S,wavelet,3);
A2 = wrcoef2('a',C,S,wavelet,2);
A1 = wrcoef2('a',C,S,wavelet,1);
H1 = wrcoef2('h',C,S,wavelet,1); 
V1 = wrcoef2('v',C,S,wavelet,1); 
D1 = wrcoef2('d',C,S,wavelet,1); 
H2 = wrcoef2('h',C,S,wavelet,2);
V2 = wrcoef2('v',C,S,wavelet,2); 
D2 = wrcoef2('d',C,S,wavelet,3);
H3 = wrcoef2('h',C,S,wavelet,3);
V3 = wrcoef2('v',C,S,wavelet,3); 
D3 = wrcoef2('d',C,S,wavelet,3);

%Display the results of a multilevel decomposition.
% Displaying level 3 structure
colormap(map);
subplot(3,4,1);image(wcodemat(A1,192));
title('Approximation A1')
subplot(3,4,2);image(wcodemat(H1,192));
title('Horizontal Detail H1')
subplot(3,4,3);image(wcodemat(V1,192));
title('Vertical Detail V1')
subplot(3,4,4);image(wcodemat(D1,192));
title('Diagonal Detail D1')
subplot(3,4,5);image(wcodemat(A2,192));
title('Approximation A2')
subplot(3,4,6);image(wcodemat(H2,192));
title('Horizontal Detail H2')
subplot(3,4,7);image(wcodemat(V2,192));
title('Vertical Detail V2')
subplot(3,4,8);image(wcodemat(D2,192));
title('Diagonal Detail D2')
subplot(3,4,9);image(wcodemat(A3,192));
title('Approximation A3')
subplot(3,4,10);image(wcodemat(H3,192));
title('Horizontal Detail H3')
subplot(3,4,11);image(wcodemat(V3,192));
title('Vertical Detail V3')
subplot(3,4,12);image(wcodemat(D3,192));
title('Diagonal Detail D3')
pause
subplot(1,1,1,'replace')
colors = size(unique(X));
grayLevels = colors(1);
a1_cod = wcodemat(A1,grayLevels);
d1_hcod = wcodemat(H1,grayLevels);
d1_vcod = wcodemat(V1,grayLevels);
d1_dcod = wcodemat(D1,grayLevels);
L1 = [a1_cod,d1_hcod;d1_vcod,d1_dcod];
image(uint8(L1));
axis image;
title('Level 1 decomposition');
pause
% 2nd level coefficients coding
a2_cod = wcodemat(A2,grayLevels);
d2_hcod = wcodemat(H2,grayLevels);
d2_vcod = wcodemat(V2,grayLevels);
d2_dcod = wcodemat(D2,grayLevels);
% Displaying level 2 structure
image(uint8([imresize([a2_cod,d2_hcod;d2_vcod,d2_dcod],size(d1_hcod),'bilinear'),d1_hcod;d1_vcod,d1_dcod]));
axis image;
title('Level 2 decomposition');
pause
% 3rd level coefficients coding
a3_cod = wcodemat(A3,grayLevels);
d3_hcod = wcodemat(H3,grayLevels);
d3_vcod = wcodemat(V3,grayLevels);
d3_dcod = wcodemat(D3,grayLevels);
% Displaying level 3 structure
image(uint8([imresize([imresize([a3_cod,d3_hcod;d3_vcod,d3_dcod],size(d2_hcod),'bilinear'),d2_hcod;d2_vcod,d2_dcod],size(d1_hcod),'bilinear'),d1_hcod;d1_vcod,d1_dcod]));
axis image;
title('Level 3 decomposition');
pause

       
% Compress the image and display it. 
% To compress the original image X, use the ddencmp command to calculate the default parameters
% and the wdencmp command to perform the actual compression. Type
[thr,sorh,keepapp] = ddencmp('cmp','wv',X);
thr
[Xcomp,CXC,LXC,PERF0,PERFL2] = wdencmp('gbl',C,S,wavelet,3,thr,sorh,keepapp);
PERF0
PERFL2
pause
% Reconstruct the original image from the multilevel decomposition. 
%To reconstruct the original image from the wavelet decomposition structure, type
NC = wthcoef2('t',C,S,3,thr,'s');
        % Reconstruction of the image from the approximate and details
X0= waverec2(NC,S,wavelet); 
pause
X=double(X)
mse=sum(sum((X-X0).*(X-X0)))/65536
PSNR=20*log10(255/sqrt(mse))
pq=pqs(X,Xcomp)
br=(524288*(1-(PERF0/100)))/65536
pause
%To view the compressed image side by side with the original, type
colormap(map);
subplot(1,2,1); image(X); title('Original Image');
axis square
subplot(1,2,2); image(X0); title('Reconstructed image');
axis square
while thr > 0
home
thr=input('give your required threshold value ');
%[thr,sorh,keepapp] = ddencmp('cmp','wv',X);
[Xcomp,CXC,LXC,PERF0,PERFL2] = wdencmp('gbl',C,S,wavelet,3,thr,sorh,keepapp);
PERF0
PERFL2
pause
NC = wthcoef2('t',C,S,3,thr,'s');
        % Reconstruction of the image from the approximate and details
        X0 = waverec2(NC,S,wavelet);
        pause
X=double(X)
mse=sum(sum((X-X0).*(X-X0)))/65536
PSNR=20*log10(255/sqrt(mse))
pq=pqs(X,Xcomp)
br=(524288*(1-(PERF0/100)))/65536
pause
%To view the compressed image side by side with the original, type
colormap(map);
subplot(1,2,1); image(X); title('Original Image');
axis square
subplot(1,2,2); %image(Xcomp); title('Compressed Image');
%axis square
%subplot(1,3,3); 
image(X0); title('Reconstructed Image');
axis square
pause
end
case 4,
    %To perform a level 4 decomposition of the image
[C,S] = wavedec2(X,4,wavelet);
%To reconstruct the level 1,2,3,4 approximation from C, type
A4 = wrcoef2('a',C,S,wavelet,4);
A3 = wrcoef2('a',C,S,wavelet,3);
A2 = wrcoef2('a',C,S,wavelet,2);
A1 = wrcoef2('a',C,S,wavelet,1);
H1 = wrcoef2('h',C,S,wavelet,1); 
V1 = wrcoef2('v',C,S,wavelet,1); 
D1 = wrcoef2('d',C,S,wavelet,1); 
H2 = wrcoef2('h',C,S,wavelet,2);
V2 = wrcoef2('v',C,S,wavelet,2); 
D2 = wrcoef2('d',C,S,wavelet,2);
H3 = wrcoef2('h',C,S,wavelet,3);
V3 = wrcoef2('v',C,S,wavelet,3); 
D3 = wrcoef2('d',C,S,wavelet,3);
H4 = wrcoef2('h',C,S,wavelet,4);
V4 = wrcoef2('v',C,S,wavelet,4); 
D4 = wrcoef2('d',C,S,wavelet,4);    
% Display the results of a multilevel decomposition.
% Displaying level 4 structure
colormap(map);
subplot(4,4,1);image(wcodemat(A1,192));
title('Approximation A1')
subplot(4,4,2);image(wcodemat(H1,192));
title('Horizontal Detail H1')
subplot(4,4,3);image(wcodemat(V1,192));
title('Vertical Detail V1')
subplot(4,4,4);image(wcodemat(D1,192));
title('Diagonal Detail D1')
subplot(4,4,5);image(wcodemat(A2,192));
title('Approximation A2')
subplot(4,4,6);image(wcodemat(H2,192));
title('Horizontal Detail H2')
subplot(4,4,7);image(wcodemat(V2,192));
title('Vertical Detail V2')
subplot(4,4,8);image(wcodemat(D2,192));
title('Diagonal Detail D2')
subplot(4,4,9);image(wcodemat(A3,192));
title('Approximation A3')
subplot(4,4,10);image(wcodemat(H3,192));
title('Horizontal Detail H3')
subplot(4,4,11);image(wcodemat(V3,192));
title('Vertical Detail V3')
subplot(4,4,12);image(wcodemat(D3,192));
title('Diagonal Detail D3')
subplot(4,4,13);image(wcodemat(A4,192));
title('Approximation A4')
subplot(4,4,14);image(wcodemat(H4,192));
title('Horizontal Detail H4')
subplot(4,4,15);image(wcodemat(V4,192));
title('Vertical Detail V4')
subplot(4,4,16);image(wcodemat(D4,192));
title('Diagonal Detail D4')
pause   
subplot(1,1,1,'replace')
colors = size(unique(X));
grayLevels = colors(1);
a1_cod = wcodemat(A1,grayLevels);
d1_hcod = wcodemat(H1,grayLevels);
d1_vcod = wcodemat(V1,grayLevels);
d1_dcod = wcodemat(D1,grayLevels);
L1 = [a1_cod,d1_hcod;d1_vcod,d1_dcod];
image(uint8(L1));
axis image;
title('Level 1 decomposition');
pause
% 2nd level coefficients coding
a2_cod = wcodemat(A2,grayLevels);
d2_hcod = wcodemat(H2,grayLevels);
d2_vcod = wcodemat(V2,grayLevels);
d2_dcod = wcodemat(D2,grayLevels);
% Displaying level 2 structure
image(uint8([imresize([a2_cod,d2_hcod;d2_vcod,d2_dcod],size(d1_hcod),'bilinear'),d1_hcod;d1_vcod,d1_dcod]));
axis image;
title('Level 2 decomposition');
pause
% 3rd level coefficients coding
a3_cod = wcodemat(A3,grayLevels);
d3_hcod = wcodemat(H3,grayLevels);
d3_vcod = wcodemat(V3,grayLevels);
d3_dcod = wcodemat(D3,grayLevels);
% Displaying level 3 structure
image(uint8([imresize([imresize([a3_cod,d3_hcod;d3_vcod,d3_dcod],size(d2_hcod),'bilinear'),d2_hcod;d2_vcod,d2_dcod],size(d1_hcod),'bilinear'),d1_hcod;d1_vcod,d1_dcod]));
axis image;
title('Level 3 decomposition');
pause
%4th level coefficients coding
a4_cod = wcodemat(A4,grayLevels);
d4_hcod = wcodemat(H4,grayLevels);
d4_vcod = wcodemat(V4,grayLevels);
d4_dcod = wcodemat(D4,grayLevels);
% Displaying level 4 structure
image(uint8([imresize([imresize([imresize([a4_cod,d4_hcod;d4_vcod,d4_dcod],size(d3_hcod),'bilinear'),d3_hcod;d3_vcod,d3_dcod],size(d2_hcod),'bilinear'),d2_hcod;d2_vcod,d2_dcod],size(d1_hcod),'bilinear'),d1_hcod;d1_vcod,d1_dcod]));
axis image;
title('Level 4 decomposition');
pause
% To compress the original image X, use the ddencmp command to calculate the default parameters
% and the wdencmp command to perform the actual compression. Type
[thr,sorh,keepapp] = ddencmp('cmp','wv',X);
thr
[Xcomp,CXC,LXC,PERF0,PERFL2] = wdencmp('gbl',C,S,wavelet,4,thr,sorh,keepapp);
PERF0
PERFL2
pause
% Reconstruct the original image from the multilevel decomposition. 
%To reconstruct the original image from the wavelet decomposition structure, type
NC = wthcoef2('t',C,S,4,thr,'s');
        % Reconstruction of the image from the approximate and details
 X0 = waverec2(NC,S,wavelet);
X=double(X)
mse=sum(sum((X-X0).*(X-X0)))/65536
PSNR=20*log10(255/sqrt(mse))
pq=pqs(X,Xcomp)
br=(524288*(1-(PERF0/100)))/65536
pause
%To view the compressed image side by side with the original, type
colormap(map);
subplot(1,2,1); image(X); title('Original Image');
axis square
subplot(1,2,2); image(X0); title('Reconstructed Image');
axis square
while thr > 0
home
thr=input('give your required threshold value ');
%[thr,sorh,keepapp] = ddencmp('cmp','wv',X);
[Xcomp,CXC,LXC,PERF0,PERFL2] = wdencmp('gbl',C,S,wavelet,4,thr,sorh,keepapp);
PERF0
PERFL2
pause
NC = wthcoef2('t',C,S,4,thr,'s');
        % Reconstruction of the image from the approximate and details
        X0 = waverec2(NC,S,wavelet);
X=double(X)
mse=sum(sum((X-X0).*(X-X0)))/65536
PSNR=20*log10(255/sqrt(mse))
pq=pqs(X,Xcomp)
br=(524288*(1-(PERF0/100)))/65536
pause
%To view the compressed image side by side with the original, type
colormap(map);
subplot(1,2,1); image(X); title('Original Image');
axis square
%subplot(1,3,2); image(Xcomp); title('Compressed Image');
%axis square
subplot(1,2,2); image(X0); title('Reconstructed Image');
axis square
pause
end
otherwise,
display('Select the level of decomposition a number between 1 and 4'); 
%return  
end%switch end
c=input('Do you want to continue with experimenting another wavelet if yes enter 1 ');
end%while end

Contact us