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 
Function Name  Purpose 

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
In this example we'll show how you can use 2D wavelet analysis to compress an image efficiently without sacrificing its clarity.
Note
Instead of directly using 
Load an image.
From the MATLAB^{®} prompt, type
load wbarb; whos
Name  Size  Bytes  Class 

X  256x256  524288  double array 
map  192x3  4608  double array 
Display the image. Type
image(X); colormap(map); colorbar;
Convert an indexed image to a grayscale image.
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.
To perform a singlelevel decomposition of the image using the bior3.7
wavelet,
type
[cA1,cH1,cV1,cD1] = dwt2(X,'bior3.7');
This generates the coefficient matrices of the levelone approximation
(cA1
) and horizontal, vertical and diagonal details
(cH1
,cV1
,cD1
,
respectively).
Construct and display approximations and details from the coefficients.
To construct the levelone approximation and details (A_{1}
, H_{1}
, V_{1}
,
and D_{1}
) from the coefficients cA_{1}
, cH_{1}
, cV_{1}
,
and cD_{1}
, type
A1 = upcoef2('a',cA1,'bior3.7',1); H1 = upcoef2('h',cH1,'bior3.7',1); V1 = upcoef2('v',cV1,'bior3.7',1); D1 = upcoef2('d',cD1,'bior3.7',1);
or
sx = size(X); A1 = idwt2(cA1,[],[],[],'bior3.7',sx); H1 = idwt2([],cH1,[],[],'bior3.7',sx); V1 = idwt2([],[],cV1,[],'bior3.7',sx); D1 = idwt2([],[],[],cD1,'bior3.7',sx);
To display the results of the level 1 decomposition, type
colormap(map); 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')
Regenerate an image by singlelevel Inverse Wavelet Transform.
To find the inverse transform, type
Xsyn = idwt2(cA1,cH1,cV1,cD1,'bior3.7');
This reconstructs or synthesizes the original image from the coefficients of the level 1 approximation and details.
Perform a multilevel wavelet decomposition.
To perform a level 2 decomposition of the image (again using
the bior3.7
wavelet), type
[C,S] = wavedec2(X,2,'bior3.7');
where X
is the original image matrix, and
2 is the level of decomposition.
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.
Extract approximation and detail coefficients.
To extract the level 2 approximation coefficients from C
,
type
cA2 = appcoef2(C,S,'bior3.7',2);
To extract the first and secondlevel detail coefficients from C
,
type
cH2 = detcoef2('h',C,S,2); cV2 = detcoef2('v',C,S,2); cD2 = detcoef2('d',C,S,2); cH1 = detcoef2('h',C,S,1); cV1 = detcoef2('v',C,S,1); cD1 = detcoef2('d',C,S,1);
or
[cH2,cV2,cD2] = detcoef2('all',C,S,2); [cH1,cV1,cD1] = detcoef2('all',C,S,1);
where the first argument ('
h
'
, '
v
'
,
or '
d
'
) determines
the type of detail (horizontal, vertical, diagonal) extracted, and
the last argument determines the level.
Reconstruct the Level 2 approximation and the Level 1 and 2 details.
To reconstruct the level 2 approximation from C
,
type
A2 = wrcoef2('a',C,S,'bior3.7',2);
To reconstruct the level 1 and 2 details from C
,
type
H1 = wrcoef2('h',C,S,'bior3.7',1); V1 = wrcoef2('v',C,S,'bior3.7',1); D1 = wrcoef2('d',C,S,'bior3.7',1); H2 = wrcoef2('h',C,S,'bior3.7',2); V2 = wrcoef2('v',C,S,'bior3.7',2); D2 = wrcoef2('d',C,S,'bior3.7',2);
Display the results of a multilevel decomposition.
Note With all the details involved in a multilevel image decomposition, it makes sense to import the decomposition into the Wavelet 2D graphical tool in order to more easily display it. For information on how to do this, see Loading Decompositions. 
To display the results of the level 2 decomposition, type
colormap(map); 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')
Reconstruct the original image from the multilevel decomposition.
To reconstruct the original image from the wavelet decomposition structure, type
X0 = waverec2(C,S,'bior3.7');
This reconstructs or synthesizes the original image from the
coefficients C
of the multilevel decomposition.
Compress the image and display it.
To compress the original image X
, use the ddencmp
command to calculate the default
parameters and the wdencmp
command
to perform the actual compression. Type
[thr,sorh,keepapp]= ddencmp('cmp','wv',X); [Xcomp,CXC,LXC,PERF0,PERFL2] = ... wdencmp('gbl',C,S,'bior3.7',2,thr,sorh,keepapp);
Note that we pass in to wdencmp
the results
of the decomposition (C
and S
)
we calculated in 7step 7. We also specify the bior3.7
wavelets,
because we used this wavelet to perform the original analysis. Finally,
we specify the global thresholding option '
gbl
'
.
See ddencmp
and wdencmp
reference pages for more information
about the use of these commands.
To view the compressed image side by side with the original, type
colormap(map); subplot(121); image(X); title('Original Image'); axis square subplot(122); image(Xcomp); title('Compressed Image'); axis square
PERF0 PERF0 = 49.8076 PERFL2 PERFL2 = 99.9817
These returned values tell, respectively, what percentage of the wavelet coefficients was set to zero and what percentage of the image's energy was preserved in the compression process.
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.
From the File menu, choose the Load > Image option.
When the Load Image dialog
box appears, select the MATfile wbarb.mat
, which
is in the MATLAB folder toolbox/wavelet/wavelet
.
Click the OK button.
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 green 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 green 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 yellow vertical 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 > 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 > 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 > 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 
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 
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 