Main Content

2-D Wavelet Compression

This section takes you through the features of wavelet 2-D true compression using the Wavelet Toolbox™ software.

For more information on the compression methods see Wavelet Compression for Images in the Wavelet Toolbox User's Guide.

For more information on the main function available when using command-line mode, see the wcompress reference pages.

Starting from a given image, the goal of the true compression is to minimize the length of the sequence of bits needed to represent it, while preserving information of acceptable quality. Wavelets contribute to effective solutions for this problem.

The complete chain of compression includes phases of quantization, coding and decoding in addition of the wavelet processing itself.

The purpose of this section is to show how to compress and uncompress a grayscale or truecolor image using various compression methods.

In this section, you'll learn to

  • Compress using global thresholding and Huffman encoding

  • Uncompress

  • Compress using progressive methods

  • Handle truecolor images

Compression by Global Thresholding and Huffman Encoding

First load and display the grayscale image mask.

load mask; 
image(X) 
axis square; 
colormap(pink(255)) 
title('Original Image: mask')

A synthetic performance of the compression is given by the compression ratio and the Bit-Per-Pixel ratio which are equivalent.

The compression ratio CR means that the compressed image is stored using only CR% of the initial storage size.

The Bit-Per-Pixel ratio BPP gives the number of bits used to store one pixel of the image.

For a grayscale image, the initial BPP is 8 while for a truecolor image the initial BPP is 24 because 8 bits are used to encode each of the three colors (RGB color space).

The challenge of compression methods is to find the best compromise between a weak compression ratio and a good perceptual result.

Let us begin with a simple method cascading global coefficients thresholding and Huffman encoding. We use the default wavelet bior4.4 and the default level which is the maximum possible level (see the wmaxlev function) divided by 2.

The desired Bit-Per-Pixel ratio BPP is set to 0.5 and the compressed image will be stored in the file named 'mask.wtc'.

meth = 'gbl_mmc_h'; % Method name 
option = 'c';         % 'c' stands for compression 
[CR,BPP] = wcompress(option,X,'mask.wtc',meth,'bpp',0.5)

CR =

  6.6925

BPP =

  0.5354

The achieved Bit-Per-Pixel ratio is actually about 0.53 (closed to the desired one) for a compression ratio of 6.7%.

Uncompression

Let us uncompress the image retrieved from the file 'mask.wtc' and compare it to the original image.

option = 'u';  % 'u' stands for uncompression 
Xc = wcompress(option,'mask.wtc');
colormap(pink(255)) 
subplot(1,2,1); image(X); 
axis square; 
title('Original Image') 
subplot(1,2,2); image(Xc); 
axis square; 
title('Compressed Image') 
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')], ...
        ['BPP: ' num2str(BPP,'%3.2f')]})

Compression by Progressive Methods

Let us now illustrate the use of progressive methods starting with the well known EZW algorithm using the Haar wavelet. The key parameter is the number of loops. Increasing it, leads to better recovery but worse compression ratio.

meth = 'ezw';     % Method name 
wname  = 'haar';  % Wavelet name 
nbloop = 6;       % Number of loops 
[CR,BPP] = wcompress('c',X,'mask.wtc',meth, ...
                     'maxloop',nbloop,'wname',wname); 
Xc = wcompress('u','mask.wtc');
colormap(pink(255)) 
subplot(1,2,1); image(X); 
axis square; 
title('Original Image') 
subplot(1,2,2); image(Xc); 
axis square; title('Compressed Image - 6 steps') 
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')], ...
        ['BPP: ' num2str(BPP,'%3.2f')]}) 

A too small number of steps (here 6) produces a very coarse compressed image. So let us examine a little better result for 9 steps and a satisfactory result for 12 steps.

[CR,BPP]= wcompress('c',X,'mask.wtc',meth,'maxloop',9, ...
                    'wname','haar'); 
Xc = wcompress('u','mask.wtc'); 
colormap(pink(255))
subplot(1,2,1); image(Xc); 
axis square; title('Compressed Image - 9 steps') 
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')],...
        ['BPP: ' num2str(BPP,'%3.2f')]})  

[CR,BPP] = wcompress('c',X,'mask.wtc',meth,'maxloop',12, ...
                     'wname','haar'); 
Xc = wcompress('u','mask.wtc');
subplot(1,2,2); image(Xc); 
axis square; 
title('Compressed Image - 12 steps') 
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')],...
        ['BPP: ' num2str(BPP,'%3.2f')]}) 

As can be seen, the reached BPP ratio is about 0.92 when using 12 steps.

Let us try to improve it by using the wavelet bior4.4 instead of haar and looking at obtained results for steps 12 and 11.

[CR,BPP] = wcompress('c',X,'mask.wtc','ezw','maxloop',12, ...
                     'wname','bior4.4'); 
Xc = wcompress('u','mask.wtc'); 
colormap(pink(255))
subplot(1,2,1); image(Xc); 
axis square; 
title('Compressed Image - 12 steps - bior4.4') 
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')], ...
        ['BPP: ' num2str(BPP,'%3.2f')]})
[CR,BPP] = wcompress('c',X,'mask.wtc','ezw','maxloop',11, ...
                     'wname','bior4.4'); 
Xc = wcompress('u','mask.wtc');
subplot(1,2,2); image(Xc); 
axis square; 
title('Compressed Image - 11 steps - bior4.4') 
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')], ...
        ['BPP: ' num2str(BPP,'%3.2f')]}) 

Starting from the eleventh loop, the result can be considered satisfactory. The reached BPP ratio is now about 0.35. It can even be slightly improved by using a more recent method: SPIHT (Set Partitioning In Hierarchical Trees).

[CR,BPP] = wcompress('c',X,'mask.wtc','spiht','maxloop',12, ...
                     'wname','bior4.4'); 
Xc = wcompress('u','mask.wtc');
colormap(pink(255)) 
subplot(1,2,1); image(X); 
axis square; 
title('Original Image') 
subplot(1,2,2); image(Xc); 
axis square; 
title('Compressed Image - 12 steps - bior4.4') 
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')], ...
        ['BPP: ' num2str(BPP,'%3.2f')]}) 
[psnr,mse,maxerr,l2rat] = measerr(X,Xc)

delete('mask.wtc') 

The final compression ratio (2.8%) and the Bit-Per-Pixel ratio (0.23) are very satisfactory. Let us recall that the first ratio means that the compressed image is stored using only 2.8% of the initial storage size.

Handling Truecolor Images

Finally, let us illustrate how to compress the wpeppers.jpg truecolor image. Truecolor images can be compressed along the same scheme as the grayscale images by applying the same strategies to each of the three color components.

The progressive compression method used is SPIHT (Set Partitioning In Hierarchical Trees) and the number of encoding loops is set to 12.

X = imread('wpeppers.jpg'); 
[CR,BPP] = wcompress('c',X,'wpeppers.wtc','spiht','maxloop',12);
Xc = wcompress('u','wpeppers.wtc'); 
subplot(1,2,1); image(X); 
axis square; 
title('Original Image') 
subplot(1,2,2); image(Xc); 
axis square;
title('Compressed Image - 12 steps - bior4.4') 
xlabel({['Compression Ratio: ' num2str(CR,'%1.2f %%')], ... 
        ['BPP: ' num2str(BPP,'%3.2f')]})
delete('wpeppers.wtc')

The compression ratio (1.65%) and the Bit-Per-Pixel ratio (0.4) are very satisfactory while maintaining a good visual perception.