This section takes you through the features of 2D discrete wavelet analysis using the Wavelet Toolbox™ software. The toolbox provides these functions for image analysis. For more information, see the function reference pages.
Note
In this section the presentation and examples use 2D arrays
corresponding to indexed image representations. However, the functions
described are also available when using truecolor images, which are
represented by m
byn
by3 arrays
of uint8
. For more information on image formats,
see Wavelets: Working with Images.
Function Name  Purpose 

Wavelet image denoising  
Provide default values for denoising and compression  
Penalized threshold for wavelet 1D or 2D denoising  
Thresholds for wavelet 2D using BirgéMassart strategy  
Wavelet denoising and compression  
Threshold settings manager 
In this section, you'll learn
How to load an image
How to analyze an image
How to perform singlelevel and multilevel image decompositions and reconstructions (command line only)
How to use Square and Tree mode features (GUI only)
How to zoom in on detail (GUI only)
How to compress an image
This example shows how you can use 2D wavelet analysis to compress an image efficiently without sacrificing its clarity.
Note: Instead of directly using image(I)
to visualize the image I
, we use image(wcodemat(I))
, which displays a rescaled version of I
leading to a clearer presentation of the details and approximations (see wcodemat
).
Load an image.
load wbarb whos X map
Name Size Bytes Class Attributes X 256x256 524288 double map 192x3 4608 double
Display the image.
image(X) colormap(map) colorbar
If the colormap is smooth, the wavelet transform can be directly applied to the indexed image; otherwise the indexed image should be converted to grayscale format. For more information, see Wavelets: Working with Images. Since the colormap is smooth in this image, you can now perform the decomposition.
Perform a singlelevel wavelet decomposition of the image using the bior3.7
wavelet. The coefficient matrix cA1
are the approximation coefficients. The horizontal, vertical, and diagonal details are in the matrices cH1
, cV1
, and cD1
, respectively.
wv = 'bior3.7';
[cA1,cH1,cV1,cD1] = dwt2(X,wv);
Use idwt1
to construct the approximations and details from the coefficients. (Note: You can also use upcoef2
.
sx = size(X); A1 = idwt2(cA1,[],[],[],wv,sx); H1 = idwt2([],cH1,[],[],wv,sx); V1 = idwt2([],[],cV1,[],wv,sx); D1 = idwt2([],[],[],cD1,wv,sx);
Display the approximations and details.
figure subplot(2,2,1) image(wcodemat(A1,192)) title('Approximation A1') subplot(2,2,2) image(wcodemat(H1,192)) title('Horizontal Detail H1') subplot(2,2,3) image(wcodemat(V1,192)) title('Vertical Detail V1') subplot(2,2,4) image(wcodemat(D1,192)) title('Diagonal Detail D1') colormap(map)
Regenerate the image by the singlelevel inverse discrete wavelet transform. Confirm the difference between the regenerated and original images are small.
Xrec = idwt2(cA1,cH1,cV1,cD1,wv); max(abs(X(:)Xrec(:)))
ans = 1.4211e13
Perform a level2 wavelet decomposition of the image using the same bior3.7
wavelet. The coefficients of all the components of a secondlevel decomposition (that is, the secondlevel approximation and the first two levels of detail) are returned concatenated into one vector, C
. Argument S
is a bookkeeping matrix that keeps track of the sizes of each component.
[c,s] = wavedec2(X,2,wv);
Extract the level 2 approximation coefficients. Extract the first and secondlevel detail coefficients.
cA2 = appcoef2(c,s,wv,2); [cH2,cV2,cD2] = detcoef2('all',c,s,2); [cH1,cV1,cD1] = detcoef2('all',c,s,1);
Reconstruct the level 2 approximation and the level 1 and level 2 details.
A2 = wrcoef2('a',c,s,wv,2); H1 = wrcoef2('h',c,s,wv,1); V1 = wrcoef2('v',c,s,wv,1); D1 = wrcoef2('d',c,s,wv,1); H2 = wrcoef2('h',c,s,wv,2); V2 = wrcoef2('v',c,s,wv,2); D2 = wrcoef2('d',c,s,wv,2);
Display the approximation and details.
figure subplot(2,4,1) image(wcodemat(A1,192)) title('Approximation A1') subplot(2,4,2) image(wcodemat(H1,192)) title('Horizontal Detail H1') subplot(2,4,3) image(wcodemat(V1,192)) title('Vertical Detail V1') subplot(2,4,4) image(wcodemat(D1,192)) title('Diagonal Detail D1') subplot(2,4,5) image(wcodemat(A2,192)) title('Approximation A2') subplot(2,4,6) image(wcodemat(H2,192)) title('Horizontal Detail H2') subplot(2,4,7) image(wcodemat(V2,192)) title('Vertical Detail V2') subplot(2,4,8) image(wcodemat(D2,192)) title('Diagonal Detail D2') colormap(map)
Compress the image. Use ddencmp
to calculate the default parameters and wdencmp
to perform the actual compression.
[thr,sorh,keepapp] = ddencmp('cmp','wv',X); [Xcomp,CXC,LXC,PERF0,PERFL2] = ... wdencmp('gbl',c,s,wv,2,thr,sorh,keepapp);
Compare the compressed image with the original image.
fprintf('Percentage of wavelet coefficients set to zero: %.4f\nPercentage of energy preserved: %.4f\n',... PERF0,PERFL2);
Percentage of wavelet coefficients set to zero: 49.8011 Percentage of energy preserved: 99.9817
figure subplot(121) image(X) title('Original Image') axis square subplot(122) image(Xcomp) title('Compressed Image') axis square colormap(map)
Note that, even though the compressed image is constructed from only about half as many nonzero wavelet coefficients as the original, there is almost no detectable deterioration in the image quality.
In this section we explore the same image as in the previous section, but we use the Wavelet Analyzer app to analyze the image.
Start the 2D Wavelet Analysis Tool.
From the MATLAB^{®} prompt, type waveletAnalyzer
.
The Wavelet Tool Main Menu appears.
Click the Wavelet 2D menu item. The discrete wavelet analysis tool for 2D image data appears.
Load an image.
At the MATLAB command prompt, type
load wbarb
X
variable. Click OK to import the
image.The image is loaded into the Wavelet 2D tool.
Analyze the image.
Using the Wavelet and Level menus located to the upper right, determine the wavelet family, the wavelet type, and the number of levels to be used for the analysis.
For this analysis, select the bior3.7
wavelet
at level 2.
Click the Analyze button. After a pause for computation, the Wavelet 2D tool displays its analysis.
Using Square Mode Features
By default, the analysis appears in “Square Mode.” This mode includes four different displays. In the upper left is the original image. Below that is the image reconstructed from the various approximations and details. To the lower right is a decomposition showing the coarsest approximation coefficients and all the horizontal, diagonal, and vertical detail coefficients. Finally, the visualization space at the top right displays any component of the analysis that you want to look at more closely.
Click on any decomposition component in the lower right window.
A blue border highlights the selected component. At the lower right of the Wavelet 2D window, there is a set of three buttons labeled “Operations on selected image.” Note that if you click again on the same component, you'll deselect it and the blue border disappears.
Click the Visualize button.
The selected image is displayed in the visualization area. You are seeing the raw, unreconstructed 2D wavelet coefficients. Using the other buttons, you can display the reconstructed version of the selected image component, or you can view the selected component at full screen resolution.
Using Tree Mode Features
Choose Tree from the View Mode menu.
Your display changes to reveal the following.
This is the same information shown in square mode, with in addition all the approximation coefficients, but arranged to emphasize the tree structure of the decomposition. The various buttons and menus work just the same as they do in square mode.
Zooming in on Detail
Drag a rubber band box (by holding down the left mouse button) over the portion of the image you want to magnify.
Click the XY+ button (located at the bottom of the screen) to zoom horizontally and vertically.
The Wavelet 2D tool enlarges the displayed images.
To zoom back to original magnification, click the History << button.
Click the Compress button, located to the upper right of the Wavelet 2D window. The Wavelet 2D Compression window appears.
The tool automatically selects thresholding levels to provide a good initial balance between retaining the image's energy while minimizing the number of coefficients needed to represent the image.
However, you can also adjust thresholds manually using the By Level thresholding option, and then the sliders or edits corresponding to each level.
For this example, select the By Level thresholding option and select the Remove near 0 method from the Select thresholding method menu.
The following window is displayed.
Select from the direction menu whether you want to adjust thresholds for horizontal, diagonal or vertical details. To make the actual adjustments for each level, use the sliders or use the left mouse button to directly drag the dashed lines.
To compress the original image, click the Compress button. After a pause for computation, the compressed image is displayed beside the original. Notice that compression eliminates almost half the coefficients, yet no detectable deterioration of the image appears.
Show the residuals.
From the Wavelet 2D Compression tool, click the Residuals button. The More on Residuals for Wavelet 2D Compression window appears.
Displayed statistics include measures of tendency (mean, mode, median) and dispersion (range, standard deviation). In addition, the tool provides frequencydistribution diagrams (histograms and cumulative histograms). The same tool exists for the Wavelet 2D Denoising tool.
Note
The statistics displayed in the above figure are related to the displayed image but not to the original one. Usually this information is the same, but in some cases, edge effects may cause the original image to be cropped slightly. To see the exact statistics, use the command line functions to get the desired image and then apply the desired MATLAB statistical function(s).
The Wavelet 2D graphical tool lets you import information from and export information to disk, if you adhere to the proper file formats.
You can save synthesized images, coefficients, and decompositions from the Wavelet 2D tool to disk, where the information can be manipulated and later reimported into the graphical tool.
Saving Synthesized Images. You can process an image in the Wavelet
2D tool, and then save the processed image to a MATfile
(with extension mat
or other).
For example, load the example analysis:
File > Example Analysis > Indexed Images > at level 3, with sym4 → Detail Durer
and perform a compression on the original image. When you close the Wavelet 2D Compression window, update the synthesized image by clicking Yes in the dialog box that appears.
Then, from the Wavelet 2D tool,
select the File > Save > Synthesized Image menu
option. A dialog box appears allowing you to select a folder and filename
for the MATfile (with extension mat
or other).
For this example, choose the name symage
.
To load the image into your workspace, type
load symage whos
Name  Size  Bytes  Class 

X  359x371  1065512  double array 
map  64x3  1536  double array 
valTHR  1x1  8  double array 
wname  1x4  8  char array 
The synthesized image is given by X
and map
contains
the colormap. In addition, the parameters of the denoising or compression
process are given by the wavelet name (wname
) and
the global threshold (valTHR
).
Saving Discrete Wavelet Transform Coefficients. The Wavelet 2D tool lets you save the coefficients of a discrete wavelet transform (DWT) to disk. The toolbox creates a MATfile in the current folder with a name you choose.
To save the DWT coefficients from the present analysis, use the menu option File > Save > Coefficients.
A dialog box appears that lets you specify a folder and filename for storing the coefficients.
Consider the example analysis:
File > Example Analysis > Indexed Images > at level 3, with sym4 → Detail Durer
After saving the discrete wavelet coefficients to the file cfsdurer.mat
,
load the variables into your workspace:
load cfsdurer whos
Name  Size  Bytes  Class 

coefs  1x142299  1138392  double array 
map  64x3  1536  double array 
sizes  5x2  80  double array 
valTHR  0x0  0  double array 
wname  1x4  8  char array 
Variable map
contains the colormap. Variable wname
contains
the wavelet name and valTHR
is empty since the
synthesized image is the same as the original one.
Variables coefs
and sizes
contain
the discrete wavelet coefficients and the associated matrix sizes.
More precisely, in the above example, coefs
is
a 1by142299 vector of concatenated coefficients, and sizes
gives
the length of each component.
Saving Decompositions. The Wavelet 2D tool lets you
save the entire set of data from a discrete wavelet analysis to disk.
The toolbox creates a MATfile in the current folder with a name you
choose, followed by the extension wa2
(wavelet
analysis 2D).
Open the Wavelet 2D tool and load the example analysis:
File > Example Analysis > Indexed Images > at level 3, with sym4 → Detail Durer.
To save the data from this analysis, use the menu option File > Save > Decomposition.
A dialog box appears that lets you specify a folder and filename
for storing the decomposition data. Type the name decdurer
.
After saving the decomposition data to the file decdurer.wa2
,
load the variables into your workspace:
load decdurer.wa2 mat whos
Name  Size  Bytes  Class 

coefs  1x142299  1138392  double array 
data_name  1x6  12  char array 
map  64x3  1536  double array 
sizes  5x2  80  double array 
valTHR  0x0  0  double array 
wave_name  1x4  8  char array 
Variables coefs
and sizes
contain
the wavelet decomposition structure. Other variables contain the wavelet
name, the colormap, and the filename containing the data. Variable valTHR
is
empty since the synthesized image is the same as the original one.
Note
Save options are also available when performing denoising or
compression inside the Wavelet 2D tool.
In the Wavelet 2D Denoising window,
you can save denoised image and decomposition. The same holds true
for the Wavelet 2D Compression window.
This way, you can save many different trials from inside the Denoising
and Compression windows without going back to the main Wavelet 2D window during a finetuning process.
When saving a synthesized signal, a decomposition or coefficients
to a MATfile, the mat
file extension is not necessary.
You can save approximations individually for each level or save them
all at once.
You can load images, coefficients, or decompositions into the Wavelet Analyzer app. The information you load may have been previously exported from the Wavelet Analyzer app, and then manipulated in the workspace; or it may have been information you generated initially from the command line.
In either case, you must observe the strict file formats and data structures used by the Wavelet 2D tool, or else errors will result when you try to load information.
Loading Images. This toolbox supports only indexed images.
An indexed image is a matrix containing only integers from 1 to n
,
where n
is the number of colors in the image.
This image may optionally be accompanied by an n
by3
matrix called map
. This is the colormap associated
with the image. When MATLAB displays such an image, it uses the
values of the matrix to look up the desired color in this colormap.
If the colormap is not given, the Wavelet 2D tool
uses a monotonic colormap with max(max(X))min(min(X))+1
colors.
To load an image you've constructed in your MATLAB workspace
into the Wavelet 2D tool, save the
image (and optionally, the variable map
) in a MATfile
(with extension mat
or other).
For instance, suppose you've created an image called brain and want to analyze it in the Wavelet 2D tool. Type
X = brain; map = pink(256); save myfile X map
To load this image into the Wavelet 2D tool, use the menu option File > Load > Image.
A dialog box appears that lets you select the appropriate MATfile to be loaded.
Note
The graphical tools allow you to load an image that does not contain integers from 1 to n. The computations are correct because they act directly on the matrix, but the display of the image is strange. The values less than 1 are evaluated as 1, the values greater than n are evaluated as n, and a real value within the interval [1,n] is evaluated as the closest integer.
The coefficients, approximations, and details produced by wavelet decomposition are not indexed image matrices.
To display these images in a suitable way, the Wavelet 2D tool follows these rules:
Reconstructed approximations are displayed using the
colormap map
.
The coefficients and the reconstructed details are
displayed using the colormap map
applied to a rescaled
version of the matrices.
Note
The first 2D variable encountered in the file (except the variable map
,
which is reserved for the colormap) is considered the image. Variables
are inspected in alphabetical order.
Loading Discrete Wavelet Transform Coefficients. To load discrete wavelet transform (DWT) coefficients into the Wavelet 2D tool, first save the appropriate data in a MATfile, which must contain at least the two variables:
coefs
, the coefficients vector
sizes
, the bookkeeping matrix
For an indexed image the matrix sizes
is
an n+2
by2 array:
For a truecolor image, the matrix sizes
is
a n+2
by3:
Variable coefs
must be a vector of concatenated
DWT coefficients. The coefs
vector for an n
level
decomposition contains 3n+1
sections, consisting
of the leveln
approximation coefficients, followed
by the horizontal, vertical, and diagonal detail coefficients, in
that order, for each level. Variable sizes
is a
matrix, the rows of which specify the size of cA_{n}
,
the size of cH_{n}
(or cV_{n}
,
or cD_{n}
),..., the size of cH_{1}
(or cV_{1}
,
or cD_{1}
), and the size of
the original image X
. The sizes of vertical and
diagonal details are the same as the horizontal detail.
After constructing or editing the appropriate data in your workspace, type
save myfile coefs sizes
Use the File > Load > Coefficients menu option from the Wavelet 2D tool to load the data into the graphical tool.
A dialog box appears, allowing you to choose the folder and file in which your data reside.
Loading Decompositions. To load discrete wavelet transform decomposition data into the Wavelet 2D tool, you must first save the appropriate
data in a MATfile (with extension wa2
or other).
The MATfile contains these variables.
Variable  Status  Description 

coefs  Required  Vector of concatenated DWT coefficients 
sizes  Required  Matrix specifying sizes of components of 
wave_name  Required  Character vector specifying name of wavelet used for
decomposition (e.g., 
map  Optional 

data_name  Optional  Character vector specifying name of decomposition 
After constructing or editing the appropriate data in your workspace, type
save myfile.wa2 coefs sizes wave_name
Use the File > Load > Decomposition menu option from the Wavelet 2D tool to load the image decomposition data.
A dialog box appears, allowing you to choose the folder and file in which your data reside.
Note
When loading an image, a decomposition, or coefficients from
a MATfile, the extension of this file is free. The mat
extension
is not necessary.