数字图像处理-图像复原

实验内容

利用ROI技术,选择一个合适的区域,判断noisyImage1.bmp和noisyImage2.bmp的噪声类型,选择合适的方法对图像进行复原,分别计算复原前后图像的峰值信噪比psnr。(没有被噪声污染的图像是originalImage.bmp)
clc,clear all;
%读取照片
f =im2double(imread('originalImage.bmp'));
f1=im2double(imread('noisyImage1.bmp'));
f2=im2double(imread('noisyImage2.bmp'));
 
% 处理noisyImage1.bmp
figure(1);
imshow(f1)
title('noisyImage1.bmp' ,'FontWeight', 'Bold', 'FontSize', 20);
figure(2);
annotation('textbox', [0.1 0.1 0.2 0.1], 'String', '请用鼠标绘制noisyImage1.bmp图片ROI区域','FitBoxToText', 'on', 'EdgeColor', 'none','Color', 'red');
[B, c, r]=roipoly(f1);%交互生成ROI
figure(3);
subplot(1,2,1);
imshow(B);
title('noisyImage1.bmp交互的生成ROI','FontWeight', 'Bold', 'FontSize', 20);
h=imhist(f1(B));
subplot(1,2,2);
bar(h,'FaceColor', 'b')
title('noisyImage1.bmp的ROI的直方图','FontWeight', 'Bold', 'FontSize', 20);
 
%处理noisyImage2.bmp
figure(4);
imshow(f2);
title('noisyImage2.bmp');
figure(5);
annotation('textbox', [0.1 0.1 0.2 0.1], 'String', '请用鼠标绘制noisyImage2.bmp图片ROI区域','FitBoxToText', 'on', 'EdgeColor', 'none','Color', 'red');
[B1, c1, r1]=roipoly(f2);%交互生成ROI
figure(6);
subplot(1,2,1);
imshow(B1);
title('noisyImage2.bmp交互生成ROI','FontWeight', 'Bold', 'FontSize', 20)
h1=imhist(f2(B))
subplot(1,2,2);
bar(h1,'FaceColor', 'r')
title('noisyImage2.bmp的ROI的直方图','FontWeight', 'Bold', 'FontSize', 20)
 
figure(7);
subplot(2,3,1);
imshow(f);
title('原图像','FontWeight', 'Bold', 'FontSize', 20)
subplot(2,3,2);
imshow(f1);
psnr_f1 = psnr(f, f1);
% text(0, 30, ['复原前图片图像信噪比峰值: ', num2str(psnr_f1), 'dB'], 'Color', 'red','FontSize', 14,'FontWeight', 'bold');
title(sprintf('noisyImage1 psnr=%0.4f', psnr_f1),'FontWeight', 'Bold', 'FontSize', 20);
 
 
meanFilter = fspecial('average', [3 3]);
F1_1= imfilter(f1, meanFilter, 'replicate');
subplot(2,3,4);
imshow(F1_1);
psnr_F1_1 = psnr(double(f), double(F1_1));
title(sprintf('均值滤波处理  psnr=%0.4f',psnr_F1_1),'FontWeight', 'Bold', 'FontSize', 20);
 
gaussianFilter = fspecial('gaussian', [3 3],  1.5);
F1_2 = imfilter(f1, gaussianFilter, 'replicate');
subplot(2,3,5);
imshow(F1_2);
psnr_F1_2= psnr(f, F1_2);
title(sprintf('高斯低通滤波处理  psnr=%0.4f',psnr_F1_2),'FontWeight', 'Bold', 'FontSize', 20);
 
 
F1_3=spfilt(f1,'gmean',3,3);
subplot(2,3,6);
imshow(F1_3);
title('几何均值滤波处理','FontWeight', 'Bold', 'FontSize', 20);
% psnr_F1_3= psnr(double(f), double(F1_3));
 
 
figure(8);
subplot(2,3,1);
imshow(f);
title('原图像','FontWeight', 'Bold', 'FontSize', 20)
subplot(2,3,2);
imshow(f2);
psnr_g1 = psnr(double(f),double(f2));
title(sprintf('noisyImage2 psnr=%0.4f', psnr_g1),'FontWeight', 'Bold', 'FontSize', 20);
 
 
meanFilter1 = fspecial('average', [3 3]);
G1_1= imfilter(f2, meanFilter1, 'replicate');
subplot(2,3,4);
imshow(G1_1);
psnr_G1_1 = psnr(double(f), double(G1_1));
title(sprintf('均值滤波处理  psnr=%0.4f',psnr_G1_1),'FontWeight', 'Bold', 'FontSize', 20);
 
gaussianFilter1 = fspecial('gaussian', [3 3],  2);
G1_2 = imfilter(f2, gaussianFilter1, 'replicate');
subplot(2,3,5);
imshow(G1_2);
psnr_G1_2= psnr(double(f), double(G1_2));
title(sprintf('高斯低通滤波处理  psnr=%0.4f',psnr_G1_2),'FontWeight', 'Bold', 'FontSize', 20);
 
