空间域滤波器(实验报告).pdf

上传人:tbuqq 文档编号:4630233 上传时间:2019-11-22 格式:PDF 页数:22 大小:3.14MB
返回 下载 相关 举报
空间域滤波器(实验报告).pdf_第1页
第1页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《空间域滤波器(实验报告).pdf》由会员分享,可在线阅读,更多相关《空间域滤波器(实验报告).pdf(22页珍藏版)》请在三一文库上搜索。

1、1 / 22 数字图像处理作业 空间域滤波器 摘要 在图像处理的过程中, 消除图像的噪声干扰是一个非常重要的问题。本文利用 matlab 软件, 采用空域滤波的方式 , 对图像进行平滑和锐化处理。平滑空间滤波 器用于模糊处理和减小噪声,经常在图像的预处理中使用;锐化空间滤波器主 要用于突出图像中的细节或者增强被模糊了的细节。本文使用的平滑滤波器有 中值滤波器和高斯低通滤波器,其中,中值滤波器对去除椒盐噪声特别有效, 高斯低通滤波器对去除高斯噪声效果比较好。使用的锐化滤波器有反锐化掩膜 滤波、 Sobel 边缘检测、 Laplacian边缘检测以及 Canny算子边缘检测滤波器。 不同的滤波方式

2、,在特定的图像处理应用中有着不同的效果和各自的优势。 b5E2RGbCAP 1、分别用高斯滤波器和中值滤波器去平滑测试图像test1和 2,模板大小分别 是 3x3 , 5x5 ,7x7;利用固定方差 sigma=1.5 产生高斯滤波器 . 附件有 产生高斯滤波器的方法。p1EanqFDPw 实验原理分析: 空域滤波是直接对图像的数据做空间变换达到滤波的目的。它是一种邻域运算, 其机理就是在待处理的图像中逐点地移动模板,滤波器在该点地响应通过事先 定义的滤波器系数与滤波模板扫过区域的相应像素值的关系来计算。如果输出 像素是输入像素邻域像素的线性组合则称为线性滤波是由参数 表征的,而且 和平 滑

