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

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

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

Sign up to vote on this title
UsefulNot useful