Wavelet Transform for N Dimensions

I stumbled upon this amazing answer several times using MATLAB idwt2 , which I completed to figure it out myself. However, I cannot figure out how to use the same thing with working with an RGB image. So, I have 3 questions.

  • How will the code be applied to the RGB image with the converted image displayed on the output, along with the high and low frequency components along the row and column, is it possible to view the merging of all components as one image? I know that I need to put on a cat operator, but I can’t figure out how to do this.

  • Secondly, I also get an amazed look! I am perplexed because I cannot follow the reason. I also added the same code with instructions showing how this image was generated.

    3. What does the expression db1 mean in the signature of the dwt function?

CODE:

  load woman; % Load image data %startImage=imread('pic_rgb.jpg'); % IF I WANT TO WORK WITH RGB IMAGE 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 startImage = X; for iLevel = 1:nLevel, [cA{iLevel},cH{iLevel},cV{iLevel},cD{iLevel}] = dwt2(startImage,'db1'); startImage = cA{iLevel}; end figure;colormap(map); imagesc(dwt2(startImage,'db1')); %THIS GIVES THE MAZED IMAGE INSTEAD OF THE TRANSFORMED IMAGE figure; 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); %reconstruct 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); 
+7
source share
1 answer

The sample image used in my answer to this other question was an indexed image , so you need to make a few changes to make this code work for an RGB image .

First, I will consider the issue of the 'db1' argument passed to DWT2 . This indicates the type of wavelet that will be used for decomposition (in this case, the Daubechies wavelet ). For more information on the available bursts, see the documentation for the WFILTERS and WAVEINFO functions .

I will cover your first two questions, showing you how to change the code from my other answer to working with an RGB image. I will use the sample image 'peppers.png' . First you want to upload your image and determine the number of values ​​that each color component has. Since the sample image is an unsigned 8-bit integer type (the most common situation), nColors will be 256:

 X = imread('peppers.png'); %# Load sample image nColors = 256; %# Number of values per color component 

If your images are large unsigned integer types (for example, 'uint16' ), the general way to find the number of color values ​​is to use the INTMAX function:

 nColors = double(intmax(class(X)))+1; 

The following code assumes an image type of 'uint8' .

The use of decompositions does not differ from the case with the indexed image. The matrices of coefficients are simply M-by-N-by-3 matrices instead of the M-by-N matrices:

 nLevel = 3; %# Number of decompositions cA = cell(1,nLevel); %# Approximation coefficient storage cH = cell(1,nLevel); %# Horizontal detail coefficient storage cV = cell(1,nLevel); %# Vertical detail coefficient storage cD = cell(1,nLevel); %# Diagonal detail coefficient storage startImage = X; for iLevel = 1:nLevel, %# Apply nLevel decompositions [cA{iLevel},cH{iLevel},cV{iLevel},cD{iLevel}] = dwt2(startImage,'db1'); startImage = cA{iLevel}; end 

The code for creating a tiled image to display horizontal, vertical and diagonal components for each decomposition will change due to the fact that we are now working with 3-D matrices and should use CAT instead of the concatenation operator [] :

 tiledImage = wcodemat(cA{nLevel},nColors); for iLevel = nLevel:-1:1 tiledImage = cat(1,cat(2,tiledImage,... wcodemat(cH{iLevel},nColors)),... cat(2,wcodemat(cV{iLevel},nColors),... wcodemat(cD{iLevel},nColors))); end figure; imshow(uint8(tiledImage-1)); %# Convert to unsigned 8-bit integer to display 

This will produce the following image showing the horizontal (top right), vertical (bottom left) and diagonal (bottom right) components for each decomposition step along with a thumbnail (top left):

enter image description here

Recovery steps are not changed compared to the other answer. Only the code needs to be changed to display the final 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; tiledImage = cat(1,cat(2,X,uint8(fullRecon)),... cat(2,uint8(partialRecon),zeros(size(X),'uint8'))); imshow(tiledImage,'InitialMagnification',50); 

And you get an image showing the original RGB image (top left), a fully restored image using all matrices of the stored part matrices (top right) and a partially restored image using none of the saved matrix detail coefficient (bottom left):

enter image description here

+9
source

All Articles