Documentation Center |
This section takes you through the features of two-dimensional 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 two-dimensional arrays corresponding to indexed image representations. However, the functions described are also available when using truecolor images, which are represented by m-by-n-by-3 arrays of uint8. For more information on image formats, see Wavelets: Working with Images. |
Function Name | Purpose |
---|---|
Single-level decomposition | |
Decomposition | |
Maximum wavelet decomposition level |
Function Name | Purpose |
---|---|
Single-level reconstruction | |
Full reconstruction | |
Selective reconstruction | |
Single reconstruction |
Function Name | Purpose |
---|---|
Extraction of detail coefficients | |
Extraction of approximation coefficients | |
Recomposition of decomposition structure |
Function Name | Purpose |
---|---|
Provide default values for de-noising and compression | |
Penalized threshold for wavelet 1-D or 2-D de-noising | |
Thresholds for wavelet 2-D using Birgé-Massart strategy | |
Wavelet de-noising and compression | |
Threshold settings manager |
In this section, you'll learn
How to load an image
How to analyze an image
How to perform single-level 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 two-dimensional 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 reference page). |
From the MATLAB^{®} prompt, type
load wbarb; whos
Name | Size | Bytes | Class |
---|---|---|---|
X | 256x256 | 524288 | double array |
map | 192x3 | 4608 | double array |
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 single-level wavelet decomposition.
To perform a single-level 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 level-one approximation (cA1) and horizontal, vertical and diagonal details (cH1,cV1,cD1, respectively).
Construct and display approximations and details from the coefficients.
To construct the level-one 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 single-level 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 second-level decomposition (that is, the second-level 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 second-level 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 2-D 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 graphical interface tools to analyze the image.
Start the 2-D Wavelet Analysis Tool.
From the MATLAB prompt, type
wavemenu
The Wavelet Tool Main Menu appears.
Click the Wavelet 2-D menu item. The discrete wavelet analysis tool for two-dimensional image data appears.
From the File menu, choose the Load > Image option.
When the Load Image dialog box appears, select the MAT-file wbarb.mat, which is in the MATLAB folder toolbox/wavelet/wavedemo. Click the OK button.
The image is loaded into the Wavelet 2-D tool.
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 2-D tool displays its analysis.
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 2-D 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 two-dimensional 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 2-D 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 2-D window. The Wavelet 2-D 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.
From the Wavelet 2-D Compression tool, click the Residuals button. The More on Residuals for Wavelet 2-D Compression window appears.
Displayed statistics include measures of tendency (mean, mode, median) and dispersion (range, standard deviation). In addition, the tool provides frequency-distribution diagrams (histograms and cumulative histograms). The same tool exists for the Wavelet 2-D De-noising 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 2-D 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 2-D 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 2-D tool, and then save the processed image to a MAT-file (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 2-D Compression window, update the synthesized image by clicking Yes in the dialog box that appears.
Then, from the Wavelet 2-D tool, select the File > Save > Synthesized Image menu option. A dialog box appears allowing you to select a folder and filename for the MAT-file (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 de-noising or compression process are given by the wavelet name (wname) and the global threshold (valTHR).
Saving Discrete Wavelet Transform Coefficients. The Wavelet 2-D tool lets you save the coefficients of a discrete wavelet transform (DWT) to disk. The toolbox creates a MAT-file 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 1-by-142299 vector of concatenated coefficients, and sizes gives the length of each component.
Saving Decompositions. The Wavelet 2-D tool lets you save the entire set of data from a discrete wavelet analysis to disk. The toolbox creates a MAT-file in the current folder with a name you choose, followed by the extension wa2 (wavelet analysis 2-D).
Open the Wavelet 2-D 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 de-noising or compression inside the Wavelet 2-D tool. In the Wavelet 2-D De-noising window, you can save denoised image and decomposition. The same holds true for the Wavelet 2-D Compression window. This way, you can save many different trials from inside the De-noising and Compression windows without going back to the main Wavelet 2-D window during a fine-tuning process. When saving a synthesized signal, a decomposition or coefficients to a MAT-file, 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 graphical interface. The information you load may have been previously exported from the graphical interface, 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 2-D 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-by-3 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 2-D 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 2-D tool, save the image (and optionally, the variable map) in a MAT-file (with extension mat or other).
For instance, suppose you've created an image called brain and want to analyze it in the Wavelet 2-D tool. Type
X = brain; map = pink(256); save myfile X map
To load this image into the Wavelet 2-D tool, use the menu option File > Load > Image.
A dialog box appears that lets you select the appropriate MAT-file 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 2-D 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 two-dimensional 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 2-D tool, first save the appropriate data in a MAT-file, which must contain at least the two variables:
coefs, the coefficients vector
sizes, the bookkeeping matrix
For an indexed image the matrix sizes is a (n+2-by-2) array:
For a truecolor image, the matrix sizes is a (n+2-by-3):
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 level-n 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 2-D 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 2-D tool, you must first save the appropriate data in a MAT-file (with extension wa2 or other).
The MAT-file contains these variables.
Variable | Status | Description |
---|---|---|
coefs | Required | Vector of concatenated DWT coefficients |
sizes | Required | Matrix specifying sizes of components of coefs and of the original image |
wave_name | Required | String specifying name of wavelet used for decomposition (e.g., db3) |
map | Optional | n-by-3 colormap matrix. |
data_name | Optional | String 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 2-D tool to load the image decomposition data.
A dialog box appears, allowing you to choose the folder and file in which your data reside.