实验八 图像的分解与合成
进行DCT,对变换系数进行区域选择,然后进行IDCT.分解和重建原图。
实验步骤:进行DCT,对变换系数进行区域选择,然后进行IDCT.分解和重建原图。
python代码见附录
%对图像进行分块的DCT变换
% 先将图片进行8*8分块,对每一块进行DCT变换。保留不同数量和部位的DCT系数,对比高频,低频系数对图像质量的影响;
% 将多个块分别反DCT,恢复原图,进行对比
x=imread('Jellyfish.jpg');
x=rgb2gray(x);
x=im2double(x);
t=dctmtx(8);
y = blkproc(x, [8 8], 'P1 * x * P2', t, t');
figure(1);imshow(log(abs(y)),[]);title('DCT系数');
mask1=[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
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];%保留了3个
mask2=[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
0 0 0 0 0 0 0 0];%保留了10
mask3=[1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 0
1 1 1 1 1 1 0 0
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];%保留了36
mask4=[ 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1];%保留了全部
mask5=[ 0 0 1 1 1 1 1 1
0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1];%保留了右下64-3
mask6=[ 0 0 0 0 1 1 1 1
0 0 0 1 1 1 1 1
0 0 1 1 1 1 1 1
0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1];%保留了右下64-10
mask7=[ 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 1
0 0 0 0 0 1 1 1
0 0 0 0 1 1 1 1
0 0 0 1 1 1 1 1
0 0 1 1 1 1 1 1
0 1 1 1 1 1 1 1];%保留了右下64-36
y1=blkproc(y,[8 8],'P1.*x',mask1);
y2=blkproc(y,[8 8],'P1.*x',mask2);
y3=blkproc(y,[8 8],'P1.*x',mask3);
y4=blkproc(y,[8 8],'P1.*x',mask4);
y5=blkproc(y,[8 8],'P1.*x',mask5);
y6=blkproc(y,[8 8],'P1.*x',mask6);
y7=blkproc(y,[8 8],'P1.*x',mask7);
y11=blkproc(y1,[8 8],'P1*x*P2',t,t);
y22=blkproc(y2,[8 8],'P1*x*P2',t',t);
y33=blkproc(y3,[8 8],'P1*x*P2',t',t);
y44=blkproc(y4,[8 8],'P1*x*P2',t',t);
y55=blkproc(y5,[8 8],'P1*x*P2',t',t);
y66=blkproc(y6,[8 8],'P1*x*P2',t',t);
y77=blkproc(y7,[8 8],'P1*x*P2',t',t);
figure(2);subplot(2,4,1),imshow(x),title('原图');
figure(2);subplot(2,4,2),imshow(y11),title('还原保留左上3');
figure(2);subplot(2,4,3),imshow(y22),title('还原保留左上10');
figure(2);subplot(2,4,4),imshow(y33),title('还原保留左上36');
figure(2);subplot(2,4,5),imshow(y44),title('还原全部');
figure(2);subplot(2,4,6),imshow(y55),title('还原保留右下63-3');
figure(2);subplot(2,4,7),imshow(y66),title('还原保留右下63-10');
figure(2);subplot(2,4,8),imshow(y77),title('还原保留右下63-36');
end