## Issue

I am using MATLAB to apply the Discrete Wavelet Transform on an image. I am applying it several times (3) in order to get a 3 level transform. I am using the `dwt2`

function provided by MATLAB in order to compress and `idwt2`

to make the decompression. The problem is that I do not know how to decompress several times, as in apply `idwt2`

several times to the previous received output, as it returns a matrix. Take for example:

```
x = idwt2(scaled3, vertical3, horizontal3, diagonal3, Lo_R, Ho_R);
```

How should `idwt2`

be applied to x?

## Solution

Looking at the documentation for `dwt2`

and `idwt2`

, it appears that you have 2 general options for reconstructing your multiply-decomposed images:

- Store all of the horizontal, vertical, and diagonal detail coefficient matrices from each decomposition step and use them in the reconstruction.
- Enter an empty matrix (
`[]`

) for any detail coefficient matrices that you didn’t save from previous decomposition steps.

Since it was a slow day, here’s some code showing how to do this and what the results look like for each case…

First, load a sample image and initialize some variables:

```
load woman; % Load image data
nLevel = 3; % Number of decompositions
nColors = size(map, 1); % Number of colors in colormap
cA = cell(1, nLevel); % Approximation coefficients
cH = cell(1, nLevel); % Horizontal detail coefficients
cV = cell(1, nLevel); % Vertical detail coefficients
cD = cell(1, nLevel); % Diagonal detail coefficients
```

Now, apply the decompositions (in this case 3) and store the detail coefficient matrices from each step in a cell array:

```
startImage = X;
for iLevel = 1:nLevel,
[cA{iLevel}, cH{iLevel}, cV{iLevel}, cD{iLevel}] = dwt2(startImage, 'db1');
startImage = cA{iLevel};
end
```

To see what the final decomposed image looks like, along with all the detail coefficient matrices along the way, run the following code (which makes use of `wcodemat`

):

```
tiledImage = wcodemat(cA{nLevel}, nColors);
for iLevel = nLevel:-1:1,
tiledImage = [tiledImage wcodemat(cH{iLevel}, nColors); ...
wcodemat(cV{iLevel}, nColors) wcodemat(cD{iLevel}, nColors)];
end
figure;
imshow(tiledImage, map);
```

You should see something like this:

Now it’s time to reconstruct! The following code performs a “full” reconstruction (using *all* of the stored detail coefficient matrices) and a “partial” reconstruction (using *none* of them), then it plots the images:

```
fullRecon = cA{nLevel};
for iLevel = nLevel:-1:1,
fullRecon = idwt2(fullRecon, cH{iLevel}, cV{iLevel}, cD{iLevel}, 'db1');
end
partialRecon = cA{nLevel};
for iLevel = nLevel:-1:1,
partialRecon = idwt2(partialRecon, [], [], [], 'db1');
end
figure;
imshow([X fullRecon; partialRecon zeros(size(X))], map, ...
'InitialMagnification', 50);
```

Notice that the original (top left) and the “full” reconstruction (top right) look indistinguishable, but the “partial” reconstruction (lower left) is very pixelated. The difference wouldn’t be as severe if you applied fewer decomposition steps, like just 1 or 2.

Answered By – gnovice

Answer Checked By – Marilyn (AngularFixing Volunteer)