3、程度的关系是非常简单的。 越大,高斯滤波器的频带就越宽,平滑程度就 越好。通过调节平滑程度参数,可在图像特征分量模糊 ( 过平滑 与平滑图像 中由于噪声和细纹理所引起的过多的不希望突变量( 欠平滑 之间取得折衷。 EmxvxOtOco 其中 fs(x,y表示经过反锐化掩蔽得到的锐化图像,是f(x,y的模糊形式。反 锐化掩蔽进一步的普遍形式称为高提升滤波。在图像中任何一点(x,y处,高提 升滤波后的图像 hb f 可定义如下:sQsAEJkW5T (2 其中 A1 ,与前式一样,是的模糊形式,此式也可以写成: (3 结合式 (1,可以得到: (4 这一表达式可计算高提升滤波图像。 如果选择拉普拉

4、斯变换,式(4变成: (5 f 10 / 22 高提升滤波处理可以通过任何一个图1 所示的掩模得以实现。当A=1时,高 提升滤波处理就是标准的拉普拉斯变换。随着A 超过 l 不断增大,锐化处理的 效果越来越不明显。最终,当A 足够大时,高提升图像将近似等于经常数调制 的图像。 GMsIasNXkA 图 1 高频提升滤波技术可以用其中一种掩膜来实现T2 ),T1 用 来找到每条线段, T2用来在这些线段的两个方向上延伸寻找边缘的断裂处,并 连接这些边缘。 AVktR43bpw 利用上述原理和 MATLAB 软件设计滤波器,对test3和 test4 进行边缘检测,结 果如下图所示: 源 图 像

5、test4 copy.bmpLaplacian 边 缘 检 测 (高 斯 滤 波 后 )test4 copy.bmp 源 图 像 -test3 corrupt.pgm Canny 边 缘 检 测 -test3 c orrupt.pgm 15 / 22 可见,经过Canny 算子较好的检测出了图像的所有边缘信息,具有较强的去噪 能力。 附录 一、参考文献 1冈萨雷斯著 .数字图像处理 (第三版 .北京:电子工业出版社,2018 2杨杰李庆著 .数字图像处理及MATLAB 实现学习与实验指导.北京:电子 工业出版社, 2018 3苏金明王永利著 .MATLAB图形图像 .北京:电子工业出版社,20

6、05 4 朱习军隋思涟等著 .MATLAB在信号与图像处理中的应用.北京:电子工业出 版社, 2009 5 张秀兰著 .基于 MATLAB的数字图像的边缘检测 .吉林化工学院学报, 2018 6 杨先花黎粤华著 .基于 MATLAB图像边缘检测算法效果对比.机电产品开发与 创新, 2018 二、源代码: 第一题 (1)平滑滤波器 1. 中值滤波器 。 ORjBnOwcEd n=3。 a=ones(n,n 。 Canny 边 缘 检 测 -test4 copy.bmp 16 / 22 p=size(I。 x1=double(I。x2=x1。 fori=1:p(1-n+1 for j=1:p(2-

7、n+1 c=x1(i:i+(n-1,j:j+(n-1。 e=c(1,:。 for u=2:n e=e,c(u,:。 end mm=median(e 。 x2(i+(n-1/2,j+(n-1/2=mm。 end end I2=uint8(x2。 imshow(I2 title( 中值滤波后的test1.pgm 大三下图像处理英文课件作业第四次作业 test2.tif,tif。 2MiJTy0dTT n=3。 a=ones(n,n 。 p=size(I。 x1=double(I。x2=x1。 fori=1:p(1-n+1 for j=1:p(2-n+1 c=x1(i:i+(n-1,j:j+(n-1

8、。 e=c(1,:。 for u=2:n e=e,c(u,:。 end mm=median(e 。 x2(i+(n-1/2,j+(n-1/2=mm。 end end I2=uint8(x2。 imshow(I2 title(中值滤波后的test2.tif 2、高斯滤波器 。 uEh0U1Yfmh r=cos(theta -sin(theta。 sin(theta cos(theta。IAg9qLsgBX fori = 1 : n2 for j = 1 : n1 u = r*j-(n1+1/2 i-(n2+1/2。 17 / 22 h(i,j=exp(-u(12/(2*sigma12/(sigm

9、a1*sqrt(2*pi*exp(- u(22/(2*sigma22/(sigma2*sqrt(2*pi。WwghWvVhPE end end h = h / sqrt(sum(sum(h.*h。 f1=conv2(I,h,same。 figure(1。 imagesc(I 。title(test1.pgm。 colormap(gray。 figure(2。 imagesc(f1 。title( 高斯滤波平滑后的test1.pgm(3x3。 colormap(gray。 n1=3 。sigma1=1.5 。n2=3。sigma2=1.5 。theta=0 。asfpsfpi4k 大三下图像处理

10、英文课件作业第四次作业 test2.tif,tif。 ooeyYZTjj1 r=cos(theta -sin(theta。 sin(theta cos(theta。BkeGuInkxI fori = 1 : n2 for j = 1 : n1 u = r*j-(n1+1/2 i-(n2+1/2。 h(i,j=exp(-u(12/(2*sigma12/(sigma1*sqrt(2*pi*exp(- u(22/(2*sigma22/(sigma2*sqrt(2*pi。PgdO0sRlMo end end h = h / sqrt(sum(sum(h.*h。 f1=conv2(I,h,same。 f

11、igure(1。 imagesc(I 。title(test2.tif。 colormap(gray。 figure(2。 imagesc(f1 。title( 高斯滤波平滑后的test2.tif(3x3。 colormap(gray。 。 3cdXwckm15 I=pic 。 picSize = size(pic。 pic = cast(pic,int32。 t = zeros(picSize(1 + 2 , picSize(2 + 2。 t = cast(t,int32。 t(2:picSize(1 + 1 , 2:picSize(2 + 1 = pic。 t(: , 1 = t(: ,

12、2。 t(: , picSize(2 + 2 = t(: , picSize(2 + 1。 t(1 , : = t(2 , :。 18 / 22 t(picSize(1 + 2 , : = t(picSize(1 + 1 , :。 A = 1.0 。 fori=2:1:picSize(1+1 for j=2:1:picSize(2+1 pic(i-1,j-1 = t(i,j*A - (t(i,j*(-8 + t(i,j-1 + t(i-1,j-1 + t(i-1,j + t(i,j+1 + t(i+1,j + t(i+1,j+1 + t(i+1,j-1 + t(i-1,j+1。 h8c52WO

13、ngM end end pic = cast(pic,uint8。 imwrite(pic,UnsharpMasking_A_, num2Str(10*A,.png,png。v4bdyGious figure。 subplot(1,2,1 imshow(I 。 title(原图像 test4 copy.bmp。 subplot(1,2,2 imshow(pic 。 title(反锐化掩膜后的test4 copy.bmp。 Sobel边缘检测 。N,M=size(I J0bm4qMpJ9 I=double(I。 h1=-1,0,1。-2,0,2。-1,0,1。 h2=-1,-2,-1。0,0,0

14、。1,2,1。 Gx=conv2(I,h1,same。 Gy=conv2(I,h2,same。 F=abs(Gx+abs(Gy。 fori=1:N for j=1:M I(i,j=F(i,j。 end end I=uint8(I。 figure。 imshow(I。 title(Sobel边缘检测test4 copy.bmp。 Laplace 边缘检测 a)Laplace 边缘检测test3_corrupt.pgmXVauA9grYP 大三下图像处理英文课件作业第四次作业 test3_corrupt.pgm,pgm。 bR9C6TJscw N,M=size(I。 figure。 subplot

15、(1,2,1。 imshow(I 。 19 / 22 title(?atest3_corrupt.pgm。 t=ones(N,M 。 t=I 。 fori=2:N-1 for j=2:M-1 I(i,j=t(i+1,j+t(i-1,j+t(i,j+1+t(i,j-1-4*t(i,j。pN9LBDdtrd end end subplot(1,2,2。 imshow(I 。 title(Laplacian边缘检测 -test3_corrupt.pgm。 b)Laplace 边缘检测test4 copy.bmp 未进行高斯滤波: 大三下图像处理英文课件作业第四次作业 test4 copy.bmp,b

16、mp。 N,M=size(I。DJ8T7nHuGT figure。 subplot(1,2,1。 imshow(I。 title(源图像 test4 copy.bmp。 t=ones(N,M。 t=I。 fori=2:N-1 for j=2:M-1 I(i,j=t(i+1,j+t(i-1,j+t(i,j+1+t(i,j-1-4*t(i,j。QF81D7bvUA end end subplot(1,2,2。 imshow(I。 title(Laplacian边缘检测 ( 未滤波 -test4 copy.bmp。 加入高斯滤波: 大三下图像处理英文课件作业第四次作业 test4 copy.bmp,

17、bmp。 N,M=size(I。4B7a9QFw9h figure。 subplot(1,2,1。 imshow(I。 title(源图像 test4 copy.bmp 。 n1=3 。sigma1=0.5。n2=3 。sigma2=0.5。theta=0。 r=cos(theta -sin(theta。 sin(theta cos(theta。 ix6iFA8xoX fori = 1 : n2 for j = 1 : n1 u = r*j-(n1+1/2 i-(n2+1/2。 h(i,j=exp(-u(12/(2*sigma12/(sigma1*sqrt(2*pi*exp(- u(22/(2

18、*sigma22/(sigma2*sqrt(2*pi。 wt6qbkCyDE 20 / 22 end end h = h / sqrt(sum(sum(h.*h。 f1=conv2(I,h,same。 t=ones(N,M。 t=f1。 fori=2:N-1 for j=2:M-1 f1(i,j=t(i+1,j+t(i-1,j+t(i,j+1+t(i,j-1-4*t(i,j。Kp5zH46zRk end end subplot(1,2,2。 imshow(f1。 title( Laplacian边缘检测 ( 高斯滤波后 -test4 copy.bmp 。 Canny算子边缘检测 。 Yl4Hd

19、OAA61 a = im2single(I。 m,n = size(a。 e = false(m,n。 GaussianDieOff = 0.0001。 PercentOfPixelsNotEdges = 0.7。 ThresholdRatio = 0.4。 sigma = 1。 thresh = 。 pw = 1:30。 ssq = sigma2。 width = find(exp(-(pw.*pw/(2*ssqGaussianDieOff,1,last。ch4PJx4BlI ifisempty(width width = 1。 end t = (-width:width。 gau = ex

20、p(-(t.*t/(2*ssq/(2*pi*ssq。 x,y=meshgrid(-width:width,-width:width。 dgau2D=-x.*exp(-(x.*x+y.*y/(2*ssq/(pi*ssq。 aSmooth=imfilter(a,gau,conv,replicate。 aSmooth=imfilter(aSmooth,gau,conv,replicate。 qd3YfhxCzo ax = imfilter(aSmooth, dgau2D, conv,replicate。E836L11DO5 ay = imfilter(aSmooth, dgau2D, conv,re

21、plicate。S42ehLvE3M mag = sqrt(ax.*ax + (ay.*ay。 magmax = max(mag(:。 ifmagmax0 mag = mag / magmax。 end 21 / 22 ifisempty(thresh counts=imhist(mag, 64。 highThresh = find(cumsum(counts PercentOfPixelsNotEdges*m*n,. 501nNvZFis 1,first / 64。 lowThresh = ThresholdRatio*highThresh。 thresh = lowThreshhighTh

22、resh。 elseif length(thresh=1 highThresh = thresh。 if thresh=1 eid = sprintf(Images:%s:thresholdMustBeLessThanOne, mfilename。 jW1viftGw9 msg = The threshold must be less than 1.。 error(eid,%s,msg。 end lowThresh = ThresholdRatio*thresh。 thresh = lowThreshhighThresh。 elseif length(thresh=2 lowThresh =

23、thresh(1。 highThresh = thresh(2。 if (lowThresh = highThresh | (highThresh = 1 eid = sprintf(Images:%s:thresholdOutOfRange, mfilename。xS0DOYWHLP msg = Thresh must be low high, where low 。 end end idxStrong = 。 for dir = 1:4 m,n = size(mag。 idx = find(ay-ay | (ay=0 & ax。 if isempty(idx v = mod(idx,m。

24、extIdx = find(v=1 | v=0 | idx(n-1*m。ZKZUQsUJed idx(extIdx = 。 end ixv = ax(idx。 iyv = ay(idx。 gradmag = mag(idx。 d = abs(iyv./ixv。 gradmag1 = mag(idx+m.*(1-d + mag(idx+m-1.*d。 gradmag2 = mag(idx-m.*(1-d + mag(idx-m+1.*d。 idxLocMax = idx(gradmag=gradmag1 & gradmag=gradmag2。 dGY2mcoKtT idxWeak = idxLo

25、cMax(mag(idxLocMax lowThresh。 e(idxWeak=1。 22 / 22 idxStrong = idxStrong。 idxWeak(mag(idxWeak highThresh。 rCYbSWRLIA end if isempty(idxStrong rstrong = rem(idxStrong-1, m+1。 cstrong = floor(idxStrong-1/m+1。 e = bwselect(e, cstrong, rstrong, 8。 e = bwmorph(e, thin, 1。 end imshow(e。 title(Canny边缘检测 -test4 copy.bmp。 申明: 所有资料为本人收集整理,仅限个人学习使用,勿做商业用途。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1