Sie sind auf Seite 1von 13

LAMPIRAN A

function [I_W , S] = func_DWT(I, level, Lo_D, Hi_D);


% Matlab implementation of SPIHT (without Arithmatic coding stage)
%
% Wavelet decomposition
%
% input: I : input image
% level : wavelet decomposition level
% Lo_D : low-pass decomposition filter
% Hi_D : high-pass decomposition filter
%
% output: I_W : decomposed image vector
% S : corresponding bookkeeping matrix
%
% please refer wavedec2 function to see more
%
% Jing Tian
% Contact me : scuteejtian@hotmail.com
% This program is part of my undergraduate project in GuangZhou, P. R.
China.
% April - July 1999

[C,S] = func_Mywavedec2(I,level,Lo_D,Hi_D);

S(:,3) = S(:,1).*S(:,2); % dim of detail coef nmatrices

L = length(S);

I_W = zeros(S(L,1),S(L,2));

% approx part
I_W( 1:S(1,1) , 1:S(1,2) ) = reshape(C(1:S(1,3)),S(1,1:2));

for k = 2 : L-1
rows = [sum(S(1:k-1,1))+1:sum(S(1:k,1))];
columns = [sum(S(1:k-1,2))+1:sum(S(1:k,2))];
% horizontal part
c_start = S(1,3) + 3*sum(S(2:k-1,3)) + 1;
c_stop = S(1,3) + 3*sum(S(2:k-1,3)) + S(k,3);
I_W( 1:S(k,1) , columns ) = reshape( C(c_start:c_stop) , S(k,1:2) );

% vertical part
c_start = S(1,3) + 3*sum(S(2:k-1,3)) + S(k,3) + 1;
c_stop = S(1,3) + 3*sum(S(2:k-1,3)) + 2*S(k,3);
I_W( rows , 1:S(k,2) ) = reshape( C(c_start:c_stop) , S(k,1:2) );

% diagonal part
c_start = S(1,3) + 3*sum(S(2:k-1,3)) + 2*S(k,3) + 1;
c_stop = S(1,3) + 3*sum(S(2:k,3));
I_W( rows , columns ) = reshape( C(c_start:c_stop) , S(k,1:2) );

end
function [significance_map, refinement] =
func_ezw_enc(img_wavedata,ezw_encoding_threshold);
% img_wavedata: wavelet coefficients to encode
% ezw_encoding_threshold: determine where to stop encoding (a lower
% threshold value gives better image reconstruction quality)
%
% significance_map:
% a string matrix containing significance data for different passes
('p','n','z','t'), where each row contains data for a different scanning
pass.
% refinement: a strubg matrix containing refinement data for different
passes ('0' or '1'), each row contains data for a different scanning pass.
%

subordinate_list = [];
refinement = [];
significance_map = [];
img_wavedata_save = img_wavedata;
img_wavedata_mat = img_wavedata;

% calculate Morton scan order


n = size(img_wavedata,1);
scan = func_morton([0:(n*n)-1],n);

% calculate initial threshold


%max(max(abs(img_wavedata)))
init_threshold = pow2(floor(log2(max(max(abs(img_wavedata))))));
threshold = init_threshold;

while (threshold >= ezw_encoding_threshold)

[str, list, img_wavedata] = func_dominant_pass(img_wavedata, threshold,


scan);
significance_map = strvcat(significance_map, char(str));

if(threshold == init_threshold),
subordinate_list = list;
else
subordinate_list = func_rearrange_list(subordinate_list, list,
scan, img_wavedata_save);
end
[encoded, subordinate_list] =
func_subordinate_pass(subordinate_list, threshold);
refinement = strvcat(refinement, strrep(num2str(encoded), ' ', ''));

threshold = threshold / 2;
end

function scan = func_morton(pos,n);

% The matrix should be n by n


% For position we start counting from 0
%
% position = [0:(n*n)-1];
% scan = morton(position, n);
% Copyright 2002 Paschalis Tsiaflakis, Jan Vangorp
% Revision 1.0 10/11/2002 19.00u

bits = log2(n*n); % number of bits needed to represent position


bin = dec2bin(pos(:),bits); % convert position to binary
% odd bits represent row number
% even bits represent column number

scan = [bin2dec(bin(:,1:2:bits-1)), bin2dec(bin(:,2:2:bits))];

function [signif_map, subordinate_list, data] =


