Main Content

Generate MATLAB Code for 2-D Decimated Wavelet Denoising and Compression

2-D Decimated Discrete Wavelet Transform Denoising

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

  1. Enter waveletAnalyzer at the MATLAB command prompt.

  2. Select Wavelet 2-D.

  3. Load the Noisy SinSin example indexed image. Using the default biorthogonal wavelet and level 3 decomposition, click Denoise.

  4. In the Select thresholding method drop-down menu, select the default Fixed form threshold and soft options. Use the default Unscaled white noise. Set the thresholds by level for the horizontal, diagonal, and vertical coefficients as follows:

    • Level 3 — 4

    • Level 2 — 4

    • Level 1 — 8

    Enter these thresholds for the horizontal, diagonal, and vertical coefficients.

  5. Select Denoise.

  6. Generate the MATLAB code with File > Generate MATLAB Code (Denoising Process).

    The operation generates the following MATLAB code.

    function [XDEN,cfsDEN,dimCFS] = func_denoise_dw2d(X)
    % FUNC_DENOISE_DW2-D Saved Denoising Process.
    %   X: matrix of data
    %   -----------------
    %   XDEN: matrix of denoised data
    %   cfsDEN: decomposition vector (see WAVEDEC2)
    %   dimCFS: corresponding bookkeeping matrix
    
    
    % Analysis parameters.
    %---------------------
    wname = 'bior6.8';
    level = 3;
    
    % Denoising parameters.
    %-----------------------
    % meth = 'sqtwolog';
    % scal_OR_alfa = one;
    sorh = 's';    % Specified soft or hard thresholding
    thrParams =  [...
        8.00000000     4.00000000     4.00000000 ; ...
        8.00000000     4.00000000     4.00000000 ; ...
        8.00000000     4.00000000     4.00000000   ...
        ];
    roundFLAG = true;
    
    % Denoise using CMDDENOISE.
    %--------------------------
    [coefs,sizes] = wavedec2(X,level,wname);
    [XDEN,cfsDEN,dimCFS] = wdencmp('lvd',coefs,sizes, ...
        wname,level,thrParams,sorh);
    
    if roundFLAG , XDEN = round(XDEN); end
    if isequal(class(X),'uint8') , XDEN = uint8(XDEN); end
    
  7. Save func_denoise_dw2d.m in a folder on the MATLAB search path, and execute the following code.

    load noissi2d.mat;
    noissi2d = X;
    [XDEN,cfsDEN,dimCFS] = func_denoise_dw2d(noissi2d);
  8. Save your denoised image in a folder on the MATLAB search path as denoisedsin.mat.

    Load the denoised image in the MATLAB workspace. Compare the result with your generated code.

    load denoisedsin.mat;
    % denoised image loaded in variable X
    subplot(121);
    imagesc(X); title('Image denoised in the GUI');
    subplot(122);
    imagesc(XDEN); title('Image denoised with generated code');
    % Norm of the difference is zero
    norm(XDEN-X,2)

2-D Decimated Discrete Wavelet Transform Compression

You can generate MATLAB code to reproduce app-based 2-D decimated wavelet compression at the command line. You must perform this operation in the Wavelet 2-D --Compression tool. You must first compress your image before you can enable the File > Generate MATLAB Code (Compression Process) operation.

  1. Enter waveletAnalyzer at the MATLAB command prompt.

  2. Select Wavelet 2-D.

  3. At the MATLAB command prompt, type

    load detfingr
    In the Wavelet 2-D tool, select File > Import from Workspace > Load Image. When the Import from Workspace dialog box appears, select the X variable. Click OK to import the data.

  4. Select the bior3.5 wavelet, and set Level to 3.

  5. Click Analyze, then click Compress.

  6. Using the default Global thresholding, set Select thresholding method to Bal.sparsity-norm (sqrt).

  7. Click Compress.

  8. File > Generate Code (Compression Process) generates the following code.

    function [XCMP,cfsCMP,dimCFS] = func_compress_dw2d(X)
    % FUNC_COMPRESS_DW2D Saved Compression Process.
    %   X: matrix of data
    %   -----------------
    %   XCMP: matrix of compressed data
    %   cfsCMP: decomposition vector (see WAVEDEC2)
    %   dimCFS: corresponding bookkeeping matrix
    
    % Analysis parameters.
    %---------------------
    wname = 'bior3.5';
    level = 3;
    
    % Compression parameters.
    %------------------------
    % meth = 'sqrtbal_sn';
    sorh = 'h';    % Specified soft or hard thresholding
    thrSettings = 10.064453124999996;
    roundFLAG = true;
    
    % Compression using WDENCMP.
    %--------------------------
    [coefs,sizes] = wavedec2(X,level,wname);
    [XCMP,cfsCMP,dimCFS] = wdencmp('gbl',coefs,sizes, ...
        wname,level,thrSettings,sorh,1);
    if roundFLAG , XCMP = round(XCMP); end
    if isequal(class(X),'uint8') , XCMP = uint8(XCMP); end
  9. Save the MATLAB program, func_compress_dw2d.m, in a folder on the MATLAB search path. Execute the following code at the command line.

    load detfingr.mat;
    % Image data is in X
    [XCMP,cfsCMP,dimCFS] = func_compress_dw2d(X);

  10. Save the compressed image from the Wavelet 2-D - - Compression tool in a folder on the MATLAB search path. Use File > Save > Compressed Image, and name the file compressed_fingerprint.mat. Execute the following code.

    load compressed_fingerprint.mat;
    % Image data is in X
    norm(XCMP-X,2)