Wavelet learning notes (image decomposition and reconstruction) - MATLAB

Posted by forced4 on Thu, 10 Feb 2022 02:41:43 +0100

There are two commands for MATLAB to realize image decomposition and reconstruction:

The first is one-layer wavelet decomposition dwt2

[CA,CH,CV,CD]=dwt2(X,'wname');

Where dwt2 represents discrete wavelet transform; X is the input parameter and is the image; ' wname 'is the name of wavelet; The output variable CA is the low-frequency decomposition information, which depicts the approximation information of the original image; CH is the low and high frequency decomposition information, depicting the transverse details of the original image; CV is the high and low frequency decomposition information, depicting the vertical details of the original image; CD is high-frequency decomposition information, depicting the details on the diagonal of the original image.

[CA,CH,CV,CD]=dwt2(X,Lo_D,Hi_D);

The input parameters can also be low-pass filter and high pass filter

The corresponding reconstruction commands are:

X=idwt2(CA,CH,CV,CD,'wname');

X=idwt2(CA,CH,CV,CD,Lo_D,Hi_D);

X=idwt2(CA,CH,CV,CD,Lo_D,Hi_D,'mode',MODE);

The second is multi-layer wavelet decomposition wavedec2

[C,S]=wavedec2(X,N,'wname');

Among them, wavedec 2 is a multi-layer wavelet decomposition function; X is the input parameter and is the image; N is the number of decomposition layers; ' 'ame' is the name of wavelet; The output variable C is the combination of all decomposition information, specifically C=[A(N)|H(N)|V(N)|D(N)....H(N-1)|V(N-1)|D(N-1)|...|H(1)|V(1)|D(1)], and C is the row vector; S is the size of each frequency band image after decomposition.

[C,S]=wavedec2(X,N,Lo_D,Hi_D);

The input parameters can also be low-pass filter and high pass filter

Since C represents a row vector, the functions appcoef2 and detcoef2 can return the coefficients of the approximation image expressed in two dimensions and the image coefficients of each high-frequency channel respectively. The specific form is

A=appcoef2(C,S,'wname',N);

D=detcoef2(O,C,S,N);

O can take 'h','v','d','all ', and return the decomposed horizontal high frequency, vertical high frequency and diagonal high frequency image coefficients.

When the O parameter is all, the return value is three high-frequency image coefficients, i.e

[H,V,D]=detcoef2('all',C,S,N);

There are two kinds of multi-layer reconstruction commands of wavelet, one is the complete reconstruction command waverec2, and the other is the partial reconstruction command wrcoef2, specifically:

X=waverec2(C,S,N,'wname');

X=waverec2(C,S,N,Lo_R,Hi_R);

X=waverec2('type',C,S,'wname',N);

X=waverec2('type',C,S,Lo_R,Hi_R,N);

X=waverec2('type',C,S,'wname');

X=waverec2('type',C,S,Lo_R,Hi_R);

wrcoef2 is used to reconstruct images from different frequency bands extracted by two-dimensional wavelet decomposition.

'type 'is a, h, V and D, corresponding to low frequency information, horizontal high frequency, vertical high frequency and diagonal high frequency information respectively

 

