Reputation: 8333
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?
Upvotes: 3
Views: 5967
Reputation: 1
% Multi-level reconstruction from DWT coefficients
% The variable "coefs" is what you get when you perform forward dwt2()
% on the image you're decomposing. It is a long row
% vector that has cA- approximation details, cH -horizontal details, cV-
% vertical details, cD-diagonal details
L=3; % For db 3-level reconstruction for example
k=size(image,1)/2^L; % I am assuming a square sized image where both
% dimensions are equal
for level=0:(L-1)
s=k*2^level;
if level==0
cA=reshape(coefs(1,1:s^2),s,s);
figure;imshow(cA,[])
end
cH=reshape(coefs(1,(s^2+1):2*s^2),s,s);
figure;imshow(cH,[])
cV=reshape(coefs(1,(2*s^2+1):3*s^2),s,s);
figure;imshow(cV,[])
cD=reshape(coefs(1,(3*s^2+1):4*s^2),s,s);
figure;imshow(cD,[])
I_rec=idwt2(cA,cH,cV,cD,"db1");
figure;imshow(I_rec,[])
cA=I_rec; % The recosntructed image is the approximation detail-cA
% for next levels of reconstruction
end
Upvotes: 0
Reputation: 125854
Looking at the documentation for dwt2
and idwt2
, it appears that you have 2 general options for reconstructing your multiply-decomposed images:
[]
) 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.
Upvotes: 11