Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wavelet Transform for N dimensions

I came across this amazing response Applying MATLAB's idwt2 several times which I executed to understand it myself. However, I am unable to get how to use the same with work with an RGB image. So, I have 3 Questions.

  1. How would the code be applied to an RGB image with only the transformed image displayed in the output that is along with the high and low frequency components along row and column,is it possible to view the fusion of all the components as a single image? I am aware that I have to put cat operator, but I cant understand how to go about it.

  2. Secondly, I am also getting a mazed image! I am perplexed since I cannot seem to follow the reason. I have also attached the same code with the statement showing how this image has been generated.

    3.What does the term db1 in the function signature of dwt imply?

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);
like image 545
Ria George Avatar asked Jul 20 '11 17:07

Ria George


1 Answers

The sample image used in my answer to that other question was an indexed image, so there are a few changes that need to be made to get that code working for an RGB image.

I'll first address your question about the 'db1' argument passed to DWT2. This specifies the type of wavelet to use for the decomposition (in this case, a Daubechies wavelet). More information about available wavelets can be found in the documentation for the functions WFILTERS and WAVEINFO.

I'll address your first two questions by showing you how to modify the code from my other answer to work for an RGB image. I'll use the sample 'peppers.png' image. You'll first want to load your image and define the number of values 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 larger unsigned integer types (e.g. 'uint16'), a general way to find the number of color values is to use the function INTMAX like so:

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

For the ensuing code, an image type of 'uint8' is assumed.

Applying the decompositions is no different than in the indexed image case. The coefficient matrices will simply be M-by-N-by-3 matrices instead of 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 to create the tiled image to show the horizontal, vertical, and diagonal components for each decomposition will change due to the fact that we are now working with 3-D matrices and must use the CAT function 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 give the following image showing the horizontal (top right), vertical (bottom left), and diagonal (bottom right) components for each decomposition step, along with the reduced image (top left):

enter image description here

The reconstruction steps are unchanged from the other answer. Only the code for displaying the final images needs to be modified:

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 will get an image showing the original RGB image (top left), the fully-reconstructed image using all of the stored detail coefficient matrices (top right), and the partially-reconstructed image using none of the stored detail coefficient matrices (bottom left):

enter image description here

like image 167
gnovice Avatar answered Nov 19 '22 06:11

gnovice