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

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

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