func_dominant_pass(img_wavedata, threshold, scan);
%
% img_wavedata: wavelet coefficients, if this step is not using the
initial theshold
% the returned 'data' from the previous step should be used (to
% correctly process already processed coefficients)
% threshold: threshold to use for this step, initial threshold should be
% pow2(floor(log2(max(max(abs(img_wavedata))))))
% scan: scan order to use when processing img_wavedata matrix
% (currently only Morton scan order supported)
%
% signif_map: returned string containing the significance info for the
data in
% img_wavedata using 'threshold' as threshold
% 'p' significant positive
% 'n' significant negative
% 'z' isolated zero
% 't' zerotree root
% subordinate_list: list containing the coefficients that are detected
significant
% in _this_ step
% first row is the original coefficient
% second row is first reconstruction value of this
coefficient
% data: new wavelet coefficients to use in the next step
%
% Copyright 2002 Paschalis Tsiaflakis, Jan Vangorp

% wavelet coefficients are saved to undo bookkeeping actions


data = img_wavedata;
dim = size(img_wavedata,1);

% significance map
signif_map = [];
signif_index = 1;

% subordinate list
subordinate_list = [];
subordinate_index = 1;

for element = 1:dim*dim;


% get matrix index for element
row = scan(element,1)+1;
column = scan(element,2)+1;

% check whether element should be processed


if(~isnan(data(row, column)) & data(row, column) < realmax),
% determine type of element
if(data(row,column) >= threshold), % element is significant
positive
signif_map(signif_index) = 'p';
signif_index = signif_index + 1;

subordinate_list(1, subordinate_index) = data(row, column);


% first reconstructed value
subordinate_list(2, subordinate_index) = threshold +
threshold/2;
subordinate_index= subordinate_index + 1;

% mark element as processed


data(row, column) = 0;

elseif(data(row,column) <= -threshold), % element is significant


negative
signif_map(signif_index) = 'n';
signif_index = signif_index+ 1;

subordinate_list(1, subordinate_index) = data(row, column);


% first reconstructed value
subordinate_list(2, subordinate_index) = -threshold -
threshold/2;
subordinate_index= subordinate_index + 1;

% mark element as processed


data(row, column) = 0;

else % determine wether element is zerotree root


% select EZW tree for element
if(row<dim/2 | column<dim/2),
mask = func_treemask(row,column,dim);
else % shortcut treemask processing (element has no tree under
it)
if(abs(data(row, column)) < threshold),
% element is zerotree root
signif_map(signif_index) = 't';
signif_index = signif_index + 1;

% mark elements as processed (only for this pass!)


data(row, column) = realmax;
else % element is isolated zero
signif_map(signif_index) = 'z';
signif_index = signif_index + 1;
end
end
masked = data .* mask;

% compare data to threshold


if(isempty(find(abs(masked) >= threshold))),
% element is zerotree root
signif_map(signif_index) = 't';
signif_index = signif_index + 1;

% mark elements as processed (only for this pass!)


data = data + (mask*realmax);
else % element is isolated zero
signif_map(signif_index) = 'z';
signif_index = signif_index + 1;
end
end
end
end

index = find(data == realmax);


data(index) = img_wavedata(index);

function subordinate_list = func_rearrange_list(orig_list, add_list, scan,


wavedata);

% orig_list: original subordinate list, this is the old subordinate list


