Wavelet Transform for N dimensions

狂风中的少年 提交于 2019-12-03 15:18:33
gnovice

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):

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):

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!