一、实验目的
1、掌握使用MATLAB进行DCT变换的方法。
二、实验内容
使用 MATLAB 实现图像的DCT变换与逆变换,使用8*8的子图像进行变换编码。
三、实验步骤
1、对图像进行DCT变换并观察频谱,对DCT变换系数进行截断并使用截断后的系数进行逆变换,比较原图像与恢复后的图像。
Matlab:
clear all;
close all;
I = imread('lena.jpg');
img_gray = im2double(rgb2gray(I));
img_dct2 = dct2(img_gray);
subplot(1,2,1);
imshow(img_gray),title('原始图像');
subplot(1,2,2);
imshow(img_dct2),title('DCT变换频谱');
clear all;
close all;
I = imread('lena.jpg');
img_gray = im2double(rgb2gray(I));
img_dct2 = dct2(img_gray);
subplot(1,2,1);
imshow(img_gray),title('原始图像');
[m,n] = size(img_dct2);
for i=1:m
for j=1:n
if abs(img_dct2(i,j))<0.1
img_dct2(i,j) = 0;
end
end
end
img_idct2 = idct2(img_dct2);
subplot(1,2,2);
imshow(img_idct2),title('恢复后的图像');
2、对图像进行变换编码与解码(只进行DCT变换与逆变换),分别使用区域编码与门限编码,比较原图像与恢复后的图像。
Matlab:
clear all;
close all;
I = imread('lena.jpg');
img_gray = im2double(rgb2gray(I));
subplot(2,2,1);
imshow(img_gray),title('原始图像lzz');
[m,n] = size(img_gray);
mask1 = zeros(8);
for i=1:3
for j=1:4-i
mask1(i,j) = 1;
end
end
I_restore1 = zeros(m,n);
for i=1:m-7
for j=1:n-7
temp = dct2(img_gray(i:i+7,j:j+7));
imgb = temp.*mask1;
I_restore1(i:i+7,j:j+7) = idct2(imgb);
end
end
subplot(2,2,2);
imshow(I_restore1),title('保留6个系数恢复后的图像lzz')
mask2 = zeros(8);
for i=1:5
for j=1:7-i
mask2(i,j) = 1;
end
end
I_restore2 = zeros(m,n);
for i=1:m-7
for j=1:n-7
temp = dct2(img_gray(i:i+7,j:j+7));
imgb = temp.*mask2;
I_restore2(i:i+7,j:j+7) = idct2(imgb);
end
end
subplot(2,2,3);
imshow(I_restore2),title('保留15个系数恢复后的图像lzz');
img_gray1 = double(rgb2gray(I));
mask3 = [16 11 10 16 24 40 51 61;12 12 14 19 26 58 60 55;14 13 16 24 40 57 69 56;14 17 22 29 51 87 80 62;18 22 37 56 68 109 103 77;24 35 55 64 81 104 113 92;49 64 78 87 103 121 120 101;72 92 95 98 112 100 103 99];
I_restore3 = zeros(m,n);
for i=1:m-7
for j=1:n-7
temp = dct2(img_gray1(i:i+7,j:j+7));
imgb = round(temp./mask3);
I_restore3(i:i+7,j:j+7) = idct2(imgb.*mask3);
end
end
subplot(2,2,4);
imshow(I_restore3,[]),title('使用亮度化矩阵量化恢复后的图像lzz');