频域滤波与图像恢复.docx
- 文档编号:7294913
- 上传时间:2023-01-22
- 格式:DOCX
- 页数:16
- 大小:1.31MB
频域滤波与图像恢复.docx
《频域滤波与图像恢复.docx》由会员分享,可在线阅读,更多相关《频域滤波与图像恢复.docx(16页珍藏版)》请在冰豆网上搜索。
频域滤波与图像恢复
1、实验目的
1.1学习如何根据观察使用一个启发式方法选择一个阈值。
1.2探究函数graythresh以进行自动阈值选取。
1.3学习如何实现自适应阈值化。
二、实验内容
2.1使用prewitt算子进行边缘检测
2.1.1对lena.tif进行分别使用imfilter函数和edge函数进行三种算子的边缘检测
Imfilter函数实现代码:
clc,clearall,closeall
roberts1=[1,0;0,-1];
roberts2=[0,1;-1,0];
prewitt1=[-1,0,1;-1,0,1;-1,0,1];
prewitt2=[1,1,1;0,0,0;-1,-1,-1];
sobel1=[-1,0,1;-2,0,2;-1,0,1];
sobel2=[1,2,1;0,0,0;-1,-2,-1];
g=imread('C:
\DocumentsandSettings\Administrator\桌面\images\Lena.tif');
f=double(g);
f1=imfilter(f,roberts1);f2=imfilter(f,roberts2);
roberts_f=max(abs(f1),abs(f2));
f1=imfilter(f,prewitt1);f2=imfilter(f,prewitt2);
prewitt_f=max(abs(f1),abs(f2));
f1=imfilter(f,sobel1);f2=imfilter(f,sobel2);
sobel_f=max(abs(f1),abs(f2));
figure,subplot(2,2,1),imshow(g),title('原图像');
subplot(2,2,2),imshow(roberts_f,[]),title('Roberts边缘检测图像');
subplot(2,2,3),imshow(prewitt_f,[]),title('Prewitt边缘检测图像');
subplot(2,2,4),imshow(sobel_f,[]),title('Sobel边缘检测图像’);
Imfilter函数操作结果显示如下:
Edge函数代码如下:
clc,clearall,closeall;
f=imread('C:
\DocumentsandSettings\Administrator\桌面\images\lena.tif');
sobel_f=edge(f,'sobel');
roberts_f=edge(f,'roberts');
prewitt_f=edge(f,'prewitt');
figure,subplot(2,2,1),imshow(f),title('原图像');
subplot(2,2,2),imshow(roberts_f),title('Roberts边缘图像');
subplot(2,2,3),imshow(prewitt_f),title('Prewitt边缘图像');
subplot(2,2,4),imshow(sobel_f),title('Sobel边缘图像');
Edge函数实现结果如下:
思考:
[I_prw1,t1]=edge(I,’prewitt’);中t1表示什么?
答:
t1表示阈值,可如下使用:
[I_prw1,t1]=edge(I,’prewitt’);
t1=[‘prewitt边缘检测,阈值=’num2str(t1)];
imshow(I_prw1);
title(t1);
2.1.2加高斯噪声并提取边缘
代码如下:
prewitt1=[-1,0,1;-1,0,1;-1,0,1];
prewitt2=[1,1,1;0,0,0;-1,-1,-1];
f=imread('C:
\DocumentsandSettings\Administrator\桌面\images\lena.tif');
noisy=imnoise(f,'gaussian',0.07);
noise=double(noisy);
g=double(f);
f1=imfilter(g,prewitt1);f2=imfilter(g,prewitt2);
prewitt_g=max(abs(f1),abs(f2));
f1=imfilter(noise,prewitt1);f2=imfilter(noise,prewitt2);
prewitt_f=max(abs(f1),abs(f2));
figure,subplot(2,2,1),imshow(f),title('原图像');
subplot(2,2,2),imshow(noisy),title('含高斯噪声图像');
subplot(2,2,3),imshow(prewitt_g,[]),title('原图边缘检测图像');
subplot(2,2,4),imshow(prewitt_f,[]),title('含高斯噪声边缘检测图像');
思考:
Matlab对噪声图像使用同样的阈值吗?
答:
不应使用同样的阈值。
2.1.3尝试使用不同的阈值
prewitt1=[-1,0,1;-1,0,1;-1,0,1];
prewitt2=[2,1,2;0,0,0;-2,-1,-2];
prewitt1=[-2,0,2;-2,0,2;-2,0,2];
prewitt2=[2,1,2;0,0,0;-2,-1,-2];
prewitt1=[-0.5,0,0.5;-0.5,0,0.5;-0.5,0,0.5];
prewitt2=[0.5,0.5,0.5;0,0,0;-0.5,-0.5,-0.5];
prewitt1=[-0.1,0,0.1;-0.1,0,0.1;-0.1,0,0.1];
prewitt2=[0.1,0.1,0.1;0,0,0;-0.1,-0.1,-0.1];
思考:
阈值影响算子对噪声的响应吗?
如何影响目标的边缘?
答:
阈值的选择直接影响分割效果。
阈值影响算子对噪声的响应,
2.2使用sobel算子、roberts算子进行边缘检测
2.2.1对cameraman.tif进行边缘检测
实现代码:
clc,clearall,closeall
roberts1=[1,0;0,-1];
roberts2=[0,1;-1,0];
sobel1=[-1,0,1;-2,0,2;-1,0,1];
sobel2=[1,2,1;0,0,0;-1,-2,-1];
prewitt1=[-1,0,1;-1,0,1;-1,0,1];
prewitt2=[1,1,1;0,0,0;-1,-1,-1];
g=imread('cameraman.tif');
noisy=imnoise(g,'gaussian',0.07);
noise=double(noisy);
f=double(g);
f1=imfilter(f,roberts1);f2=imfilter(f,roberts2);
roberts_f=max(abs(f1),abs(f2));
f1=imfilter(f,sobel1);f2=imfilter(f,sobel2);
sobel_f=max(abs(f1),abs(f2));
f1=imfilter(f,prewitt1);f2=imfilter(f,prewitt2);
prewitt_f=max(abs(f1),abs(f2));
figure,subplot(2,2,1),imshow(g),title('原图像');
subplot(2,2,2),imshow(roberts_f,[]),title('Roberts边缘检测图像');
subplot(2,2,3),imshow(sobel_f,[]),title('Sobel边缘检测图像');
subplot(2,2,4),imshow(prewitt_f,[]),title('Prewitt边缘检测图像');
实现结果图:
2.2.2加高斯噪声并提取边缘
实现代码:
clc,clearall,closeall
roberts1=[1,0;0,-1];
roberts2=[0,1;-1,0];
sobel1=[-1,0,1;-2,0,2;-1,0,1];
sobel2=[1,2,1;0,0,0;-1,-2,-1];
prewitt1=[-1,0,1;-1,0,1;-1,0,1];
prewitt2=[1,1,1;0,0,0;-1,-1,-1];
g=imread('cameraman.tif');
noisy=imnoise(g,'gaussian',0.07);
noise=double(noisy);
f=double(g);
f1=imfilter(f,roberts1);f2=imfilter(f,roberts2);
roberts_f=max(abs(f1),abs(f2));
f1=imfilter(f,sobel1);f2=imfilter(f,sobel2);
sobel_f=max(abs(f1),abs(f2));
f1=imfilter(f,prewitt1);f2=imfilter(f,prewitt2);
prewitt_f=max(abs(f1),abs(f2));
g1=imfilter(noise,roberts1);g2=imfilter(noise,roberts2);
roberts_g=max(abs(g1),abs(g2));
g1=imfilter(noise,sobel1);g2=imfilter(noise,sobel2);
sobel_g=max(abs(g1),abs(g2));
g1=imfilter(noise,prewitt1);g2=imfilter(noise,prewitt2);
prewitt_g=max(abs(g1),abs(g2));
figure,subplot(4,2,1),imshow(g),title('原图像');
subplot(4,2,2),imshow(noisy),title('含高斯噪声图像');
subplot(4,2,3),imshow(roberts_f,[]),title('Roberts边缘检测图像');
subplot(4,2,4),imshow(roberts_g,[]),title('含噪声Roberts边缘检测图像');
subplot(4,2,5),imshow(sobel_f,[]),title('Sobel边缘检测图像');
subplot(4,2,6),imshow(sobel_g,[]),title('含噪声Sobel边缘检测图像');
subplot(4,2,7),imshow(prewitt_f,[]),title('Prewitt边缘检测图像');
subplot(4,2,8),imshow(prewitt_g,[]),title('含噪声prewitt边缘检测图像');
实现结果:
思考:
在有噪声和无噪声的情况下,sobel算子和prewitt算子、roberts算子比较如何?
答:
在上图中,为原图cameraman.tif叠加了高斯噪声,形成了一副含噪声的图像。
通过运行的结果图可以清晰的看出,在对没有叠加噪声的图像进行处理的时候Prewitt边缘检测的效果最好,人物的轮廓基本清晰,而Robert算子进行检测时,图像轮廓还不完整,部分边缘丢失。
在对有噪声的图像进行处理时,prewitt算子和Sobel算子都能较清晰的提取出图像的轮廓,而robert算子提取得轮廓就较为模糊,人物边缘线都不能很好的提出,说明Robel算子不具备抑制噪声的效果。
2.3实验内容3
2.3.1观察分析coins.tif的直方图,以确定合适的阈值T
clc,clearall,closeall
f=imread('F:
\images\coins.png');
figure,subplot(1,2,1),imshow(f),title('原图像');
subplot(1,2,2),imhist(uint8(f)),title('直方图');
问题:
直方图的哪个峰表示背景像素;哪个峰对应硬币的像素?
答:
高峰为背景像素,低峰为硬币像素,选择双峰谷底处为阈值,观察直方图得到T大约为95.
clc,clearall,closeall
f=imread('F:
\images\coins.png');
figure
(1),subplot(2,2,1),imshow(f),title('原图像');
subplot(2,2,2),imhist(uint8(f)),title('直方图');
s=input('目标物为?
','s');T=input('请输入适当的阙值');
[m,n]=size(f);
fori=1:
m
forj=1:
n
g(i,j)=f(i,j)>T;
end
end
subplot(2,2,3),imshow(g,[]),title('阙值分割图');
subplot(2,2,4),axisoff
text(0,0.6,['阙值T=',num2str(T)]);
text(0,0.8,['目标物为',s]);figure
(1);
3.2设定阈值T=85并生成新图像。
使用im2bw(I,T/255);
clc,clearall,closeall
f=imread('F:
\images\coins.png');
figure
(1),subplot(2,2,1),imshow(f),title('原图像');
subplot(2,2,2),imhist(uint8(f)),title('直方图');
T=85;
subplot(2,2,3),im2bw(f,T/255),title('阙值分割图');
subplot(2,2,4),axisoff
text(0,0.6,['阙值T=85']);
问题:
问题:
函数im2bw的作用是什么;为什么要除以255
答:
函数im2bw的作用是使用阈值(threshold)变换法把灰度图像(grayscaleimage)转换成二值图像。
除以255是为了把阈值化成0到1之间的数,对于图像上阈值大于该值的画成黑色,小于该值的就画成白色。
3.3调用graythresh()函数自动生成阙值
clc,clearall,closeall
f=imread('F:
\images\coins.png');
figure
(1),subplot(2,2,1),imshow(f),title('原图像');
subplot(2,2,2),imhist(uint8(f)),title('直方图');
T=graythresh(f);
subplot(2,2,3),im2bw(f,T),title('阙值分割图');
subplot(2,2,4),axisoff
text(0,0.6,['阙值T=',num2str(T)]);
问题:
效果有什么不同,graythresh有什么优势?
答:
(1)使用graythresh函数后产生的新图像更加圆了,边缘比较上两幅图更加平滑。
(2)自己选择的阈值不一定是最佳的,需要不停地调整取值,才能选择合适的阈值。
而greythresh函数会根据背景的不同,自动调整阈值,达到最佳的效果。
3.4采用不同的图像,并叠加不同程度的噪声,重复上述操作
代码实现:
clc,clearall,closeall
f=imread('F:
\images\blood.tif');
noisy=imnoise(f,'gaussian',0.5);
noise=double(noisy);
T=graythresh(f);
F=graythresh(noisy);
figure
(1),subplot(2,4,1),imshow(f),title('原图像');
subplot(2,4,2),imhist(uint8(f)),title('原图像直方图');
subplot(2,4,3),im2bw(f,T),title('原图像阙值分割图');
subplot(2,4,4),axisoff
text(0,0.6,['阙值T=',num2str(T)]);
subplot(2,4,5),imshow(noisy),title('含高斯噪声图像');
subplot(2,4,6),imhist(noisy),title('高斯噪声图像直方图');
subplot(2,4,7),im2bw(noisy,F),title('噪声图像阙值分割图');
subplot(2,4,8),axisoff
text(0,0.6,['噪声图像阙值T=',num2str(F)]);
实现结果:
noisy=imnoise(f,'gaussian',0.07);
三、实验心得
通过对不同的图像叠加噪声后使用Roberts算子、Sobel算子、Prewitt算子进行边缘提取可以发现,Roberts算子在进行边缘提取时容易丢失一部分边缘,而且抑制噪声的能力很弱,几乎没有,使用该算子对具有陡峭边缘且不含噪声或噪声很少的出现进行处理时效果较好。
而Sobel算子和Prewitt算子都对噪声具有一定的抑制效果,但也不能完全抑制,对有噪声的图像进行处理是还是有点模糊。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 滤波 图像 恢复