I am trying to use the functions provided in the Matlab Wavelet Toolbox to create a multi-level discrete wavelet decomposition of an image, extracting the coefficients, manipulating them, and recomposing them back into the image.
I tried using a number of functions but none of them seem to do what I need. These are the steps to do this.
Use wavedec2 to decompose the image into [C,S].
[C,S] = wavedec2(X,N,Lo_D,Hi_D)
I then must use detcoef2 to extract the detail coefficients from [C,S]. [C,S] is the 'wavelet decomposition structure', it does not represent the actual coefficients such as cD, cH, cV.
[H,V,D] = detcoef2('all',C,S,N)
Manipulate the data
Reconstruct [C,S] ???? no function does this.
Use waverec2 to recompose the original image.
X = waverec2(C,S,Lo_R,Hi_R)
The problem is with step 4. There is no function that recreates the [C,S] and I can't call the function waverec2 because it needs the manipulated version of C and S.
Do I not need wavedec2 and waverec2? Perhaps should I just use detcoef2 and upcoef2?
Someone with some experience with DWT could solve this in a minute, I am fairly new to it.
Thanks
I'm curious as to why you can't use dwt2
for computing the 2D DWT of images. What you have there is a lot more work than what you should be doing. dwt2
is much more suitable to do what you want. You'd call dwt2
like so:
[LL,LH,HL,HH] = dwt2(X,Lo_D,Hi_D);
X
is your image, and Lo_D
and Hi_D
are your low-pass and high-pass filters you want to apply to the image. LL
is the low-passed version of the image, where the horizontal and vertical directions are low-passed, LH
is where the vertical direction is low-passed and the horizontal direction is high-passed, HL
is the vertical direction is high-passed and the horizontal direction is low-passed, and HH
is where both directions are high-passed. As such LH
, HL
and HH
are the detail coefficients while LL
contains the structure.
You can also specify the filter you want with a string as the second parameter:
[LL,LH,HL,HH] = dwt2(X,'wname');
'wname'
is a string that specifies what filter you want. You can type in help wfilters
to see what filters are available.
For example, by doing using cameraman.tif
from MATLAB's system path, we can do a one level 2D DWT (using the Haar wavelet) and show all of the components like so:
im = imread('cameraman.tif');
[LL, LH, HL, HH] = dwt2(im2double(im), 'haar');
imshow([LL LH; HL HH], []);
I use im2double
to convert the image to double precision to ensure accuracy. We get this image:
Note that the image is subsampled by 2 in order to produce the decompositions of LL, LH, HL
and HH
.
Once you have these components, you can certainly manipulate them to your heart's content. Once you manipulate them, you can simply use idwt2
like so:
Y = idwt2(LL,LH,HL,HH,Lo_R,Hi_R); %//or
Y = idwt2(LL,LH,HL,HH,'wname');
The four components are assumed to be double
, and so you can convert the images back to whatever type that was representing the image. Assuming your image was uint8
, you can do: Y = im2uint8(Y);
to convert back.
This should hopefully be what you're looking for!