# function manik % 2D image analysis. Demonstrates image compression using % biorthogonal 9/7 filters. % Load a test image.

Matlab test images consist of a matrix, X, % color palette, map, which maps each value of the matrix to a % color. Here, we will apply the Discrete Wavelet Transform to X. load woman2 close all clf image(X) colormap(map) axis image; set(gca,'XTick',[],'YTick',[]); title('Original') pause % We will use the 9/7 filters with symmetric extension at the % boundaries. dwtmode('sym') wname = 'bior4.4' % Plot the structure of a two stage filter bank. t = wtree(X,2,'bior4.4'); plot(t) pause close(2) % Compute a 2-level decomposition of the image using the 9/7 filters. [wc,s] = wavedec2(X,2,wname); % Extract the level 1 coefficients. a1 = appcoef2(wc,s,wname,1); h1 = detcoef2('h',wc,s,1); v1 = detcoef2('v',wc,s,1); d1 = detcoef2('d',wc,s,1); % Extract the level 2 coefficients. a2 = appcoef2(wc,s,wname,2); h2 = detcoef2('h',wc,s,2); v2 = detcoef2('v',wc,s,2); d2 = detcoef2('d',wc,s,2); % Display the decomposition up to level 1 only. ncolors = size(map,1); % Number of colors. sz = size(X); cod_a1 = wcodemat(a1,ncolors); cod_a1 = wkeep(cod_a1, sz/2); cod_h1 = wcodemat(h1,ncolors); cod_h1 = wkeep(cod_h1, sz/2); cod_v1 = wcodemat(v1,ncolors); cod_v1 = wkeep(cod_v1, sz/2); cod_d1 = wcodemat(d1,ncolors); cod_d1 = wkeep(cod_d1, sz/2); image([cod_a1,cod_h1;cod_v1,cod_d1]); axis image; set(gca,'XTick',[],'YTick',[]); title('Single stage decomposition') colormap(map) pause % Display the entire decomposition upto level 2. cod_a2 = wcodemat(a2,ncolors); cod_a2 = wkeep(cod_a2, sz/4); cod_h2 = wcodemat(h2,ncolors); cod_h2 = wkeep(cod_h2, sz/4); cod_v2 = wcodemat(v2,ncolors); cod_v2 = wkeep(cod_v2, sz/4); cod_d2 = wcodemat(d2,ncolors); cod_d2 = wkeep(cod_d2, sz/4); image([[cod_a2,cod_h2;cod_v2,cod_d2],cod_h1;cod_v1,cod_d1]);

8).wc.2).[]).wc.[].ncolors). wrcoef2('d'.ncolors).2).4.|| is the L2 vector norm.[].2). max(max(abs(X-XXhat)))) % Compression can be accomplished by applying a threshold to the % wavelet coefficients.4.'YTick'.[]). set(gca. image(cod_ra2).'YTick'. image(cod_rh2). axis image.'XTick'.s. cod_ra2 = wcodemat(ra2.4. subplot(3.wc.[].[]).wname.ncolors).s. axis image.[]. titl e('ra1') subplot(3.wc.s.'XTick'.'YTick'. cod_ra1 = wcodemat(ra1.11).ncolors).wname. title('Ori ginal') subplot(3. set(gca.4.[].wname. rd2 = wrcoef2('d'. sprintf('Reconstruction error (using wrcoef2) = %g'. tit le('rh2') subplot(3. wrcoef2('v'.wname).[]). set(gca.'XTick'.1). wrcoef2('h'.[]).wname.'YTick'.s. set(gca. titl e('ra2') subplot(3. image(cod_rd2).6). max(max(abs(X-Xhat)))) % Another way to reconstruct the image.[].s.'XTick'.[]).4.wname.1).s. axis image. title('Two stage decomposition') colormap(map) pause % Here are the reconstructed branches ra2 = wrcoef2('a'. % perf0 = compression performance = Percentage of zeros in wc_comp. set(gca.s. XXhat = waverec2(wc.'YTick'.'YTick'. image(cod_rv2). cod_rd1 = wcodemat(rd1.wc. axis image. image(cod_ra1).'XTick'. set(gca.'YTick'. rh2 = wrcoef2('h'.12).wc.2).[]. cod_rd2 = wcodemat(rd2. titl e('rh1') subplot(3.'XTick'. set(gca.axis image.5). axis image.[].'XTick'.9).'YTick'.10).wname. % ||. image(cod_rh1).s. thr = 20. cod_rh2 = wcodemat(rh2. Xhat = ra2 + rh2 + rv2 + rd2 + rh1 + rv1 + rd1.[].1).[]. axis image.s.ncolors).ncolors).1). image(cod_rd1). axis image.4.wname.'XTick'.'XTick'.[]). set(gca.4.'XTick'.4. sprintf('Reconstruction error (using waverec2) = %g'. set(gca. cod_rv2 = wcodemat(rv2. tit le('rv2') subplot(3. Last argument = 1 means do not % threshold the approximation coefficients. titl e('rd1') subplot(3.wc.1). % 'h' means use hard thresholding. % perfL2 = energy recovery = 100 * ||wc_comp||^2 / ||wc||^2.wname.[]).ncolors). cod_rh1 = wcodemat(rh1.7). rv2 = wrcoef2('v'. wdencmp is the function that does this. .'YTick'. image(X). image(cod_rv1). axis image.ncolors).wc. ra1 rh1 rv1 rd1 = = = = wrcoef2('a'.4. set(gca. cod_rv1 = wcodemat(rv1.[]). tit le('rd2') pause % Adding together the reconstructed average at level 2 and all of % the reconstructed details gives the full reconstructed image. titl e('rv1') subplot(3.[]).'YTick'. axis image.

ncolors). clf subplot(1.'h'. image(cod_X_comp).'XTick'. set(gca.2.1f%%'.perfL2.2. axis image. axis image.'YTick'.2.1). set(gca.perf0. perf0)) % Return to default settings. title('Original') cod_X_comp = wcodemat(X_comp.[].ncolors). subplot(1. dwtmode('zpd') . thr_v].[]).2).'XTick'. title('Compressed using variable hard thresholds') xlabel(sprintf('Energy retained = %2.1f%% \nNull coefficients = %2.thr.'h'). title('Compressed using global hard threshold') xlabel(sprintf('Energy retained = %2.[X_comp.[]). thr_d = [23 19].2). % diagonal thresholds. % vertical thresholds.'YTick'.wname. [X_comp.'YTick'.wname. image(X).thr.perfL2] = wdencmp('gbl'. axis image.perfL2.[]). thr_d.1). % horizontal thresholds.s_comp. perf0)) pause % Better compression can be often be obtained if different thresholds % are allowed for different subbands. image(X). thr_h = [21 17].'XTick'.1f%% \nNull coefficients = %2.2.perf0. title('Original') cod_X_comp = wcodemat(X_comp.perfL2] = wdencmp('lvd'.[]).s_comp.[]. set(gca. clf subplot(1.1). thr_v = [21 17].wc_comp. set(gca.2.wc_comp.[].[]. subplot(1. image(cod_X_comp).X.1f%%'.'XTick'.s. axis image. thr = [thr_h.2.wc.'YTick'.