clc,close all,clear all
%-------One dimensional signal decomposition and reconstruction---------%
% load sumsin.mat
% [LO_D,HI_D,LO_R,HI_R]=wfilters('db3');%db3 Extraction of wavelet filter
% [CA,CD]=dwt(sumsin,'db3');%One layer decomposition of one-dimensional signal dwt function
% [C,L]=wavedec(sumsin,3,'db3');%Multilayer decomposition of one-dimensional signal wavedec,Acquisition function of hierarchical detail data detcoef and appco
% [CD1,CD2,CD3]=detcoef(C,L,[1 2 3]);
% CA3=appcoef(C,L,'db3',3);
% CA2=appcoef(C,L,'db3',2);
% CA1=appcoef(C,L,LO_R,HI_R,1);
% figure(1),
% subplot(3,1,1),plot(sumsin),title('original signal '),xlabel('t'),ylabel('X(t)');
% subplot(3,1,2),plot(CA),title('db3 Decompose low frequency signal'),xlabel('t'),ylabel('CA(t)');
% subplot(3,1,3),plot(CD),title('db3 Decompose high frequency signal'),xlabel('t'),ylabel('CD(t)');
% figure(2),
% subplot(3,2,1),plot(CA3),title('Approximation signal after three-layer decomposition'),xlabel('t'),ylabel('CA3(t)');
% subplot(3,2,2),plot(CD3),title('High frequency signal after three-layer decomposition'),xlabel('t'),ylabel('CD3(t)');
% subplot(3,2,3),plot(CA2),title('Approximation signal after two-layer decomposition'),xlabel('t'),ylabel('CA2(t)');
% subplot(3,2,4),plot(CD2),title('High frequency signal after two-layer decomposition'),xlabel('t'),ylabel('CD2(t)');
% subplot(3,2,5),plot(CA1),title('Approximation signal after one-layer decomposition'),xlabel('t'),ylabel('CA1(t)');
% subplot(3,2,6),plot(CD1),title('High frequency signal after one layer decomposition'),xlabel('t'),ylabel('CD1(t)');
% 
% Y=idwt(CA,CD,'db3','zpd');%Original signal reconstruction
% figure(3),
% subplot(3,1,1),plot(sumsin),title('original signal '),xlabel('t'),ylabel('X(t)');
% subplot(3,1,2),plot(Y),title('Reconstructed signal'),xlabel('t'),ylabel('Y(t)');
% subplot(3,1,3),plot(Y-sumsin),title('Original and reconstructed error signals'),xlabel('t'),ylabel('EX(t)');
% 
% YA=idwt(CA,[],'db3','zpd');%Reconstructed signal with only low-pass information
% YD=idwt([],CD,'db3','zpd');%Reconstructed signal with only high pass information
% 
% CCD1=C;%CCD1 Remove the low-pass signal, CCD2 All high pass signals are removed and reconstructed
% CCD2=C;
% CCD1(1:L(1))=0;
% CCD2(L(1)+1:end)=0;
% YA3=waverec(CCD2,L,LO_R,HI_R);
% YD3=waverec(CCD1,L,'db3');
% figure(4)
% subplot(5,1,1),plot(sumsin),title('original signal '),xlabel('t'),ylabel('X(t)');
% subplot(5,1,2),plot(YA),title('Reconstructed signal from low-pass information only'),xlabel('t'),ylabel('YA(t)');
% subplot(5,1,3),plot(YD),title('Reconstructed signal from high pass information only'),xlabel('t'),ylabel('YD(t)');
% subplot(5,1,4),plot(YA3),title('Only the low-pass information decomposed by three layers is used to reconstruct the signal'),xlabel('t'),ylabel('YA3(t)');
% subplot(5,1,5),plot(YD3),title('Only all high pass information after three-layer decomposition is used to reconstruct the signal'),xlabel('t'),ylabel('YD3(t)');


%---------Image decomposition and reconstruction-------------%
load woman
[CA,CH,CV,CD]=dwt2(X,'haar');%One layer wavelet decomposition of two-dimensional image signal
[C,S]=wavedec2(X,2,'haar');%Multilayer wavelet decomposes two-dimensional image signal, which is decomposed into two layers here, N=2
CA2=appcoef2(C,S,'haar',2);%Extract the low-frequency coefficient of the second layer decomposition
%Extract the high-frequency coefficients in all directions of the second layer decomposition
CH2=detcoef2('h',C,S,2);%Horizontal high frequency
CV2=detcoef2('v',C,S,2);%Vertical high frequency
CD2=detcoef2('d',C,S,2);%Diagonal high frequency

CA1=appcoef2(C,S,'haar',1);%Extract the low-frequency coefficient of the first layer decomposition
[CH1,CV1,CD1]=detcoef2('all',C,S,1);%Extract the high-frequency coefficient after the decomposition of the first layer at one time
PCH=[CA2 CH2;CV2 CD2];%Splice the decomposed coefficients into a matrix, PCH Store the coefficient after decomposition of the second layer, PCH1 Coefficient of two-layer decomposition
PCH1=[PCH CH1;CV1 CD1];


%display dwt2 Low frequency and high frequency coefficients obtained by decomposing the first layer
figure(1),
subplot(2,2,1),imshow(CA,[]),title('Approximation information CA');
subplot(2,2,2),imshow(CH,[]),title('Lateral details CH');
subplot(2,2,3),imshow(CV,[]),title('Vertical details CV');
subplot(2,2,4),imshow(CD,[]),title('Diagonal details CD');

%Display the low-frequency and high-frequency coefficients obtained by the second layer decomposition
figure(2),
subplot(2,2,1),imshow(CA2,[]),title('Second layer approximation information CA2');
subplot(2,2,2),imshow(CH2,[]),title('Horizontal detail information of the second floor CH2');
subplot(2,2,3),imshow(CV2,[]),title('Second floor vertical details CV2');
subplot(2,2,4),imshow(CD2,[]),title('Diagonal details of the second floor CD2');

