直接上代码
clc
clear
I = imread('Lena.tiff'); %输入灰度图像
I_D = im2double(I);
D = blkproc(I_D,[8,8],'dct2'); %分块DCT,blkproc相当于matlab中的Map
cover=[1 1 1 1 1 0 0 0 %直接用与计算丢掉低频
1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
X = blkproc(D,[8,8],'P1.*x',cover); %保留15个系数
IX = blkproc(X,[8,8],'idct2'); %DCT反变换得到压缩图像
subplot(1,2,1);imshow(I);title('原始图像');
subplot(1,2,2);imshow(IX);title('与计算压缩');
RGB = imread('Test.png');
R = RGB(:,:,1);
G = RGB(:,:,2);
B = RGB(:,:,3);
DR = blkproc(R,[8,8],'dct2');
DG = blkproc(G,[8,8],'dct2');
DB = blkproc(B,[8,8],'dct2');
colortable= [17 18 24 47 99 99 99 99 ;
18 21 26 66 99 99 99 99 ;
24 26 56 99 99 99 99 99 ;
47 66 99 99 99 99 99 99 ;
99 99 99 99 99 99 99 99 ;
99 99 99 99 99 99 99 99 ;
99 99 99 99 99 99 99 99 ;
99 99 99 99 99 99 99 99];
X1 = blkproc(DR,[8,8],'fix(x./P1)',colortable); %用JPEG标准量化表进行量化
I1 = blkproc(X1,[8,8],'idct2');
I1 = (I1 - min(reshape(I1,512*512,1)))./(max(reshape(I1,512*512,1))- min(reshape(I1,512*512,1)));
X2 = blkproc(DG,[8,8],'fix(x./P1)',colortable); %用JPEG标准量化表进行量化
I2 = blkproc(X2,[8,8],'idct2');
I2 = (I2 - min(reshape(I2,512*512,1)))./(max(reshape(I2,512*512,1))- min(reshape(I2,512*512,1)));
X3 = blkproc(DB,[8,8],'fix(x./P1)',colortable); %用JPEG标准量化表进行量化
I3 = blkproc(X3,[8,8],'idct2');
I3 = (I3 - min(reshape(I3,512*512,1)))./(max(reshape(I3,512*512,1))- min(reshape(I3,512*512,1)));
figure
subplot(1,2,1);imshow(RGB);title('原始图像');
subplot(1,2,2);imshow(cat(3,I1,I2,I3),[]);title('仿JPEG标准量化表压缩')