can anyone help me in the calculation of the compression ratio for the code below
    2 views (last 30 days)
  
       Show older comments
    
%-------script sampletestv2----------------
clc;
% This is a test program that tests the EZW encoder and decoder
% matrix X is an example matrix same as Shapiro's paper given
% Written by xiangang li
%
% This function calls:
%    dominantpass.m
%    subordinatepass.m
%    checkdescents1.m
%    checkchildren.m
%    mapping.m
X=[63 -34 49 10 7 13 -12 7;
   -31 23 14 -13 3 4 6 -1;
   15 14 3 -12 5 -7 3 9;
   -9 -7 -14 8 4 -2 3 2;
   -5 9 -1 47 4 6 -2 2;
   3 0 -3 2 3 -2 0 4;
   2 -3 6 -4 3 6 3 6;
   5 11 5 6 0 3 -4 4];
X0=X;
Y0=max(X);
     Y1=max(Y0); 
     for i=0:20;
        if 2^i<=Y1 & 2^i>0.5*Y1;
           threshold=2^i;   % get initial threshold T0;
           initialthreshold=threshold; % get initial threshold T0;
           laststeplevel=i+1;% last step level
           break;
        end;
     end;
       sublist=[];
       sub_list=[];
       [xx,yy]=size(X);
A=mappingv2(xx);
[m,n]=size(A);
global N;      % Let Morton scanorder vector as a global variable
 N=zeros(m*n,2);
       for i=1:m,
          for j=1:n,
                   N(A(i,j),1)=i;
                   N(A(i,j),2)=j;
           end
       end
         order=1;  
  while threshold ~= 0.5,  % if threshold~=1, do dominantpass and subordinatepass.
     threshold
     %Dominant Pass
     [D,X,sublist,sub_list] = dominantpassv2(X,threshold,sublist,sub_list); 
     DD{order}=D
     significantlist{order}=sub_list;
      %Subordinate pass
      threshold=threshold/2;
      if threshold ==0.5,
          break;
      end
     S = subordinatepassv2(sublist,threshold);
     SS{order}=S
     order=order+1;
end
%*********************************%   
%*********EZW decoder*************%
%*********************************%
   global N;
   [m,n]=size(N);% the size of initial image
                 % m is the pixels of initial image
   XX=zeros(sqrt(m)); % initialize the reconstructured image to zero;
   threshold=initialthreshold; % initial theshold ;
   sublist=[]; % sublist is the new position matrix 
               % for all significant coefficients 'p' and 'n';
for level=1:laststeplevel, 
              RR=zeros(size(XX)); % reference matrix RR;
       [a,b]=size(DD{level}); % ?    
   % dominant pass
          i=1; j=1;
       while j<=b,
           if RR(N(i,1),N(i,2))==0
                if DD{level}(j)=='p'
                    if threshold==1
                       XX(N(i,1),N(i,2))=threshold; 
                   else
                       XX(N(i,1),N(i,2))=1.5*threshold;
                   end
                end
                if DD{level}(j)=='n'
                   if threshold==1
                       XX(N(i,1),N(i,2))=-threshold; 
                   else
                       XX(N(i,1),N(i,2))=-1.5*threshold;
                   end
                end
                if DD{level}(j)=='t'& A(N(i,1),N(i,2))<=m/4
                   RR=checkchildrenv2(i,RR);% all zerotree's descendants are set to 1.
                end
                RR(N(i,1),N(i,2))=1; %reference matrix =1;
                i=i+1;
                j=j+1;
          else i=i+1;
          end
      end
   % subordinate pass
   [xx,yy]=size(significantlist{level});
   threshold=threshold/2;
     for i=1:xx,
         if level==laststeplevel|threshold==0.5
             break
         end
        if SS{level}(i)==1
           if XX(sub_list(i,1),sub_list(i,2))>0;
              XX(sub_list(i,1),sub_list(i,2))= fix(XX(sub_list(i,1),sub_list(i,2))+ threshold/2);
           else 
              XX(sub_list(i,1),sub_list(i,2))= fix(XX(sub_list(i,1),sub_list(i,2))-threshold/2);
           end
        end
        if SS{level}(i)==0
           if XX(sub_list(i,1),sub_list(i,2))>0;
              XX(sub_list(i,1),sub_list(i,2))= fix(XX(sub_list(i,1),sub_list(i,2))-threshold/2);
           else 
              XX(sub_list(i,1),sub_list(i,2))= fix(XX(sub_list(i,1),sub_list(i,2))+threshold/2);
           end
        end
     end
        threshold
        level
        XX
