RenHao

20150604_JPEG_compression

Jun 4th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.35 KB | None | 0 0
  1. close all
  2. clear all
  3. x = [ 52    55    61    66    70    61    64    73 ;
  4.  63    59    55    90    109    85    69    72 ;
  5.  62    59    68    113    144    104    66    73 ;
  6.  63    58    71    122    154    106    70    69 ;
  7.  67    61    68    104    126    88    68    70 ;
  8.  79    65    60    70    77    68    58    75 ;
  9.  85    71    64    59    55    61    65    83 ;
  10.  87    79    69    68    65    76    78    94
  11.  ];
  12.  
  13. % each entry in x falls in range [0,255] , so subtracting the midpoint 128 to modify the range to [-128,127]
  14.  g = x-128*ones(size(x));
  15.  
  16.  N=8;
  17. G=zeros(N,N,N,N);
  18. GG=zeros(N,N);
  19. alpha_u=1;
  20. alpha_v=1;
  21.  
  22. %2D DCT
  23. for u = 0:N-1
  24.     for v = 0:N-1
  25.         for x = 0:N-1
  26.             for y = 0:N-1
  27.                
  28.                 G(u+1,v+1,x+1,y+1) = 0.25* ((u~=0)+(u==0)/sqrt(2))* ((v~=0)+(v==0)/sqrt(2)) * g(x+1,y+1) * cos(pi*u*(2*x+1/2)/N) * cos(pi*v*(2*y+1/2)/N) ;
  29.            
  30.             end
  31.         end
  32.         GG(u+1,v+1) = sum(sum(G(u+1,v+1,:,:),3),4)
  33.     end
  34. end
  35.  
  36. %Quantization matrix
  37. %This allows one to greatly reduce the amount of information in the high frequency components.
  38. %This is done by simply dividing each component in the frequency domain by a constant for that component, and then rounding to the nearest integer
  39.  
  40. Q=[16   11   10   16   24   40   51   61 ;
  41.  12   12   14   19   26   58   60   55 ;
  42.  14   13   16   24   40   57   69   56 ;
  43.  14   17   22   29   51   87   80   62 ;
  44.  18   22   37   56   68   109   103   77 ;
  45.  24   35   55   64   81   104   113   92 ;
  46.  49   64   78   87   103   121   120   101 ;
  47.  72   92   95   98   112   100   103   99]
  48.  
  49. % rounds each element of X to the nearest integer
  50. B=round(GG./Q)
  51.  
  52.  
  53. %%%%%%%%%%%%%%%%%%%%%%%%%
  54. %%%%%%%% REVERSE %%%%%%%%
  55. %%%%%%%%%%%%%%%%%%%%%%%%%
  56. XX = B.*Q
  57.  
  58. %N=8;
  59. X=zeros(N,N,N,N);
  60. x=zeros(N,N);
  61.  
  62. for i = 0:N-1
  63.     for j = 0:N-1
  64.         for u = 0:N-1
  65.             for v = 0:N-1
  66.                
  67.                 X(i+1,j+1,u+1,v+1) = 0.25* ((u~=0)+(u==0)/sqrt(2))* ((v~=0)+(v==0)/sqrt(2)) * XX(u+1,v+1) * cos(pi*u*(2*i+1/2)/N) * cos(pi*v*(2*j+1/2)/N) ;
  68.            
  69.             end
  70.         end
  71.         x(i+1,j+1) = sum(sum(X(i+1,j+1,:,:),3),4);
  72.     end
  73. end
  74.  
  75. x
  76.  
  77. uc = zeros(N,N);
  78. uc = x+128
  79.  
  80. er = f - uc
  81. ab_er = abs(er)
  82. point_er = 0;
  83.  
  84. for i = 0:N-1
  85.     for j = 0:N-1
  86.         point_er = point_er + ab_er(i+1,j+1)
  87.     end
  88. end
  89. point_er/64
Advertisement
Add Comment
Please, Sign In to add comment