Wavelet Toolbox |
Two-Dimensional Analysis Using the Command Line
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).
|
Since the colormap is smooth in this image, you can now perform the decomposition.
This generates the coefficient matrices of the level-one approximation (cA1) and horizontal, vertical and diagonal details (cH1,cV1,cD1, respectively).
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);
This reconstructs or synthesizes the original image from the coefficients of the level 1 approximation and details.
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.
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);
where the first argument ('
h'
, '
v'
, or '
d'
) determines the type of detail (horizontal, vertical, diagonal) extracted, and the last argument determines the level.
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. |
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')
This reconstructs or synthesizes the original image from the coefficients C of the multilevel decomposition.
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 Step 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.
Two-Dimensional Discrete Wavelet Analysis | Two-Dimensional Analysis Using the Graphical Interface |
© 1994-2005 The MathWorks, Inc.