Monday, October 29, 2007

Matlab code:Use DCT for image compression

Just show the idea for gray level image. RGB image is in similar way. It takes the following steps:
  1. DCT conversion and create a 1D matrix
  2. Sort elements of the matrix from the highest to the smallest and get the indices list
  3. Choose the highest N elements
  4. Inverse DCT
Matlab code:
dvalue=imread('map.bmp');
dvalue = double(dvalue)/255;
dvalue = rgb2gray(dvalue);
img_dct=dct2(dvalue);
img_pow=(img_dct).^2;
img_pow=img_pow(:);
[B,index]=sort(img_pow);%no zig-zag
B=flipud(B);
index=flipud(index);
compressed_dct=zeros(size(dvalue));
coeff = 20000;% maybe change the value
for k=1:coeff
compressed_dct(index(k))=img_dct(index(k));
end
im=idct2(compressed_dct);
imwrite(im, 'maptemp2.bmp')

24 comments:

  1. or use 'im2double' to change unit8 image to double

    ReplyDelete
  2. Anonymous5:48 AM

    i think the flipud are not required
    use instead sort(var,'descend');

    ReplyDelete
  3. Anonymous5:48 AM

    i think the flipud are not required
    use instead sort(var,'descend');

    ReplyDelete
  4. Anonymous4:27 AM

    is it the entire code for implementing dct for video compression........
    reply on sargam86@gmail.com

    ReplyDelete
  5. Anonymous1:52 AM

    do you anyone have matlab code for shape adaptive discrete cosine transform????

    mail me on vairam_mit@yahoo.co.in

    ReplyDelete
  6. Anonymous7:31 PM

    will try this code..
    thank you^^

    ReplyDelete
  7. hi i need a code for feature extraction usin DCT for fingerprint recognition

    ReplyDelete
  8. i need dct code for vedio compression........
    plz mail it on tshank30@gmail.com
    thanks...

    ReplyDelete
  9. Anonymous5:43 AM

    I want to extract DCT coefficient for an image in matlab?? how to do that???

    ReplyDelete
  10. Anonymous5:25 PM

    I am navya,

    If you have mini project full source code,document please mail me friend.
    my mail id was navyanali64@gmail.com.

    Kindly please reply me
    thanks

    ReplyDelete
    Replies
    1. hi navya if u want mini matlab project then mail me

      deepeshrawat987@gmail.com

      or visit

      http://deepeshrawat987.blogspot.in/

      Delete
  11. Anonymous5:05 AM

    do anyone knw the code for image compression and decompression.. plss mail me friends

    my mail id swathireddi7@gmail.com

    do reply me

    ReplyDelete
  12. Anonymous8:54 AM

    I need code for Mp3 compression using MDCT

    ReplyDelete
  13. Anonymous8:57 AM

    I need code for Mp3 compression using MDCT.Plz mail me friends.My mail ID is induks15@gmail.com

    ReplyDelete
  14. anyone can send code for 2d-dct in matlab pls its very urgent

    ReplyDelete
  15. Anonymous5:57 AM

    i need dct matlab code for image compression.. plz mail it on aartigdverma@yahoo.com

    ReplyDelete
  16. i need dct matlab code..plz mail it to kshahid000@gmail.com

    ReplyDelete
    Replies
    1. clear all;
      p=imread('arez.jpg');
      a=imresize(p,[150,150]);


      %DCT
      Z(:,:,1)=dct2(a(:,:,1));
      Z(:,:,2)=dct2(a(:,:,2));
      Z(:,:,3)=dct2(a(:,:,3));
      for i=1:100
      for j=1:100
      if((i+j)>60)
      Z(i,j,1)=0;
      Z(i,j,2)=0;
      Z(i,j,3)=0;
      end
      end
      end
      K(:,:,1)=idct2(Z(:,:,1));
      K(:,:,2)=idct2(Z(:,:,2));
      K(:,:,3)=idct2(Z(:,:,3));
      subplot(2,4,1);
      %imshow(Z);
      imshow(uint8(K));
      title('70% compression DCT');
      Z(:,:,1)=dct2(a(:,:,1));
      Z(:,:,2)=dct2(a(:,:,2));
      Z(:,:,3)=dct2(a(:,:,3));
      for i=1:100
      for j=1:100
      if((i+j)>100)
      Z(i,j,1)=0;
      Z(i,j,2)=0;
      Z(i,j,3)=0;
      end
      end
      end
      K(:,:,1)=idct2(Z(:,:,1));
      K(:,:,2)=idct2(Z(:,:,2));
      K(:,:,3)=idct2(Z(:,:,3));
      subplot(2,4,2);
      %imshow(Z);
      imshow(uint8(K));
      title('50% compression DCT');
      Z(:,:,1)=dct2(a(:,:,1));
      Z(:,:,2)=dct2(a(:,:,2));
      Z(:,:,3)=dct2(a(:,:,3));
      for i=1:100
      for j=1:100
      if((i+j)>140)
      Z(i,j,1)=0;
      Z(i,j,2)=0;
      Z(i,j,3)=0;
      end
      end
      end
      K(:,:,1)=idct2(Z(:,:,1));
      K(:,:,2)=idct2(Z(:,:,2));
      K(:,:,3)=idct2(Z(:,:,3));
      subplot(2,4,3);
      %imshow(Z);
      imshow(uint8(K));
      title('30% compression DCT');
      Z(:,:,1)=dct2(a(:,:,1));
      Z(:,:,2)=dct2(a(:,:,2));
      Z(:,:,3)=dct2(a(:,:,3));
      for i=1:100
      for j=1:100
      if((i+j)>180)
      Z(i,j,1)=0;
      Z(i,j,2)=0;
      Z(i,j,3)=0;


      end
      end
      end


      K(:,:,1)=idct2(Z(:,:,1));
      K(:,:,2)=idct2(Z(:,:,2));
      K(:,:,3)=idct2(Z(:,:,3));
      subplot(2,4,4);
      %imshow(Z);
      imshow(uint8(K));
      title('10% compression DCT');

      Delete
  17. Anonymous1:44 AM

    Hi, I need dct matlab code for image compression.. plz mail it on "ahmadelect@gmail.com" ; Thanks

    ReplyDelete
    Replies
    1. clear all;
      p=imread('arez.jpg');
      a=imresize(p,[150,150]);


      %DCT
      Z(:,:,1)=dct2(a(:,:,1));
      Z(:,:,2)=dct2(a(:,:,2));
      Z(:,:,3)=dct2(a(:,:,3));
      for i=1:100
      for j=1:100
      if((i+j)>60)
      Z(i,j,1)=0;
      Z(i,j,2)=0;
      Z(i,j,3)=0;
      end
      end
      end
      K(:,:,1)=idct2(Z(:,:,1));
      K(:,:,2)=idct2(Z(:,:,2));
      K(:,:,3)=idct2(Z(:,:,3));
      subplot(2,4,1);
      %imshow(Z);
      imshow(uint8(K));
      title('70% compression DCT');
      Z(:,:,1)=dct2(a(:,:,1));
      Z(:,:,2)=dct2(a(:,:,2));
      Z(:,:,3)=dct2(a(:,:,3));
      for i=1:100
      for j=1:100
      if((i+j)>100)
      Z(i,j,1)=0;
      Z(i,j,2)=0;
      Z(i,j,3)=0;
      end
      end
      end
      K(:,:,1)=idct2(Z(:,:,1));
      K(:,:,2)=idct2(Z(:,:,2));
      K(:,:,3)=idct2(Z(:,:,3));
      subplot(2,4,2);
      %imshow(Z);
      imshow(uint8(K));
      title('50% compression DCT');
      Z(:,:,1)=dct2(a(:,:,1));
      Z(:,:,2)=dct2(a(:,:,2));
      Z(:,:,3)=dct2(a(:,:,3));
      for i=1:100
      for j=1:100
      if((i+j)>140)
      Z(i,j,1)=0;
      Z(i,j,2)=0;
      Z(i,j,3)=0;
      end
      end
      end
      K(:,:,1)=idct2(Z(:,:,1));
      K(:,:,2)=idct2(Z(:,:,2));
      K(:,:,3)=idct2(Z(:,:,3));
      subplot(2,4,3);
      %imshow(Z);
      imshow(uint8(K));
      title('30% compression DCT');
      Z(:,:,1)=dct2(a(:,:,1));
      Z(:,:,2)=dct2(a(:,:,2));
      Z(:,:,3)=dct2(a(:,:,3));
      for i=1:100
      for j=1:100
      if((i+j)>180)
      Z(i,j,1)=0;
      Z(i,j,2)=0;
      Z(i,j,3)=0;


      end
      end
      end


      K(:,:,1)=idct2(Z(:,:,1));
      K(:,:,2)=idct2(Z(:,:,2));
      K(:,:,3)=idct2(Z(:,:,3));
      subplot(2,4,4);
      %imshow(Z);
      imshow(uint8(K));
      title('10% compression DCT');

      Delete
  18. Anonymous9:13 AM

    I am doing project in video compression... I had a code but i got an error..Anyone help me to rectify the error in the code..If anyone interested pls contact me in this id angelicammu90@gmail.com

    ReplyDelete
  19. i need dct matlab code for medical image compression.. plz mail it on ismail.maulood@gmail.com

    ReplyDelete
  20. for matlab image mini project such as noise filter visit

    http://deepeshrawat987.blogspot.in/

    ReplyDelete