title: DCT of an image
body: <p>I work on a function in Matlab that calculates the DCT (discrete cosine transform) of an image. I don't know what is not working in my code,which is used in image compression.please help me.
  Any ideas please.</p>
  
  <pre><code>   clc;close all;clear all;
     image=('cameraman.tif');
      [h w] = size(image);
      image = double(image) - 128;
      b=8;
      block = zeros(b,b);
  
   image_t=zeros(size(image));
   for k=1:b:h
       for l=1:b:w
          image_t(k:k+b-1,l:l+b-1)= image(k:k+b-1,l:l+b-1);
          for u=1:b
              for v=1:b
                  if u == 0
                      Cu = 1/sqrt(2);
                  else
                      Cu = 1;
                  end
                  if v == 0
                      Cv = 1/sqrt(2);
                  else
                      Cv = 1;
                  end
                  Res_sum=0;
                  for x=1:b;
                      for y=1:b
                          Res_sum = Res_sum + ((image_t(x,y))*cos(((2*x)+1)*u*pi/(2*b))*cos(((2*y)+1)*v*pi/(2*b)));  
                      end
                  end
                  dct= (1/4)*Cu*Cv*Res_sum;
                  block(u,v) = dct;
  
              end
          end
          image_comp(k:k+b-1,l:l+b-1)=block(u,v);
       end
   end
  end
  </code></pre>