Main Content

Generate MATLAB Code for 2-D Stationary Wavelet Denoising

You can generate MATLAB® code to reproduce app-based 2-D stationary wavelet denoising at the command line. You can generate code to denoise both indexed and truecolor images. You must perform this operation in the SWT Denoising 2-D tool. You must first denoise your image before you can enable the File > Generate MATLAB Code (Denoising Process) operation.

2-D Stationary Wavelet Transform Denoising

  1. Enter waveletAnalyzer at the MATLAB command prompt.

  2. Select SWT Denoising 2-D.

  3. At the MATLAB command prompt, type

    load noiswom;
    In the SWT Denoising 2-D tool, select File > Import Image from Workspace. When the Import from Workspace dialog box appears, select the X variable. Click OK to import the image.

  4. Select the db4 wavelet, and set the Level to 5.

  5. Click Decompose Image.

  6. Use the default soft thresholding method with Fixed form threshold and Unscaled white noise for Select noise structure.

  7. Set the following thresholds for the horizontal, diagonal, and vertical details. Ensure that you set the thresholds for the three detail coefficient types.

    • Level 1 — 5

    • Level 2 — 4

    • Level 3 — 3

    • Level 4 — 2

    • Level 5 — 1

  8. Click Denoise.

  9. Select File > Generate MATLAB Code (Denoising Process).

    The operation generates the following MATLAB code.

    function [XDEN,wDEC] = func_denoise_sw2d(X)
    % FUNC_DENOISE_SW2D Saved Denoising Process.
    %   X: matrix of data
    %   -----------------
    %   XDEN: matrix of denoised data
    %   wDEC: stationary wavelet decomposition
    
    % Analysis parameters.
    %---------------------
    wname = 'db4';
    level = 5;
    
    % Denoising parameters.
    %-----------------------
    % meth = 'sqtwolog';
    % scal_OR_alfa = one;
    sorh = 's';    % Specified soft or hard thresholding
    % Order of thresholds down each column is H,D,V
    % Order in SWT2 output is H,V,D where each coefficient
    % matrix is repeated L times where L is the number of levels.
    thrSettings =  [...
        5.0000      4.0000      3.0000      2.0000      1.0000 ; ...
        5.0000      4.0000      3.0000      2.0000      1.0000 ; ...
        5.0000      4.0000      3.0000      2.0000      1.0000   ...
        ];
    roundFLAG = false;
    
    % Decompose using SWT2.
    %---------------------
    wDEC = swt2(X,level,wname);
    
    isRGB = ndims(wDEC) == 4 && size(wDEC,3) == 3;
    % Denoise
    permDir = [1 3 2];
    
    for j = 1:level
        for kk=1:3
            ind = (permDir(kk)-1)*level+j;
            thr = thrSettings(kk,j);
            if isRGB
                wDEC(:,:,:,ind) = wthresh(wDEC(:,:,:,ind),sorh,thr);
            else
                wDEC(:,:,ind) = wthresh(wDEC(:,:,ind),sorh,thr);
            end
        end
    end
    
    % Reconstruct the denoise signal using ISWT2.
    %-------------------------------------------
    XDEN = iswt2(wDEC,wname);
    if roundFLAG , XDEN = round(XDEN); end
    

  10. Save this MATLAB program as func_denoise_sw2d.m in a folder on the MATLAB search path.

    Execute the following code.

    load noiswom
    [XDEN,wDEC] = func_denoise_sw2d(X);

  11. Save the denoised image as denoisedwom.mat in a folder on the MATLAB search path.

  12. Execute the following code.

    load denoisedwom
    % Compare the GUI and command line results
    imagesc(X); title('GUI Operation'); colormap(gray);
    figure;
    imagesc(XDEN); title('Command Line Operation');
    colormap(gray);
    norm(XDEN-X,2)

Note

Thresholds are derived from a subset of the coefficients in the stationary wavelet decomposition. For more information, see Coefficient Selection.