end
initialiamge=X0
reconstructedimage=XX
difference=XX-X0
 whos
       %------function mappingv2-------------------
  function A = mappingv2(n)
if n == 2
     A = [1 2; 3 4];
else
     B = mappingv2(n/2);
     A = [B B+(n/2)^2; B+(n/2)^2*2 B+(n/2)^2*3];
end
%------function dominantpassv2-----------------
function [D,X,sublist,sub_list] = dominantpassv2(X,threshold,sublist,sub_list) 
% Dominant pass function
D=[];
global N;
[m,n]=size(X);
% X is the coefficients matrix
R=zeros(m); % matrix R is a reference matrix, same size as X; '0' means
              %this coefficient is not a descendant from zerotree root;
[a,b]=size(N);
                if abs(X(1,1))>=threshold % X(1,1) is DC coefficient
                    sublist=[sublist, abs(X(1,1))]; % put significant coefficients's value to sublist
                    sub_list=[sub_list;N(1,1),N(1,2)];% put the significant coefficients' position in sub_list
                    if X(1,1)>0;
                       D=[D,'p']; 
                    else D=[D,'n'];
                    end  
                    X(1,1)=0;
                else D=[D,'z'];
                end 
            for k=2:4,
                  if abs(X(N(k,1),N(k,2)))>=threshold, 
                      sublist=[sublist, abs(X(N(k,1),N(k,2)))]; 
                    % append this significant coefficient to the subordinate list;
                      sub_list=[sub_list;N(k,1),N(k,2)];
                                   if X(N(k,1),N(k,2))>0 % determine the sign
                                      D=[D,'p']; % >0,assign a "p"
                                   else D=[D,'n'];% <0,assign a "n"
                                   end
                                   X(N(k,1),N(k,2))=0; 
        % the significent coefficients is replaced by a '0' in the coefficients matrix  
                 else
                    % 2,3,4 has no parents,just check its descendants.
                                   result = checkdescendants1v2( k,X,threshold,0); 
                                   if result==1
                                       D=[D,'z'];
                                   else
                                       D=[D,'t'];
                                       R(N(k,1),N(k,2))=1;   % Zerotree, make all its descendants
                                       R=checkchildrenv2(k,R); % refference matrix component to 1.
                                   end    
                 end
             end
             for k=5:a,
                     if abs(X(N(k,1),N(k,2)))>=threshold, 
                        sublist=[sublist, abs(X(N(k,1),N(k,2)))]; 
                        sub_list=[sub_list;N(k,1),N(k,2)];
                                   if X(N(k,1),N(k,2))>0, % determine the sign
                                      D=[D,'p']; % >0,assign a "p"
                                   else D=[D,'n'];% <0,assign a "n"
                                   end
                                   X(N(k,1),N(k,2))=0; 
                     elseif R(N(k,1),N(k,2))==0
                                   result = checkdescendants1v2( k,X,threshold,0); 
                        % Check its has significant descendants?               
                                   if result==1,
                                       D=[D,'z']; % isolated zero
                                   else D=[D,'t'];% zerotree
                                       R(N(k,1),N(k,2))=1;  
                                       R=checkchildrenv2(k,R);
                                   % if zerotree, reference matrix coefficients=1
                                   end
                     end
             end
          %-------function subordinantpassv2-------
function S = subordinatepassv2(sublist,threshold) 
S=[];
[m,n]=size(sublist);
   for i=1:n;
      if bitand(sublist(1,i),threshold)==threshold,
       %if sublist(1,i)>=threshold,
           S=[S,1];
           %sublist(1,i)=sublist(1,i)-threshold;
       else S=[S,0]; 
       end
   end