%Display the image spliced by the second layer and the high-frequency information decomposed by the first layer
figure(3)
subplot(2,2,1),imshow(PCH,[]),title('Second layer mosaic image');
subplot(2,2,2),imshow(CH1,[]),title('First floor horizontal details CH1');
subplot(2,2,3),imshow(CV1,[]),title('First floor vertical details CV1');
subplot(2,2,4),imshow(CD1,[]),title('Diagonal details on the first floor CD1');

%Comparison of all coefficient images after decomposition with the original image
figure(4)
subplot(1,2,1),imshow(X,[]),title('original image ');
subplot(1,2,2),imshow(PCH1,[]),title('All coefficient mosaic images');

%Partial reconstruction command of wavelet wrcoef2('type',C,S,'wname')
A2=wrcoef2('a',C,S,'haar',2);%Reconstruct the low-frequency signal of the second layer
H2=wrcoef2('h',C,S,'haar',2);%Reconstruct the horizontal high frequency signal of the second layer
V2=wrcoef2('v',C,S,'haar',2);%Reconstruct the vertical high frequency signal of the second layer
D2=wrcoef2('d',C,S,'haar',2);%Reconstruct the diagonal high-frequency signal of the second layer


A1=wrcoef2('a',C,S,'haar',1);%Reconstruct the low-frequency signal of the first layer
H1=wrcoef2('h',C,S,'haar',1);%Reconstruct the horizontal high frequency signal of the first layer
V1=wrcoef2('v',C,S,'haar',1);%Reconstruct the vertical high frequency signal of the first layer
D1=wrcoef2('d',C,S,'haar',1);%Reconstruct the diagonal high-frequency signal of the first layer


%Complete reconstruction command of wavelet waverec2(C,S,N,'wname')
CCA=zeros(size(C));
CCH=zeros(size(C));
CCV=zeros(size(C));
CCD=zeros(size(C));
CCA(1:S(1,1)*S(1,2))=C(1:S(1,1)*S(1,2));
CCH(S(1,1)*S(1,2)+1:S(1,1)*S(1,2)*2)=C(S(1,1)*S(1,2)+1:S(1,1)*S(1,2)*2);
CCV(S(1,1)*S(1,2)*2+1:S(1,1)*S(1,2)*3)=C(S(1,1)*S(1,2)*2+1:S(1,1)*S(1,2)*3);
CCD(S(1,1)*S(1,2)*3+1:S(1,1)*S(1,2)*4)=C(S(1,1)*S(1,2)*3+1:S(1,1)*S(1,2)*4);
YA2=waverec2(CCA,S,'haar');
YH2=waverec2(CCH,S,'haar');
YV2=waverec2(CCV,S,'haar');
YD2=waverec2(CCD,S,'haar');


%idwt2 restructure
YA=idwt2(CA,[],[],[],'haar');
YH=idwt2([],CH,[],[],'haar');
YV=idwt2([],[],CV,[],'haar');
YD=idwt2([],[],[],CD,'haar');
YA11D=idwt2([],CH,CV,CD,'haar');

figure(5),
subplot(2,3,1),imshow(X,[]),title('original image ')
subplot(2,3,2),imshow(YA,[]),title('The image reconstructed by the first layer approximation information')
subplot(2,3,3),imshow(YH,[]),title('The image reconstructed by the first layer of horizontal detail information')
subplot(2,3,4),imshow(YV,[]),title('Image reconstructed from the first layer of longitudinal detail information')
subplot(2,3,5),imshow(YV,[]),title('The first layer of diagonal detail information reconstructs the image')
subplot(2,3,6),imshow(YA11D,[]),title('The reconstructed image of all details in the first layer')
figure(6)
subplot(2,2,1),imshow(YA2,[]),title('Image reconstructed by second layer approximation information under complete reconstruction command')
subplot(2,2,2),imshow(YH2,[]),title('Image reconstructed by the second layer of lateral detail information under the complete reconstruction command')
subplot(2,2,3),imshow(YV2,[]),title('Image reconstructed by the second layer of longitudinal detail information under the complete reconstruction command')
subplot(2,2,4),imshow(YD2,[]),title('Image reconstructed by diagonal detail information of the second layer under the complete reconstruction command')
figure(7)
subplot(2,2,1),imshow(A2,[]),title('Image reconstructed by second layer approximation information under partial reconstruction command')
subplot(2,2,2),imshow(H2,[]),title('Image reconstructed by the second layer of lateral detail information under the partial reconstruction command')
subplot(2,2,3),imshow(V2,[]),title('Image reconstructed by the second layer of longitudinal detail information under the partial reconstruction command')
subplot(2,2,4),imshow(D2,[]),title('Image reconstructed by diagonal detail information of the second layer under partial reconstruction command')





%------Validate with Toolkit----------%
%wavemenu

 

Topics: MATLAB