G1_3=spfilt(f2,'gmean',3,3);
subplot(2,3,6);
imshow(G1_3);
title('几何均值滤波处理','FontWeight', 'Bold', 'FontSize', 20);
% psnr_G1_3= psnr(uint8(f), uint8(G1_3));

实验结果:

通过选取感兴趣的ROI区域,对图像进行直方图分析,可以看出第一个噪声图像的噪声类型是高斯噪声,第二个噪声图像的噪声类型为均匀分布噪声。通过对图像进行均值滤波、高斯低通滤波、几何均值滤波进行滤波处理以及对比复原前后图像的峰值信噪比psnr,可以看出高斯低通滤波比均值滤波效果会好一点,但是几何均值滤波效果最好,对于其他滤波方法,几何均值滤波能更好地保留图像的细节和边缘。但是图像有一定的模糊。

noisyImage1.bmp处理:

复原前图片psnr=18.4871

均值滤波处理psnr=20.9134

高斯低通滤波psnr=21.1912

noisyImage2.bmp处理:

复原前图片psnr=18.7602

均值滤波处理psnr=20.9578

高斯低通滤波psnr=21.1133

分析noisyImage3.bmp的幅度谱,判断图像被什么类型的噪声污染,分析噪声的相关参数。基于噪声类型和参数的估计,选择合适的频域滤波器对图像进行复原。
clc,clear all;
f=im2double(imread('noisyImage3.bmp'));%读取图片
[M,N]=size(f);
figure;
subplot(2,3,1);
imshow(f);
title('noisyImage3.bmp','FontWeight', 'Bold', 'FontSize', 24);
 
F=fft2(f);%傅里叶变换
f1=fftshift(F);%对图像频谱进行移动,使0频率点在中心
fa=abs(f1);%获取幅度谱
subplot(2,3,2);
imshow(log(1+fa),[]);%幅度谱
title('noisyImage3.bmp的幅度谱','FontWeight', 'Bold', 'FontSize', 24);
 
D0=128;W=16;n=2;%巴特沃兹带阻滤波器
H = BBRF(M,N,D0,W,n);%频域滤波器系数H
G=F.*ifftshift(H);
g=real(ifft2(G));%空域
subplot(2,3,3);
imshow(H,[]);
title('巴特沃思带阻滤波器','FontWeight', 'Bold', 'FontSize', 24);
 
subplot(2,3,4);
imshow(g);
title('滤波结果','FontWeight', 'Bold', 'FontSize', 24);
 
filter_g=fft2(g);%傅里叶变换
f2=fftshift(filter_g);%对图像频谱进行移动,使0频率点在中心
fb=abs(f2);%获取幅度谱
subplot(2,3,5);
imshow(log(1+fb),[]);%幅度谱
title('滤波图像的幅度谱','FontWeight', 'Bold', 'FontSize', 24);
 
figure
z1=ifftshift(log(1+fa));
subplot(2,1,1);
plot(z1(1,1:250),'r');%原图的频谱的曲线
title('原图的频谱的曲线','FontWeight', 'Bold', 'FontSize', 24);
subplot(2,1,2);
z2=ifftshift(log(1+fb));
plot(z2(1,1:250),'b');%滤波后图像的频域曲线
title('滤波后图像的频域曲线','FontWeight', 'Bold', 'FontSize', 24);

实验结果:

通过观察图像的幅度谱,可以看到图像的幅度谱在高频处出现极亮点,可判断图像被周期噪声污染。采用巴特沃思带阻滤波器来实现周期噪声的滤除,有效地滤除指定频率范围内的信号。经过滤波后的噪声图像条纹消失,清晰度提高。

验证性实验:对图像lena.bmp添加20%和70%的椒盐噪声,利用自适应中值滤波器对该图像进行复原,分别计算复原后图像的峰值信噪比psnr,并将实验结果和经典的中值滤波器进行对比,验证以下结论:与经典的中值滤波器相比,(1)自适应中值滤波器在有效抑制椒盐噪声的同时,能更好地保留图像细节信息。(2)多次使用自适应中值滤波器,图像模糊的副作用不明显。
clc,clear all;
figure(1)
%读取原始图像
f =im2double(imread('lena.bmp'));
subplot(3,3,1);
imshow(f);
title('lena.bmp','FontWeight', 'Bold', 'FontSize', 20);
 
%添加椒盐噪声20%
noise_20 = imnoise(f, 'salt & pepper', 0.2);
subplot(3,3,2);
imshow(noise_20);
psnr_f20_0 = psnr(f, noise_20);
% text(0, 30, ['添加20%椒盐噪声图像信噪比峰值: ', num2str(psnr_f20_0), 'dB'], 'Color', 'red','FontSize', 14,'FontWeight', 'bold');
% title(sprintf('添加20%椒盐噪声 psnr=%0.4f',psnr_f20_0),'FontWeight', 'Bold', 'FontSize', 20);
title('添加20%椒盐噪声','FontWeight', 'Bold', 'FontSize', 20);
xlabel(sprintf('psnr=%0.4f',psnr_f20_0),'FontWeight', 'Bold', 'FontSize', 20)
 
