%设置压缩比cr
cr = 0.5;
%读入并显示原始图像
I1 = imread('lena512.bmp');
figure(1);
imshow(I1);
%对图像进行FFT
I1 = double(I1);
fftcoe = blkproc(I1, [8 8], 'fft2(x)');
coevar = im2col(fftcoe, [8 8], 'distinct');
coe = coevar;
[y, ind] = sort(coevar);
[m, n] = size(coevar);
snum = 64 - 64*cr;
%舍去不重要的系数
for i = 1: n
coe(ind(1: snum), i) = 0;
end
B2 = col2im(coe, [8 8], [512 512], 'distinct');
%对子图像块进行IFFT获得各个子图像的复原图像,并显示压缩图像
I2 = blkproc(B2, [8 8], 'ifft2(x)');
figure(2);
imshow(I2, [ ]);
%计算圴方误差
%erms = erms(I1, I2)
e = double(I1) - double(I2);
[m, n] = size(e);
ERMS = sqrt(sum(e(:).^2)/(m*n))
% 设置压缩比,cr=0.5为2:1压缩;cr=0.1250为8:1压缩
cr = 0.125;
initialimage = imread('lena512.bmp');
initialimage = double(initialimage)/255;
figure();
subplot(121);
imshow(initialimage);
%对图像进行DCT变换
t = dctmtx(8);
dctcoe = blkproc(initialimage, [8 8], 'P1*x*P2', t, t');
%将DCT变换后的矩阵转换成列,并按升序排列
coevar = im2col(dctcoe, [8 8], 'distinct');
coe = coevar;
[y, ind] = sort(coevar);
[m, n] = size(coevar);
%舍去不重要的系数
snum = 64-64 * cr;
for i = 1:n
coe(ind(1:snum), i) = 0;
end
%把列变换为二维矩阵
b2 = col2im(coe, [8 8], [512 512], 'distinct');
%逆DCT变换
i2 = blkproc(b2, [8 8], 'P1*x*P2', t', t);
subplot(122);
imshow(i2);
e = double(initialimage)-double(i2);
[m, n] = size(e);
erms = sqrt(sum(e(:).^2)/(m*n))