📄 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')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -