Donnerstag, 14. März 2013

2-D Inverse Discrete Cosine Transform


Consider the result obtained after DCT. (Check 2d-DCT )
Apply Inverse Discrete Cosine Transform to obtain the original Image.




MATLAB CODE:

%2-D INVERSE DISCRETE COSINE TRANSFORM
%PREALLOCATE THE MATRIX
A=zeros(size(B));
Temp=zeros(size(B));
[M N]=size(B);

x=1:M;
x=repmat(x',1,N);
y=repmat(1:N,M,1);

figure,
imshow(log(abs(B)),[]);colormap(jet);title('After DCT');







for i=1:M
    for j = 1: N
   
        if(i==1)
          AlphaP=sqrt(1/M);
        else
          AlphaP=sqrt(2/M);
        end
       
        if(j==1)
          AlphaQ=sqrt(1/N);
        else
          AlphaQ=sqrt(2/N);
        end
                 
        cs1=cos((pi*(2*x-1)*(i-1))/(2*M));
        cs2=cos((pi*(2*y-1)*(j-1))/(2*N));
        Temp=B.*cs1.*cs2*AlphaP*AlphaQ;
              
          A(i,j)=sum(sum(Temp));
    end
end
%OUTPUT
figure,
imshow(abs(A),[0 255]);title('Image after IDCT');












Mittwoch, 13. März 2013

2-D Discrete Cosine Transform


DCT is a technique for converting a signal into elementary frequency components.
It is widely used in image compression. Check Inverse discrete cosine transform for the reverse process.





MATLAB CODE:



%CONSIDER A MATRIX


A=[4 2 9 60; 7 10 5 77;88 66 44 3];




%PREALLOCATE THE MATRICES


B=zeros(size(A));


Temp=zeros(size(A));


display(A);










%FIND THE SIZE OF THE
MATRIX


[M N]=size(A);




for i=1:M


    for j=1:N


       


        if(i==1)


          AlphaP=sqrt(1/M);


        else


          AlphaP=sqrt(2/M);


        end


       


        if(j==1)


          AlphaQ=sqrt(1/N);


        else


          AlphaQ=sqrt(2/N);


        end



For A(1,1),  m=1 and n=1. If m= 1 then AlphaP=sqrt(1/M)=0.5774
If n=1 then AlphaQ=sqrt(1/N)=0.5000


For A(2,3),m=1 and n=3. AlphaP=sqrt(2/M)=0.8165
 AlphaQ =  sqrt(2/N) = 0.7071











Temp=0;


       for x=1:M


            for y=1:N


                   cs1=cos((pi*(2*x-1)*(i-1))/(2*M));


                cs2=cos((pi*(2*y-1)*(j-1))/(2*N));


                Temp=Temp+(A(x,y)*cs1*cs2);


             end


        end


       B(i,j)=AlphaP*AlphaQ*Temp;


    end


end


%2-D discrete Cosine Transform




display(B);











EXAMPLE 2



%2-D DISCRETE COSINE TRANSFORM FOR AN IMAGE


%READ THE IMAGE
I=imread('coins.png');
I=I(90:150,140:210);



A=double(I);

%PREALLOCATE THE MATRIX
B=zeros(size(A));
Temp=zeros(size(A));
[M N]=size(A);


%
x=1:M;
x=repmat(x',1,N);
y=repmat(1:N,M,1);


for i=1:M
    for j = 1: N
   
        if(i==1)
          AlphaP=sqrt(1/M);
        else
          AlphaP=sqrt(2/M);
        end
       
        if(j==1)
          AlphaQ=sqrt(1/N);
        else
          AlphaQ=sqrt(2/N);
        end
       
             
               
        cs1=cos((pi*(2*x-1)*(i-1))/(2*M));
        cs2=cos((pi*(2*y-1)*(j-1))/(2*N));
        Temp=A.*cs1.*cs2;
              
      
        B(i,j)=AlphaP*AlphaQ*sum(sum(Temp));
    end
end

%OUTPUT
figure,
subplot(2,1,1);imshow(I);title('Original Image');
subplot(2,1,2),imshow(log(abs(B)),[]);
colormap(jet);title('After DCT');