0

i'm working on a function in matlab that calculates the inverse DCT on an image i dont know what is note working in my code after i execute i get a black image , I alredy did the DCT function and it works all i need now is the invert DCT

any ideas plz....

here is my main programme :

I=imread('illustration.JPG');
I=rgb2gray(I);
figure,
imshow(I);title('limage original en niveau de gris');
I=I(1:1600,1:1600);
figure,
imshow(I);title('coupure de limage utilisé pour que ces dimensions   sont    multiple de 8');
figure,
DCT=dct(I);
imshow(DCT);title('DCT1');%calcule de DCT avec la function predifinie dct
DCT2=DCTlocal(I);
figure,
imshow(DCT2);title('DCT2');%calcule de DCT avec l'algorithme données

figure,
Irec=idct(DCT);
imshow(Irec/255);title('Irec ');

figure,
Irec2=InvDCTLocale(DCT2);
imshow(Irec2);title('Irec2');

and here is my DCT function :

   function image_comp = DCTlocal( I )
   N=8;
   [n1,n2]=size(I);
    I=double(I)-128;
    block_dct = zeros(N);

    %loop true block
     for k=1:N:n1
     for l=1:N:n2
      %save true image
      current_block = I(k:k+N-1,l:l+N-1);
      %loop true cos(u,v)
       for u=0:N-1
        for v=0:N-1
          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; %loop true pixel values
         for x=0:N-1
          for y=0:N-1
            Res_sum = Res_sum +      ((current_block(x+1,y+1))*cos(((2*x)+1)*u*pi/(2*N))*cos(((2*y)+1)*v*pi/(2*N)));  
          end
         end
         dct = 1/sqrt(2*N) * Cu * Cv * Res_sum; %calculate DCT
        block_dct(u+1,v+1) = dct;
      end
     end
     image_comp(k:k+N-1,l:l+N-1) = block_dct(u+1,v+1);
    end
     end
    end

and now here is my iverse DCT function where the problem is :

      function image_decomp = InvDCTLocale( DCT )

      N=8;
      [n1,n2]=size(DCT);
      block_idct = zeros(N);

      %loop true block
       for k=1:N:n1
      for l=1:N:n2
      %save true image
      current_block = DCT(k:k+N-1,l:l+N-1);
      %loop true cos(u,v)
      for u=0:N-1
      for v=0:N-1
          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; %loop true pixel values
         for x=0:N-1
          for y=0:N-1
            Res_sum = Res_sum +Cu* Cv *   ((current_block(x+1,y+1))*cos(((2*x)+1)*u*pi/(2*N))*cos(((2*y)+1)*v*pi/(2*N)));  
          end
         end
         I = 1/sqrt(2*N)*Res_sum; 
        block_idct(u+1,v+1) = I;
      end
     end
     image_decomp(k:k+N-1,l:l+N-1) = block_idct(u+1,v+1);
 end
 end
 end
Imad Kby
  • 3
  • 3
  • what is output of your DCT/IDCT? my bet is you are truncating/rounding to integers something and or introduce scaling of colors for image conversion after DCT and do not scale back before IDCT. (too lazy to check your code) but if it helps you can compute (I)DCT from DFT for comparison see my [1D FDCT and IFDCT by FFT with the same length](http://stackoverflow.com/a/22779268/2521214) – Spektre Dec 21 '16 at 16:00
  • thanks ,for my otput in DCT function i get a similaire result as the in build function dct2 , but in the idct function i get a black image – Imad Kby Dec 21 '16 at 18:23
  • can i get the code in matlab pls ... – Imad Kby Dec 21 '16 at 18:33
  • not from me as I do not use Matlab at all ... – Spektre Dec 21 '16 at 21:11
  • What are the output values? Black just mean it is small enough number. for example if the IDCT output is in range `<0.0,1.0>` after converting to RGB or grayscale without any scaling you got pixels `0x00000000` to `0x00010101` (24bit RGB) which is still black ... try scaling the result before truncating with `x'=(x-min)*255/(max-min)` where `x` is original output cell value and `x'` is scaled one and `min,max` is the min and max value from whole output. – Spektre Dec 22 '16 at 08:15

0 Answers0