I am applying dwt2 function on an image to apply wavelet transformation which is giving four result images cA(low pass image),cH (Horizontal Detail image),cV(Vertical Detail Image),cD (Diagonal Detail Image). Every this fine till Now. I want to visualize those result images.
Present I am visualizing those four result images using this below code.
image = imread(imagePath);
wavename = 'haar';
[cA,cH,cV,cD] = dwt2(im2double(iamge),wavename);
imshow([cA,cH; cV,cD],'Colormap',gray);
when i run this code visualizing result look like this
But i want my results should look like this can any one help me please.
In the second image of your question, the two-level wavelet transform is displayed. With your code example using dwt2
, you are only doing a single-level decomposition.
To do a two-level decomposition, you can use the wavedec2
function with N=2
. To create the plot as shown, you have to take a closer look at the return values of wavedec2
:
(Image by Mathworks, from [2])
The vector C
contains all the approximation coefficients, stored in a column-wise fashion. S
is the so-called "book-keeping" matrix, as it contains information on how the data is stored.
Now the first image, cA2
in the example above, is the first 32*32 entries of C
. Using ii
and jj
as index variables, we can get the relevant parts of C
and use reshape
to get back to the image format:
ii = 1; jj = prod(S(1,:));
cA2 = reshape(C(ii:jj),S(1,:));
The other second-level coefficients are similarly obtained:
ii = jj+1; jj = ii + prod(S(2,:)) - 1;
cH2 = reshape(C(ii:jj),S(2,:));
ii = jj+1; jj = ii + prod(S(2,:)) - 1;
cV2 = reshape(C(ii:jj),S(2,:));
ii = jj+1; jj = ii + prod(S(2,:)) - 1;
cD2 = reshape(C(ii:jj),S(2,:));
The first-level coefficients can also be obtained in the same way, using the third row of S
:
ii = jj+1; jj = ii + prod(S(3,:)) - 1;
cH1 = reshape(C(ii:jj),S(3,:));
ii = jj+1; jj = ii + prod(S(3,:)) - 1;
cV1 = reshape(C(ii:jj),S(3,:));
ii = jj+1; jj = ii + prod(S(3,:)) - 1;
cD1 = reshape(C(ii:jj),S(3,:));
Now the plot can simply be created by arranging the images as desired:
imshow([[cA2,cH2; cV2,cD2],cH1;cV1,cD1],'Colormap',pink)
To add borders, you can use the rectangle
function and the information from S
:
% Small rectangles
rectangle('Position',[0,0,S(1,1),S(1,2)],'LineWidth',2,'EdgeColor','y');
rectangle('Position',[S(1,1),0,S(1,1),S(1,2)],'LineWidth',2,'EdgeColor','y');
rectangle('Position',[0,S(1,2),S(1,1),S(1,2)],'LineWidth',2,'EdgeColor','y');
rectangle('Position',[S(1,1),S(1,2),S(1,1),S(1,2)],'LineWidth',2,'EdgeColor','y');
% Large rectangles
rectangle('Position',[0,S(3,2),S(3,1),S(3,2)],'LineWidth',2,'EdgeColor','y');
rectangle('Position',[S(3,1),0,S(3,1),S(3,2)],'LineWidth',2,'EdgeColor','y');
rectangle('Position',[S(3,1),S(3,2),S(3,1),S(3,2)],'LineWidth',2,'EdgeColor','y');