% (in scan order!)
% add_list: new subordinate list items that should be added to the
subordinate
% list while maintaining scan order
% scan: scan order to use
% wavedata: _original_ wavelet coefficients (used to determine correct
order
% for subordinate list
%
% subordiante_list: new subordiante list in scan order
%
% Copyright 2002 Paschalis Tsiaflakis, Jan Vangorp
% Revision 1.0 10/11/2002 19.00u

subordinate_list = [];
o_index = 1; % index original_list
a_index = 1; % index add_list

for element = 1:size(scan,1),


row = scan(element,1)+1;
column = scan(element,2)+1;

% test index before list, otherwise you get out of matrix dimensions!!
if(size(orig_list,2) >= o_index & wavedata(row, column) ==
orig_list(1,o_index)),
subordinate_list = [subordinate_list orig_list(:,o_index)];
o_index = o_index + 1;
elseif(size(add_list,2) >= a_index & wavedata(row, column) ==
add_list(1,a_index)),
subordinate_list = [subordinate_list add_list(:,a_index)];
a_index = a_index + 1;
end
end

function [encoded, subordinate_list] =


func_subordinate_pass(subordinate_list, threshold);
%
% subordinate_list: current subordinate list containing coefficietns that
are
% alreday detected as significant
% first row is the original coefficient
% second row is current reconstruction value of this
coefficient
% this list should be in the correct scan order to
reduce complexity
% of decoder (Morton)
% threshold: current threshold to use when comparing
%
% encoded: matrix containing 0's and 1's for refinement of the suborinate
list
% subordinate_list: new subordinate_list (second row containing
reconstruction values
% is updated to the include refinement -> new
reconstuction values)
%
% Copyright 2002 Paschalis Tsiaflakis, Jan Vangorp
% Revision 1.0 9/11/2002 19.00u

% compare to threshold + threshold/2; bigger = 1, smaller = 0


encoded = zeros(1,size(subordinate_list,2));
encoded(find(abs(subordinate_list(1,:)) > abs(subordinate_list(2,:)))) = 1;

% update subordinate_list(2,:) (reconstructed values)


for i = 1:length(encoded),
if(encoded(i) == 1),
if(subordinate_list(1,i) > 0),
subordinate_list(2,i) = subordinate_list(2,i) + threshold/4;
else
subordinate_list(2,i) = subordinate_list(2,i) - threshold/4;
end
else
if(subordinate_list(1,i) > 0),
subordinate_list(2,i) = subordinate_list(2,i) - threshold/4;
else
subordinate_list(2,i) = subordinate_list(2,i) + threshold/4;
end
end
end

function img_ezw_stream_bit = func_huffman_encode(significance_map,


refinement);

% significance map: a string array containing significace map data


('p','n','z' and 't')
% refinement: a string array containing refinement data ('0' and '1')
%
% img_ezw_stream_bit: resulting bitstream ('0' and '1')
%
% Copyright 2002 Paschalis Tsiaflakis, Jan Vangorp
% Revision 1.0 10/11/2002 19.00u

img_ezw_stream_bit = [];
strings = size(significance_map,1);

for i = 1:strings,
% insert significance map using Huffman
index = 1;
while(index <= size(significance_map,2) &
~strcmp(significance_map(i,index),' ')),
if(strcmp(significance_map(i,index),'t')),
img_ezw_stream_bit = [img_ezw_stream_bit '0'];
elseif(strcmp(significance_map(i,index),'z')),
img_ezw_stream_bit = [img_ezw_stream_bit '10'];
elseif(strcmp(significance_map(i,index),'n')),
img_ezw_stream_bit = [img_ezw_stream_bit '110'];
else
img_ezw_stream_bit = [img_ezw_stream_bit '1110'];
end
index = index + 1;
end
% insert seperator
img_ezw_stream_bit = [img_ezw_stream_bit '1111'];
refine = size(strrep(refinement(i,:), ' ', ''),2);
% insert length of refinement (20 bits)
img_ezw_stream_bit = [img_ezw_stream_bit
strrep(char(dec2bin(refine,20)), ' ', '')];
% insert refinement
img_ezw_stream_bit = [img_ezw_stream_bit strrep(refinement(i,:), ' ',
'')];
end

% append end of stream


img_ezw_stream_bit = [img_ezw_stream_bit '11111'];

% stringlength should be multiple of 8: append zeros


% ASCII code for '0' is 48
append = 8 - mod(size(img_ezw_stream_bit,2), 8);
img_ezw_stream_bit = [img_ezw_stream_bit char(ones(1,append)*48)];

DECOMPRES

function [significance_map, refinement] =


func_huffman_decode(img_ezw_stream_bit);

% significance map: a string array containing significace map data


('p','n','z' and 't')
% refinement: a string array containing refinement data ('0' and '1')
%
% img_ezw_stream_bit: input bitstream ('0' and '1')
%
% Copyright 2002 Paschalis Tsiaflakis, Jan Vangorp
% Revision 1.0 10/11/2002 19.00u

significance_map = [];
refinement = [];

complete = 0;
decode = 1;
index = 1;
significance = [];
refine = [];

while(decode),
% get bit
bit = img_ezw_stream_bit(index);
index = index + 1;
if(bit == '1'),
% get next bit
bit = img_ezw_stream_bit(index);
index = index + 1;
if(bit == '1'),
% get next bit
bit = img_ezw_stream_bit(index);
index = index + 1;
if(bit == '1'),
% get next bit
bit = img_ezw_stream_bit(index);
index = index + 1;
if(bit == '1'),
% seperator detected
complete = 1;
else
significance = [significance, 'p'];
end
else
significance = [significance, 'n'];
end
else
significance = [significance, 'z'];
end
else
significance = [significance, 't'];
end

if(complete),
complete = 0;
% get size of refinement data (next 20 bits)
stringlength = bin2dec(img_ezw_stream_bit(1,index:index+19));
index = index + 20;
refine = [refine img_ezw_stream_bit(1,index:index+stringlength-1)];
index = index + stringlength;

% update significance map and refinement data


significance_map = strvcat(significance_map, significance);
refinement = strvcat(refinement, refine);
significance = [];
refine = [];
end

% check for end of stream ('11111')


bits = img_ezw_stream_bit(1,index:index+4);
if(strcmp(bits, '11111')),
decode = 0;
end
end

function img_wavedata_dec = func_ezw_decode(dim, threshold,


significance_map, refinement);
% dim: dimension of the wavelet matrix to reconstruct
% threshold: initial threshold used while encoding
% significance_map: a string matrix containing significance data for
% different scanning passes ('p','n','z','t'), where each row contains
% data for a different scanning pass.
%
% refinement: a string matrix containing refinement data for
% different scanning passes ('0' or '1'), where each row contains data for
% a different scanning pass.
%
% img_wavedata_dec: reconstructed wavelet coefficients
%

img_wavedata_dec = zeros(dim,dim);

% calculate Morton scan order


scan = func_morton([0:(dim*dim)-1],dim);
% number of steps significance map (and refinement data)
steps = size(significance_map,1);

for step = 1:steps,


% decode significancemap for this step
img_wavedata_dec = func_decode_significancemap(img_wavedata_dec,
significance_map(step,:), threshold, scan);

img_wavedata_dec = func_decode_refine(img_wavedata_dec,
refinement(step,:), threshold, scan);

threshold = threshold/2;

end
function scan = func_morton(pos,n);

% The matrix should be n by n


% For position we start counting from 0
%
% position = [0:(n*n)-1];
% scan = morton(position, n);
% Copyright 2002 Paschalis Tsiaflakis, Jan Vangorp
% Revision 1.0 10/11/2002 19.00u

bits = log2(n*n); % number of bits needed to represent position


bin = dec2bin(pos(:),bits); % convert position to binary

% odd bits represent row number


% even bits represent column number

scan = [bin2dec(bin(:,1:2:bits-1)), bin2dec(bin(:,2:2:bits))];


function img_wavedata_dec = func_decode_significancemap(img_wavedata_dec,
significance_map, threshold, scan);

% img_wavedata_dec: input wavelet coefficients


% significance_map: string containing the significance map ('p','n','z'
and 't')
% threshold: threshold to use during this decoding pass (dominant pass)
% scan: scan order to use (Morton)
%
% img_wavedata_dec: the decoded wavelet coefficients
%

backup = img_wavedata_dec;

n = size(img_wavedata_dec,1);
index = 1;

for element = 1:n*n;


% get matrix index for element
row = scan(element,1)+1;
column = scan(element,2)+1;

% check whether element should be processed


if(isfinite(img_wavedata_dec(row, column))),

% determine type of element


if(significance_map(index) == 'p'), % element is significant
positive
img_wavedata_dec(row, column) = threshold + threshold/2;
elseif(significance_map(index) == 'n'), % element is significant
negative
img_wavedata_dec(row, column) = -threshold - threshold/2;
elseif(significance_map(index) == 'z'), % element is isolated zero
img_wavedata_dec(row, column) = 0;
else
% element is zerotree root ('t')
img_wavedata_dec(row, column) = 0;

% mark decendants as inf


mask = func_treemask_inf(row,column,n);
img_wavedata_dec = img_wavedata_dec + mask;
end
index = index + 1;
end
end

% inf should be restored to 0


img_wavedata_dec(find(img_wavedata_dec > realmax)) = 0;

% original img_wavedata_dec was overwritten with 0: restore


img_wavedata_dec = img_wavedata_dec + backup;

function mask = func_treemask_inf(x, y, dim);

% x y is the position in the matrix of the node where the EZW tree
% should start; top left is x=1 y=1
%
% dim is the dimension of the mask (should be the same dimension as
% the wavelet data)
%
% mask is the returnend matrix to select the relevant coefficients
% from the wavelet coefficient matrix
% selected = mask .* wavelet_matrix;
% Copyright 2002 Paschalis Tsiaflakis, Jan Vangorp
% Revision 1.0 10/11/2002 19.00u

mask = zeros(dim);

x_min = x;
x_max = x;
y_min = y;
y_max = y;

while(x_max <= dim & y_max <= dim),


mask(x_min:x_max, y_min:y_max) = inf;

% calculate new subset


x_min = 2*x_min - 1;
x_max = 2*x_max;
y_min = 2*y_min - 1;
y_max = 2*y_max;
end
function img_wavedata_dec = func_decode_refine(img_wavedata_dec,
refinement, threshold, scan);
%
% img_wavedata_dec: input wavelet coefficients
% refinement: string containing refinement data ('0' and '1')
% threshold: threshold to use during this refinement pass
% scan: scan order to use (Morton)
%
% img_wavedata_dec: the refined wavelet coefficients
%

n = size(img_wavedata_dec,1);
index = 1;

for element = 1:n*n;


% get matrix index for element
row = scan(element,1)+1;
column = scan(element,2)+1;

% check whether element should be processed


if(img_wavedata_dec(row, column) ~= 0),
% get refinement data
ref = refinement(index);

% if refinement bit is 1, add T/4 to current value


% if refinement bit is 0, subtract T/4 from current value
if(ref == '1'),
if(img_wavedata_dec(row, column) > 0),
img_wavedata_dec(row, column) = img_wavedata_dec(row, column)
+ threshold/4;
else
img_wavedata_dec(row, column) = img_wavedata_dec(row, column)
- threshold/4;
end
else
if(img_wavedata_dec(row, column) > 0),
img_wavedata_dec(row, column) = img_wavedata_dec(row, column)
- threshold/4;
else
img_wavedata_dec(row, column) = img_wavedata_dec(row, column)
+ threshold/4;
end
end
index = index + 1;
end
end

function im_rec = func_InvDWT(I_W, S, Lo_R, Hi_R, level);


% Matlab implementation of SPIHT (without Arithmatic coding stage)
%
% Inverse wavelet decomposition
%
% input: I_W : decomposed image vector
% S : corresponding bookkeeping matrix
% Lo_D : low-pass decomposition filter
% Hi_D : high-pass decomposition filter
% level : wavelet decomposition level
%
%
%
%
%
%
%

L = length(S);

m = I_W;

C1 = zeros(1,S(1,3)+3*sum(S(2:L-1,3)));

% approx part
C1(1:S(1,3)) = reshape( m( 1:S(1,1) , 1:S(1,2) ), 1 , S(1,3) );

for k = 2:L-1
rows = [sum(S(1:k-1,1))+1:sum(S(1:k,1))];
columns = [sum(S(1:k-1,2))+1:sum(S(1:k,2))];
% horizontal part
c_start = S(1,3) + 3*sum(S(2:k-1,3)) + 1;
c_stop = S(1,3) + 3*sum(S(2:k-1,3)) + S(k,3);
C1(c_start:c_stop) = reshape( m( 1:S(k,1) , columns ) , 1, c_stop-
c_start+1);
% vertical part
c_start = S(1,3) + 3*sum(S(2:k-1,3)) + S(k,3) + 1;
c_stop = S(1,3) + 3*sum(S(2:k-1,3)) + 2*S(k,3);
C1(c_start:c_stop) = reshape( m( rows , 1:S(k,2) ) , 1 , c_stop-
c_start+1 );
% diagonal part
c_start = S(1,3) + 3*sum(S(2:k-1,3)) + 2*S(k,3) + 1;
c_stop = S(1,3) + 3*sum(S(2:k,3));
C1(c_start:c_stop) = reshape( m( rows , columns ) , 1 , c_stop-
c_start+1);
end

if (( L - 2) > level) %set those coef. in higher scale to 0


temp = zeros(1, length(C1) - (S(1,3)+3*sum(S(2:(level+1),3))));
C1(S((level+2),3)+1 : length(C1)) = temp;
end

S(:,3) = [];

im_rec = func_Mywaverec2(C1,S, Lo_R, Hi_R);

Das könnte Ihnen auch gefallen