%wavelet based compression using Sofm
clear all;
% CLOSE ALL closes all the open figure windows.
close all;
%read the image
% Load original image.
load woman;
% X contains the loaded image.
% map contains the loaded colormap.
c=input('enter the codebook size');
b=input('enter the block size b=');
if b==4
imshow(uint8(X));
else
imshow('invalid');
end
nbcol = size(map,1);
imshow(uint8(X));
figure;
% Perform single-level decomposition
% of X using haar.
[cA1,cH1,cV1,cD1] = dwt2(X,'haar');
% Images coding.
cod_X = wcodemat(X,nbcol);
cod_cA1 = wcodemat(cA1,nbcol);
cod_cH1 = wcodemat(cH1,nbcol);
cod_cV1 = wcodemat(cV1,nbcol);
cod_cD1 = wcodemat(cD1,nbcol);
dec2d = [cod_cA1, cod_cH1; cod_cV1, cod_cD1 ];
imshow(uint8(dec2d));
%A5 = idwt2(cod_cA1, cod_cH1, cod_cV1, cod_cD1,'db1');
%imshow(uint8(A5));
%disp('dfds');
[cA2,cH2,cV2,cD2] = dwt2(cA1,'haar');
% Images coding.
cod_cA1 = wcodemat(cA1,nbcol);
cod_cA2 = wcodemat(cA2,nbcol);
cod_cH2 = wcodemat(cH2,nbcol);
cod_cV2 = wcodemat(cV2,nbcol);
cod_cD2 = wcodemat(cD2,nbcol);
dec2d = [cod_cA2, cod_cH2; cod_cV2, cod_cD2 ];
imshow(uint8(dec2d));
figure;
% disp('dfds');
[cA3,cH3,cV3,cD3] = dwt2(cA2,'haar');
% Images coding.
cod_cA2 = wcodemat(cA2,nbcol);
cod_cA3 = wcodemat(cA3,nbcol);
cod_cH3 = wcodemat(cH3,nbcol);
cod_cV3 = wcodemat(cV3,nbcol);
cod_cD3 = wcodemat(cD3,nbcol);
dec2d = [cod_cA3, cod_cH3; cod_cV3, cod_cD3 ];
% imshow(uint8(dec2d));
%disp('dfds');
[cA4,cH4,cV4,cD4] = dwt2(cA3,'haar');
% Images coding.
cod_cA3 = wcodemat(cA3,nbcol);
cod_cA4 = wcodemat(cA4,nbcol);
cod_cH4 = wcodemat(cH4,nbcol);
cod_cV4 = wcodemat(cV4,nbcol);
cod_cD4 = wcodemat(cD4,nbcol);
dec2d = [cod_cA4, cod_cH4; cod_cV4, cod_cD4 ];
%imshow(uint8(dec2d));
%disp('dfds');
% Using some plotting commands,
% the following figure is generated.
%n=input('enter the decomposition level');
%[Lo_D,Hi_D,Lo_R,Hi_R] = wfilters('haar');
%[c,s]=wavedec2(uint8(dec2d),1,Lo_D,Hi_D);
%[thr,nkeep] = wdcbm2(uint8(dec2d),1.5,prod(s(1,:)));
%disp('level-dependent thresholds');
%disp(thr);
%disp(' numbers of coefficients to be');
%disp(nkeep);
comp_image = IMNOISE(uint8(X),'gaussian',0,.1);
[compressed_image,TREED,PERF0,PERFL2] =WPDENCMP(148,'s',1,'haar','threshold',2,1);
subplot(2,1,1);
imshow(uint8(X));
title('original image');
subplot(2,1,2);
imshow(comp_image);
title('compressed_image');
%disp(PERF0);
% disp('compression ratio=');