%-------function checkdescendantv2------------
function   result = checkdescendants1v2(j,X,threshold,result)
% initial set result=0
% if the result=1, means that a coefficient has at least 
% 1 significant descendant.
global N
[m,n]=size(N);
for i=(4*j-3):4*j;
        if result==1 | i > m
           break;
        end;
       if abs(X(N(i,1),N(i,2)))>=threshold     
          result=1;
          break; 
       else 
       result=checkdescendants1v2(i,X,threshold,result);
       end;
end;
%------function checkchildrenv2------------
function   RR=checkchildrenv2(j,RR)
% if a symbol 't' is encounted, then make all its descendants in reference  
% matrix RR's components equal 1---ZEROTREES
global N
[m,n]=size(N);
for i=(4*j-3):4*j;
        if  i<=m, 
           RR(N(i,1),N(i,2))=1;
           RR=checkchildrenv2(i,RR);
       end
end;
     %------output--------
threshold =
    32
DD = 
  Columns 1 through 2
    'pnztpttttztttttttptt'    'ztnptttttttt'
  Columns 3 through 5
    [1x56 char]    [1x56 char]    [1x56 char]
  Columns 6 through 7
    'zzzttztttztttttnnttt'    [1x256 char]
  Column 8
    'ztzttzttzttttttztttztzttttzttptt'
SS = 
  Columns 1 through 3
    [1x4 double]    [1x6 double]    [1x20 double]
  Columns 4 through 6
    [1x41 double]    [1x59 double]    [1x65515 double]
  Column 7
    [1x65535 double]
threshold =
    16
DD = 
  Columns 1 through 2
    'pnztpttttztttttttptt'    'ztnptttttttt'
  Columns 3 through 5
    [1x56 char]    [1x56 char]    [1x56 char]
  Columns 6 through 7
    'zzzttztttztttttnnttt'    [1x256 char]
  Column 8
    'ztzttzttzttttttztttztzttttzttptt'
SS = 
  Columns 1 through 3
    [1x4 double]    [1x6 double]    [1x20 double]
  Columns 4 through 6
    [1x41 double]    [1x59 double]    [1x65515 double]
  Column 7
    [1x65535 double]
threshold =
     8
DD = 
  Columns 1 through 2
    'pnztpttttztttttttptt'    'ztnptttttttt'
  Columns 3 through 5
    [1x56 char]    [1x56 char]    [1x56 char]
  Columns 6 through 7
    'zzzttztttztttttnnttt'    [1x256 char]
  Column 8
    'ztzttzttzttttttztttztzttttzttptt'
SS = 
  Columns 1 through 3
    [1x4 double]    [1x6 double]    [1x20 double]
  Columns 4 through 6
    [1x41 double]    [1x59 double]    [1x65515 double]
  Column 7
    [1x65535 double]
threshold =
     4
DD = 
  Columns 1 through 2
    'pnztpttttztttttttptt'    'ztnptttttttt'
  Columns 3 through 5
    [1x56 char]    [1x56 char]    [1x56 char]
  Columns 6 through 7
    'zzzttztttztttttnnttt'    [1x256 char]
  Column 8
    'ztzttzttzttttttztttztzttttzttptt'
SS = 
  Columns 1 through 3
    [1x4 double]    [1x6 double]    [1x20 double]
  Columns 4 through 6
    [1x41 double]    [1x59 double]    [1x65515 double]
  Column 7
    [1x65535 double]
threshold =
     2
DD = 
  Columns 1 through 2
    'pnztpttttztttttttptt'    'ztnptttttttt'
  Columns 3 through 5
    [1x56 char]    [1x56 char]    [1x56 char]
  Columns 6 through 7
    'zzzttztttztttttnnttt'    [1x256 char]
  Column 8
    'ztzttzttzttttttztttztzttttzttptt'
SS = 
  Columns 1 through 3
    [1x4 double]    [1x6 double]    [1x20 double]
  Columns 4 through 6
    [1x41 double]    [1x59 double]    [1x65515 double]
  Column 7
    [1x65535 double]
threshold =
     1
DD = 
  Columns 1 through 2
    'pnztpttttztttttttptt'    'ztnptttttttt'
  Columns 3 through 5
    [1x56 char]    [1x56 char]    [1x56 char]
  Columns 6 through 7
    'zzzttztttztttttnnttt'    [1x256 char]
  Column 8
    'ztzttzttzttttttztttztzttttzttptt'
