1、 实验四 频域滤波与图像变换编码 实验目的 通过实验了解频域高频和低频滤波器对图像处理的效果,了解离散余弦变换在图像变换编码中的作用。 1.载入图像’cameraman.tif’,加入椒盐噪声,编程设计一阶巴特沃斯低通滤波器,改变滤波器的参数,观察并比较滤波效果。 close all; clear all; I1=imread('pout.tif'); subplot(2,3,1), imshow(I1); title('原始图像') I2=imnoise(I1,'salt & pepper'); subplot(2,3,2) imshow(I2); title(
2、'加噪图像'); f=double(I2); g=fft2(f); g=fftshift(g); [N1,N2]=size(g); n=1; d0=5; n1=fix(N1/2); n2=fix(N2/2); for i=1:N1 for j=1:N2 d=sqrt((i-n1)^2+(j-n2)^2); h=1/(1+0.414*(d/d0)^(2*n)); result(i,j)=h*g(i,j); end end result=ifftshift(result); X2=ifft2(res
3、ult); X3=uint8(real(X2)); subplot(2,3,3), imshow(X3); title('Butterworth 低通滤波器,d0=5'); d0=11; n1=fix(N1/2); n2=fix(N2/2); for i=1:N1 for j=1:N2 d=sqrt((i-n1)^2+(j-n2)^2); h=1/(1+0.414*(d/d0)^(2*n)); result(i,j)=h*g(i,j); end end result=ifftshift(result
4、); X2=ifft2(result); X3=uint8(real(X2)); subplot(2,3,4), imshow(X3); title('d0=11'); d0=25 n1=fix(N1/2); n2=fix(N2/2); for i=1:N1 for j=1:N2 d=sqrt((i-n1)^2+(j-n2)^2); h=1/(1+0.414*(d/d0)^(2*n)); result(i,j)=h*g(i,j); end end result=ifftshift(result);
5、 X2=ifft2(result); X3=uint8(real(X2)); subplot(2,3,5), imshow(X3); title('d0=25'); d0=50 n1=fix(N1/2); n2=fix(N2/2); for i=1:N1 for j=1:N2 d=sqrt((i-n1)^2+(j-n2)^2); h=1/(1+0.414*(d/d0)^(2*n)); result(i,j)=h*g(i,j); end end result=ifftshift(result);
6、X2=ifft2(result); X3=uint8(real(X2)); subplot(2,3,6), imshow(X3); title('d0=50'); 由图可知,由于对噪声模型的估计不准确,使用巴特沃斯滤波器在平滑了噪声的同时,也使图像模糊了,随着截断频率的增加,图像的模糊程度减小,滤除噪声的效果也越来越差。 2.载入图像’trees.tif’,编程设计二阶巴特沃斯高通滤波器,观察到图像中各区域边界得到增强,而图中原来比较平滑区域内部的灰度动态范围被压缩,整幅图变得比较昏暗。 close all; clear all; I1=imread('trees.
7、tif'); subplot(1,2,1), imshow(I1); title('原始图像'); f=double(I1); g=fftshift(fft2(f)); [N1,N2]=size(g); n=2;d0=5; n1=fix(N1/2); n2=fix(N2/2); for i=1:N1 for j=1:N2 d=sqrt((i-n1)^2+(j-n2)^2); if d==0 h==0; else h=1/(1+(d0/d)^(2*n)); end
8、 result(i,j)=h*g(i,j); end end result=ifftshift(result); X2=ifft2(result); X3=uint8(real(X2)); subplot(1,2,2), imshow(X3); title('Butterworth 高通滤波器'); 从高通滤波器后的图像可见,图像昏暗,很多细节看不清了,这是由于图像的大部分能量集中在低频区域,而高频使图中各区域的边界得到较明显增强的同时滤掉了低频成分,使图中原来比较平滑区域内部的灰度动态范围被压缩,整幅图变得比较昏暗。 3.载入图像,将图像划分成8
9、8的图象块,计算它们的DCT系数,并且只保留64个DCT系数中的10个,然后对每个图象块利用这10个系数进行逆DCT变换来重构图象。 close all; clear all; I=imread('logo.tif'); I=im2double(I); T=dctmtx(8); B=blkproc(I,[8 8],'P1*x*P2',T,T'); mask=[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]; B2=blkproc(B,[8 8],'P1.*x',mask); I2=blkproc(B2,[8 8],'P1*x*P2',T',T); subplot(1,2,1),imshow(I); title('原始图像'); subplot(1,2,2),imshow(I2); title('经压缩解压后的图像')