media_20=medfilt2(noise_20,[7,7],'symmetric');
subplot(3,3,4);
imshow(media_20);
psnr_f20_1 = psnr(f, media_20);
title('中值滤波 ','FontWeight', 'Bold', 'FontSize', 20);
xlabel(sprintf('psnr=%0.4f',psnr_f20_1),'FontWeight', 'Bold', 'FontSize', 20)
 
filter_n20=adpmedian(noise_20,7);
subplot(3,3,5);
imshow(filter_n20);
psnr_f20_2 = psnr(f, filter_n20);
title('自适应中值滤波 ','FontWeight', 'Bold', 'FontSize', 20);
xlabel(sprintf('psnr=%0.4f',psnr_f20_2),'FontWeight', 'Bold', 'FontSize', 20)
 
n=6;
for_20=filter_n20;
for i=2:4
    n=n+1;
    for_20=adpmedian(for_20,7);
    subplot(3,3,n);
    imshow(for_20);
    psnr_f20_3 = psnr(f, for_20);
    title(sprintf('第%d次自适应中值滤波', i),'FontWeight', 'Bold', 'FontSize', 20);
    xlabel(sprintf('psnr=%0.4f',psnr_f20_3),'FontWeight', 'Bold', 'FontSize', 20)
end
 
figure(2);
subplot(3,3,1);
imshow(f);
title('lena.bmp','FontWeight', 'Bold', 'FontSize', 20);
 
%添加椒盐噪声70%
noise_70 = imnoise(f, 'salt & pepper', 0.7);
subplot(3,3,2);
imshow(noise_70);
psnr_f70_0 = psnr(f, noise_70);
title('添加70%椒盐噪声','FontWeight', 'Bold', 'FontSize', 20);
xlabel(sprintf('psnr=%0.4f',psnr_f70_0),'FontWeight', 'Bold', 'FontSize', 20)
 
media_70=medfilt2(noise_70,[7,7],'symmetric');
subplot(3,3,4);
imshow(media_70);
psnr_f70_1 = psnr(f, media_70);
title('中值滤波','FontWeight', 'Bold', 'FontSize', 20);
xlabel(sprintf('psnr=%0.4f',psnr_f70_1),'FontWeight', 'Bold', 'FontSize', 20)
 
filter_n70=adpmedian(noise_70,7);
subplot(3,3,5);
imshow(filter_n70);
psnr_f70_2 = psnr(f, filter_n70);
title('自适应中值滤波 ' ,'FontWeight', 'Bold', 'FontSize', 20);
xlabel(sprintf('psnr=%0.4f',psnr_f70_2),'FontWeight', 'Bold', 'FontSize', 20)
 
n=6;
for_70=filter_n70;
for i=2:4
    n=n+1;
    for_70=adpmedian(for_70,7);
    subplot(3,3,n);
    imshow(for_70);
    title(sprintf('第%d次自适应中值滤波', i),'FontWeight', 'Bold', 'FontSize', 20);
    psnr_f70_3 = psnr(f, for_70);
    title(sprintf('第%d次自适应中值滤波', i),'FontWeight', 'Bold', 'FontSize', 20);
    xlabel(sprintf('psnr=%0.4f',psnr_f70_3),'FontWeight', 'Bold', 'FontSize', 20)
end

实验结果:

自适应中值滤波比中值滤波抑制椒盐噪声的效果更好,图像细节更明显。使用多次自适应中值滤波,对于不同噪声图像效果可能会有所不同,在椒盐噪声为20%的图像当中,图像的滤波出现边缘模糊;在椒盐噪声为70%的图像当中,能更有效的去除椒盐噪声,滤波效果更好。但是根据实验结果看出多次使用自适应中值滤波器后图像模糊的副作用并不明显。

噪声为20%的图片处理:

添加20%噪声psnr=12.4286

中值滤波psnr=25.5575

自适应中值滤波psnr=31.9232

第二次自适应中值滤波psnr=30.5820

第三次自适应中值滤波psnr=30.0018

第四次自适应中值滤波psnr=29.7086

噪声为70%的图片处理:

添加70%噪声psnr=6.9936

中值滤波psnr=17.9311

自适应中值滤波psnr=20.6947

第二次自适应中值滤波psnr=23.9096

第三次自适应中值滤波psnr=24.2507

第四次自适应中值滤波psnr=24.3065

实验总结

  1. 在图像中选择一个灰度值基本恒定的区域,计算这个区域的灰度直方图,由灰度直方图的形状可判断图像的噪声类型。
  2. 周期噪声幅度谱在高频处会出现极亮的点,对周期噪声的抑制步骤为①确定周期噪声对应的参数②设计带阻滤波器③利用频域滤波增强原理实现图像的频域滤波④观察输出图像及其幅度谱,若噪声抑制效果不理想,调整参数,重新设计滤波器,返回步骤③
  3. 自适应中值滤波器相比中值滤波器,可以提供更加平衡的滤波效果,尤其是在噪声密度变化较大的图像中。通过多次应用自适应中值滤波器,可以进一步减少图像中的噪声,尤其是在第一次滤波未能完全去除的噪声点。但多次应用后,边缘可能会变得不够清晰,影响图像的锐度。

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值