threshold =
    16
level =
     1
XX =
    56   -40    56     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0    40     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
threshold =
     8
level =
     2
XX =
    60   -36    52     0     0     0     0     0
   -28    20     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0    44     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
threshold =
     4
level =
     3
XX =
    62   -34    50    10     0    14   -14     0
   -30    22    14   -14     0     0     0     0
    14    14     0   -14     0     0     0    10
   -10     0   -14    10     0     0     0     0
     0    10     0    46     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0    10     0     0     0     0     0     0
threshold =
     2
level =
     4
XX =
    63   -35    49    11     7    13   -13     7
   -31    23    15   -13     0     5     7     0
    15    15     0   -13     5    -7     0     9
    -9    -7   -15     9     5     0     0     0
    -5     9     0    47     5     7     0     0
     0     0     0     0     0     0     0     5
     0     0     7    -5     0     7     0     7
     5    11     5     7     0     0    -5     5
threshold =
     1
level =
     5
XX =
    63   -34    49    10     7    13   -12     7
   -31    23    14   -13     3     4     6     0
    15    14     3   -12     5    -7     3     9
    -9    -7   -14     8     4    -2     3     2
    -5     9     0    47     4     6    -2     2
     3     0    -3     2     3    -2     0     4
     2    -3     6    -4     3     6     3     6
     5    11     5     6     0     3    -4     4
threshold =
                       0.5
level =
     6
XX =
    63   -34    49    10     7    13   -12     7
   -31    23    14   -13     3     4     6    -1
    15    14     3   -12     5    -7     3     9
    -9    -7   -14     8     4    -2     3     2
    -5     9    -1    47     4     6    -2     2
     3     0    -3     2     3    -2     0     4
     2    -3     6    -4     3     6     3     6
     5    11     5     6     0     3    -4     4
initialiamge =
    63   -34    49    10     7    13   -12     7
   -31    23    14   -13     3     4     6    -1
    15    14     3   -12     5    -7     3     9
    -9    -7   -14     8     4    -2     3     2
    -5     9    -1    47     4     6    -2     2
     3     0    -3     2     3    -2     0     4
     2    -3     6    -4     3     6     3     6
     5    11     5     6     0     3    -4     4
reconstructedimage =
    63   -34    49    10     7    13   -12     7
   -31    23    14   -13     3     4     6    -1
    15    14     3   -12     5    -7     3     9
    -9    -7   -14     8     4    -2     3     2
    -5     9    -1    47     4     6    -2     2
     3     0    -3     2     3    -2     0     4
     2    -3     6    -4     3     6     3     6
     5    11     5     6     0     3    -4     4
difference =
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
  Name                      Size               Bytes  Class     Attributes
A                         8x8                  512  double              
D                         1x20                  40  char                
DD                        1x8                 1496  cell                
N                        64x2                 1024  double    global    
RR                        8x8                  512  double              
S                         1x59                 472  double              
SS                        1x7              1049860  cell                
TC_1                      1x22                 176  double              
X                         8x8                  512  double              
X0                        8x8                  512  double              
XX                        8x8                  512  double              
XXX                     256x256             524288  double              
Y                       256x256              65536  uint8               
Y0                        1x8                   64  double              
Y1                        1x1                    8  double              
a                         1x1                    8  double              
b                         1x1                    8  double              
difference                8x8                  512  double              
i                         1x1                    8  double              
initialiamge              8x8                  512  double              
initialthreshold          1x1                    8  double              
j                         1x1                    8  double              
laststeplevel             1x1                    8  double              
level                     1x1                    8  double              
m                         1x1                    8  double              
map                     256x3                 6144  double              
n                         1x1                    8  double              
order                     1x1                    8  double              
reconstructedimage        8x8                  512  double              
significantlist           1x8              2100672  cell                
sub_list                 61x2                  976  double              
sublist                   0x0                    0  double              
threshold                 1x1                    8  double              
xx                        1x1                    8  double              
yy                        1x1                    8  double
>>
2 Comments
  Image Analyst
      
      
 on 9 Apr 2015
				Why did you post this? There is no question. Did you mean for this to go into the File Exchange instead of here? What are we supposed to do with this?
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
