Here we take a file and then compress the file applying DCT and FFT on the image. The compression is done
1. 70%
2. 50%
3. 30%
4. 10%.
The Code is like this :
1. 70%
2. 50%
3. 30%
4. 10%.
The Code is like this :
clear all; p=imread('kaka.jpg'); a=imresize(p,[100,100]); %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'); 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'); 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'); 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'); %FFT Z(:,:,1)=fft2(a(:,:,1)); Z(:,:,2)=fft2(a(:,:,2)); Z(:,:,3)=fft2(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)=ifft2(Z(:,:,1)); K(:,:,2)=ifft2(Z(:,:,2)); K(:,:,3)=ifft2(Z(:,:,3)); subplot(2,4,5); %imshow(Z); imshow(uint8(K)); title('70% compression FFT'); Z(:,:,1)=fft2(a(:,:,1)); Z(:,:,2)=fft2(a(:,:,2)); Z(:,:,3)=fft2(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)=ifft2(Z(:,:,1)); K(:,:,2)=ifft2(Z(:,:,2)); K(:,:,3)=ifft2(Z(:,:,3)); subplot(2,4,6); %imshow(Z); imshow(uint8(K)); title('50% compression FFT'); Z(:,:,1)=fft2(a(:,:,1)); Z(:,:,2)=fft2(a(:,:,2)); Z(:,:,3)=fft2(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)=ifft2(Z(:,:,1)); K(:,:,2)=ifft2(Z(:,:,2)); K(:,:,3)=ifft2(Z(:,:,3)); subplot(2,4,7); %imshow(Z); imshow(uint8(K)); title('30% compression FFT'); Z(:,:,1)=fft2(a(:,:,1)); Z(:,:,2)=fft2(a(:,:,2)); Z(:,:,3)=fft2(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)=ifft2(Z(:,:,1)); K(:,:,2)=ifft2(Z(:,:,2)); K(:,:,3)=ifft2(Z(:,:,3)); subplot(2,4,8); %imshow(Z); imshow(uint8(K)); title('10% compression FFT');
OUTPUT :
OUTPUT : compressed image |
Original Image |
o = imread ('off stuff(F:):\downloads:\tat.jpg');
ReplyDeletew = size (o, 2);
samplesHalf = floor(w / 2);
samplesQuarter = floor(w / 4);
samplesEighth = floor(w / 8);
ci2 = [];
ci4 = [];
ci8 = [];
for k = 1:3% all color layers: RGB
for i = 1:size(o, 1)% all rows
rowDCT = dct(double(o(i,:,k)));
ci2(i,:,k) = idct(rowDCT(1:samplesHalf), w);
ci4(i,:,k) = idct(rowDCT(1:samplesQuarter), w);
ci8(i,:,k) = idct(rowDCT(1:samplesEighth), w);
end
end
h = size(o, 1);
samplesHalf = floor(h / 2);
samplesQuarter = floor(h / 4);
samplesEighth = floor(h / 8);
ci2f = [];
ci4f = [];
ci8f = [];
for k=1:3% all color layers:RGB
for i=1:size(o, 2)% all columnsJECT:
IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
columnDCT2=dct(double(ci2(:,i,k)));
columnDCT4=dct(double(ci4(:,i,k)));
columnDCT8=dct(double(ci8(:,i,k)));
ci2f(:,i,k) = idct(columnDCT2(1:samplesHalf), h);
ci4f(:,i,k) = idct(columnDCT4(1:samplesQuarter), h);
ci8f(:,i,k) = idct(columnDCT8(1:samplesEighth), h);
end
end
subplot(2,2,1), image(uint8(o)), title('Original Image');
subplot(2,2,2), image(uint8(ci2)), title('Compression Factor 2');
subplot(2,2,3), image(uint8(ci4)), title('Compression Factor 4');
subplot(2,2,4), image(uint8(ci8)), title('Compression Factor 8');
figure
subplot(2,2,1), image(uint8(o)), title('Original Image');
subplot(2,2,2), image(uint8(ci2f)), title('Compression Factor 2 * 2');
subplot(2,2,3), image(uint8(ci4f)), title('Compression Factor 4 * 4');
subplot(2,2,4), image(uint8(ci8f)), title('Compression Factor 8 * 8');
can u just guide what is mistake in this code for image compression using dct and how to correct this
no mistake it is working 100%
ReplyDelete