## Demo: 2-D Haar Wavelet

Table of Contents |

Developer | sonots |
---|---|

First Edition | 05/2006 |

Last Modified | 05/2006 |

Language | Matlab |

This page is written in both English and Japanese.

## Abstract

This is a note rather than a project to use wavedec2 from wavelet toolbox.

## Math or Algorithm

Refer [1]

## doHaarWavelet.m

% doHaarWavelet: Apply 2-D haar wavelet for an image. % [C, S] = doHaarWavelet(I, R) returns the approximated % haar wavlet decomposition of the matrix I. The wavelet % decomposition is applied until getting overall approximation % and the # of coefficients is reduced into R percent (such as 5). % Outputs are the approximated decomposition vector C and the % corresponding bookkeeping matrix S. % % Requirements: wavelet toolbox % See also wavedec2, waverec2 % http://dali.feld.cvut.cz/ucebna/matlab/toolbox/wavelet/wavedec2.html % % Author : Naotoshi Seo % Date : May, 2006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [C, S] = doHaarWavelet(I, R) % Testing % I = imread('../recog_pie_jpg/11/04001_B_W_11_gray_01.png'); I = double(I); if nargin < 2 R = 100; end % Test dwt2 % [CA,CH,CV,CD] = dwt2(I, 'haar'); % Test reconstruction % rec = idwt2(CA,CH,CV,CD, 'haar'); % rec = uint8(rec); % isequal(I, rec) % imshow(rec) % Test wavedec2 % N = 1; % [C,S] = wavedec2(I, N, 'haar'); % A{N} = C(1:S(N,1)*S(N,2)); % A{N} = reshape(A{N}, S(N,1), S(N,2)); % isequal(CA, A{N}) % Test reconstruction % rec = waverec2(C, S, 'haar'); % rec = uint8(rec); % isequal(I, rec) % imshow(rec) N = ceil(log2(length(I))); [C,S] = wavedec2(I, N, 'haar'); % Test reconstruction % C(1000:end) = 0 % rec = waverec2(C, S, 'haar'); % rec = uint8(rec); % imshow(rec) DIM = floor(length(C)*(R*0.01)); C = C(1:DIM); end

## An experiment of data compression by haar wavelet

### Source Codes

function wavedec2Test(percent) I = imread('lena.png'); I = double(I); R = I(:, :, 1); G = I(:, :, 2); B = I(:, :, 3); N = ceil(log2(length(R))); [RC, RS] = wavedec2(R, N, 'haar'); [GC, GS] = wavedec2(G, N, 'haar'); [BC, BS] = wavedec2(B, N, 'haar'); DIM = floor(length(RC)*(percent*0.01)); RC(DIM+1:end) = 0; GC(DIM+1:end) = 0; BC(DIM+1:end) = 0; RR = waverec2(RC, RS, 'haar'); GR = waverec2(GC, GS, 'haar'); BR = waverec2(BC, BS, 'haar'); IR = cat(3, RR, GR, BR); IR = uint8(IR); imshow(IR); imwrite(IR, sprintf('lenadwt%02d.png', percent), 'PNG'); end