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]);

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

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

Sign up to vote on this title
UsefulNot useful