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');





















Keine Kommentare:

Kommentar veröffentlichen