影像壓縮編碼碼matlab實現——變換編碼

fpga&matlab發表於2020-12-24

%設定壓縮比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))
 

相關文章