N维的小波变换

时间:2011-07-20 17:50:57

标签: matlab image-processing wavelet

我偶然发现了这个惊人的回应Applying MATLAB's idwt2 several times,我自己去了解它。但是,我无法使用与RGB图像一起使用相同的方法。所以,我有3个问题。

  1. 如何将代码应用于RGB图像,只有输出中显示的变换图像以及沿行和列的高频和低频分量,是否可以查看所有的融合组件作为单个图像?我知道我必须把猫操作员,但我不明白如何去做。

  2. 其次,我也得到一个迷宫图像!我很困惑,因为我似乎无法遵循原因。我还在声明如何生成此图像的语句中添加了相同的代码。

    3. db1的函数签名中的术语dwt意味着什么?

  3. 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);
    

1 个答案:

答案 0 :(得分:9)

my answer to that other question中使用的示例图片为indexed image,因此需要进行一些更改才能使该代码适用于RGB image

我将首先解答有关传递给DWT2'db1'参数的问题。这指定了用于分解的小波类型(在本例中为Daubechies wavelet)。有关可用小波的更多信息,请参阅函数WFILTERSWAVEINFO的文档。

我将通过向您展示如何修改我的其他答案中的代码以解决RGB图像问题来解决前两个问题。我将使用示例'peppers.png'图片。您首先要加载图像并定义每个颜色分量所具有的值的数量。由于样本图像是无符号8位整数类型(最常见的情况),nColors将为256:

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

如果您的图片是较大的无符号整数类型(例如'uint16'),找到颜色值数量的一般方法是使用函数INTMAX,如下所示:

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

对于随后的代码,假设图像类型为'uint8'

应用分解与索引图像的情况没有什么不同。系数矩阵将简单地是M×by-by-3矩阵而不是M-by-N矩阵:

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

创建平铺图像以显示每个分解的水平,垂直和对角线组件的代码将会发生变化,因为我们现在正在使用3-D矩阵并且必须使用CAT函数连接运算符[]

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

这将给出下图显示每个分解步骤的水平(右上),垂直(左下)和对角线(右下)组件以及缩小图像(左上角):

enter image description here

重建步骤与其他答案没有变化。只需要修改显示最终图像的代码:

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

您将获得显示原始RGB图像(左上)的图像,使用所有存储的细节系数矩阵(右上)的完全重建图像,以及使用没有存储的细节系数的部分重建图像矩阵(左下):

enter image description here